当我们有大量项目时,大体构建步骤一样,但是有很多的环境变量时,我们可以抽取出来,形成文件,并通过共享库方式引入,
通过再次生成文件,调用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