jenkinsfile将公共环境变量放在共享库资源文件中访问

当我们有大量项目时,大体构建步骤一样,但是有很多的环境变量时,我们可以抽取出来,形成文件,并通过共享库方式引入,

通过再次生成文件,调用load方式加载进pipeline环境中。

目录结构:

jenkinsfile:

#!groovy
@Library("jenkinslibrary") _
 
 
 
//def parser = new org.devops.parseJson()
def healthCheck = new org.devops.healthCheck()
def harborSearch = new org.devops.harborSearch()
def k8sSearch = new org.devops.k8sSearch()
def exportEnv = new org.devops.exportEnv()
 
String Service = "${env.Service}"
String devOrProd = "${env.devOrProd}"
def dockerFile = libraryResource "com/ehome2/dockerfile"
def deploymentYamlFile = libraryResource "com/ehome2/deployment.yaml"
def serviceYamlFile = libraryResource "com/ehome2/service.yaml"
def baseEnvConfig = libraryResource "com/ehome2/${devOrProd}/base.groovy"
def serviceEnvConfig = libraryResource "com/ehome2/${devOrProd}/${Service}.groovy"
 
 
 
//pipeline
pipeline{
    agent any
    options {
    timestamps() //日志会有时间
    skipDefaultCheckout() //删除隐式checkout scm语句
    disableConcurrentBuilds() //禁止并行
    timeout(time: 1, unit: "HOURS") //流水线超时设置1h
	}
 
 
    
    stages{
    
		stage("initial env"){
			steps{
				script{
                    //将共享库中的resource资源写入文件
					sh "echo \'${baseEnvConfig}\' > base.groovy"
                    // 通过load函数 加载groovy文件,使环境变量直接生效   
					load 'base.groovy'
					sh "echo \'${serviceEnvConfig}\' > service.groovy" 
					load 'service.groovy'
				}
			}
		}
		stage("test env"){
			steps{
				script{
                    // 测试环境变量是否全局有效
                    println("MemConf ${env.MemConf}")
					println("MemConf ${MemConf}")
				    println("MemConf ${env.dockerFile_start }")
					println("MemConf ${dockerFile_start }")
					println("MemConf ${env.target_dir  }")
					println("MemConf ${target_dir }")
				}
			}
		}

base.groovy

env.gitlab_auth = "8ad24c23-ff19-422f-af3d-xxxxxxxxxxxx"
env.gitlab_url = "http://172.24.112.158/root/project.git"
env.gitlab_branch = "*/uat"
env.harbor_auth = "91de3ca0-67d9-4706-b036-xxxxxxxxxxxx"
env.harbor_host =  "172.24.101.210"
env.release = "uat"
env.JDK_V = "8"
env.k8s_master_ip = "172.24.101.219"
env.NS = "project"
env.server_replicas = "1"
env.Apollo = "http://172.24.101.175:8080"

service.groovy

env.Port = "21111"
env.Appid = "ddd-248"
env.target_dir = "${WORKSPACE}/urm-parent/urm-module/urm-user"
env.request_mem = "1Gi"
env.limit_mem = "2Gi"
env.java_Xms = "1g"
env.java_Xmx = "1g"
env.java_Xmn = "512m"
env.MemConf = "-Xms${env.java_Xms} -Xmx${env.java_Xmx} -Xmn${env.java_Xmn}"
env.JAVA_OPTS = "-server ${env.MemConf} -XX:SurvivorRatio=8 -XX:MetaspaceSize=256m -XX:MaxMetaspaceSize=512m -XX:+UseParallelGC -XX:ParallelGCThreads=4 -XX:+UseParallelOldGC -XX:+UseAdaptiveSizePolicy -XX:+PrintGCDetails -XX:+PrintTenuringDistribution -XX:+PrintGCTimeStamps -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/ -Xloggc:/gc.log -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=10M"
env.dockerFile_start = "java ${env.JAVA_OPTS} -Denv=${env.release} -Dapp.id=${env.Appid} -Dapollo.configService=${env.Apollo} -Dserver.port=${env.Port} -Djava.security.egd=file:/dev/./urandom -jar /opt/${Service}.jar"

转载至:https://blog.csdn.net/kcrist/article/details/105387723

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值