制品晋级实践
制品晋级也需要一条流水线来负责制品的晋级。
准备仓库: 按照环境不同,创建四个仓库分别存储不同环境的包。通过一条单独的流水线完成晋级。
如果包没有问题,可以将这个包晋级到其他仓库当中,步骤就是先下载包,然后传上去。
用户选择:
- 晋级策略
- 包信息
Jenkinsfile内容:先选晋级策略,然后需要将晋级的包下载下来,然后上传到新的仓库。
获取到制品的信息:
String artifactURL = "${env.artifactUrl}".toString()pipeline {agent anystages {stage('Hello') {steps {script{println("The artifact url is: ${artifactURL}")env.version = "${params.artifactUrl}".split('/')[-2]env.appName = "${params.artifactUrl}".split('/')[-3]env.buName = "${params.artifactUrl}".split('/')[-4]env.pkgName = env.appName + "-${version}.jar"sh """echo ${params.artifactUrl} -o ${env.pkgName} -u admin:admin123"""}}}}
}
Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on build-01 in /data/cicd/jenkinsagent/workspace/nexus/nexus-chajian-download
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] script
[Pipeline] {
[Pipeline] echo
The artifact url is: http://139.198.166.235:8082/repository/devops-micro/org/devops/devops-lib/8.8.8/devops-lib-8.8.8.jar
[Pipeline] sh
+ echo http://139.198.166.235:8082/repository/devops-micro/org/devops/devops-lib/8.8.8/devops-lib-8.8.8.jar -o devops-lib-8.8.8.jar -u admin:admin123
http://139.198.166.235:8082/repository/devops-micro/org/devops/devops-lib/8.8.8/devops-lib-8.8.8.jar -o devops-lib-8.8.8.jar -u admin:admin123
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
完整代码如下:
currentBuild.description = "trigger by ${params.updatePolicy}"pipeline {agent {label "build"}stages{stage("PullArtifact"){steps{script{env.version = "${params.artifactUrl}".split('/')[-2]env.appName = "${params.artifactUrl}".split('/')[-3] // Day5/acmp-myapp-serviceenv.buName = "${params.artifactUrl}".split('/')[-4] env.pkgName = env.appName + "-${version}.jar"sh """curl ${params.artifactUrl} -o ${env.pkgName} -u admin:admin123"""}}}stage("UpdateArtifact"){steps{script{switch("${params.updatePolicy}") {case "dev>uat":repoName = "zeyang-uat"targetDir = "${env.buName}/${env.appName}/${env.version}/"PushRawArtifacts(repoName,targetDir, "./", "${env.pkgName}" )break;case "uat>stag":repoName = "zeyang-stag"targetDir = "${env.buName}/${env.appName}/${env.version}/"PushRawArtifacts(repoName,targetDir, "./", "${env.pkgName}" )break;case "stag>prod":repoName = "zeyang-prod"targetDir = "${env.buName}/${env.appName}/${env.version}/"PushRawArtifacts(repoName,targetDir, "./", "${env.pkgName}" )break;default:println("xxxxxxxxx")}}}}}
}//上传制品
def PushRawArtifacts(repoName,targetDir, filePath, pkgName ){withCredentials([usernamePassword(credentialsId: '111b337e-89a0-4761-9ca3-6ee626ccd7a1', passwordVariable: 'TOKEN', usernameVariable: 'USER')]) {sh """curl -X POST "http://192.168.1.200:8081/service/rest/v1/components?repository=${repoName}" \-H "accept: application/json" \-H "Content-Type: multipart/form-data" \-F "raw.directory=${targetDir}" \-F "raw.asset1=@${filePath}/${pkgName};type=application/java-archive" \-F "raw.asset1.filename=${pkgName}" \-u "${USER}":"${TOKEN}""""}}
总结:先选择晋级策略,然后下载原始的制品,然后获取到原始的ID信息,然后重命名更改名称,最后上传到制品库就行了。