如何在 Jenkinsfile 中将 Artifactory discardOldBuilds 设置为 true?
How to set Artifactory discardOldBuilds to true in a Jenkinsfile?
我正在使用 Jenkins 多分支管道实施构建作业。最后阶段是将构建输出上传到 Artifactory。
通过界面配置独立作业时,有一个 "Discard old builds from Artifactory" 选项,它允许我只保留与 "Max # of builds to keep" 设置中指定的相同数量的构建。
我的 Jenkinsfile 中的上传阶段是这样配置的:https://wiki.jenkins-ci.org/display/JENKINS/Artifactory+-+Working+With+the+Pipeline+Jenkins+Plugin
...我的 Jenkinsfile 中还有以下内容,它会清理 Jenkins 工作区中的构建:
properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '10']]])
如何在我的 Jenkinsfile 中将 discardOldBuilds 设置为 true 以便我还可以清理存储在 Artifactory 中的构建?
进入官方 JenkinsFile https://github.com/jenkinsci/jenkins/blob/master/Jenkinsfile 有:
properties([[$class: 'jenkins.model.BuildDiscarderProperty', strategy:
[$class: 'LogRotator', numToKeepStr: '50', artifactNumToKeepStr: '20']
]])
自己使用 github 插件和 jenkins 多分支管道的示例:
#!groovy
node {
try {
properties([
[
$class: 'jenkins.model.BuildDiscarderProperty',
strategy: [
$class: 'LogRotator',
numToKeepStr: '10'
]
],
[
$class: 'GithubProjectProperty',
displayName: '',
projectUrlStr: 'https://github.com/xxxxxxx/xxxxx-xxxx/'
]
])
env.JAVA_HOME = tool 'JDK8'
def mvnHome = tool 'Maven'
stage 'Clean Workspace'
deleteDir()
stage 'Git Checkout Source'
checkout scm
stage 'Build Maven Module'
sh "${mvnHome}/bin/mvn clean install -DskipTests=true -DskipITs=true -U"
stage 'Unit Test Maven Module'
sh "${mvnHome}/bin/mvn test -DskipTests=false -DskipITs=true"
stage 'Integration Test Maven Module'
sh "${mvnHome}/bin/mvn verify -DskipTests=true -DskipITs=false"
stage 'Nexus Deploy Artifact'
sh "${mvnHome}/bin/mvn deploy:deploy -Pnexus-deploy"
stage 'Trigger Job xxxxxx-xxxxxx /master'
build job: 'xxxxxx-xxxxxx/master', propagate: false, wait: false
stage 'Send Success Email'
mail from: 'noreply-jenkins@xxxxxxx.io',
to: 'tools@xxxxxxx.io',
subject: "[JENKINS] ${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - [SUCCESS]!",
body: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - SUCCESS!"
}
catch (Exception ex) {
mail from: 'noreply-jenkins@xxxxxxx.io',
to: 'tools@xxxxxxx.io',
subject: "FAILURE - [JENKINS] ${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - [FAILURE]!",
body: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - FAILURE (${ex.message})!"
throw ex
}
}
在我的 Sonatype Nexus 中,我创建了一个清理工件的任务。
我不使用'Artifactory',但我相信你可以创建一个内部任务来清除它。
也可以手动去掉,在maven使用的情况下,可以按照这个例子:
How do you deal with maven-3 timestamped snapshots efficiently?
希望对你有所帮助。
计划在即将发布的 Jenkins Artifactory 插件版本中支持从管道脚本触发 Artifactory 构建保留。看到这个问题:
https://www.jfrog.com/jira/browse/HAP-796
这已在最新的 Artifactory Jenkins 插件中修复。
根据文档,要在将构建信息发布到 Artifactory 时触发构建保留,请使用以下方法:
buildInfo retention maxBuilds: 10
buildInfo retention maxDays: 7
// deleteBuildArtifacts is false by default.
buildInfo retention maxBuilds: 10, maxDays: 7, doNotDiscardBuilds: ["3", "4"], deleteBuildArtifacts: true
我正在使用 Jenkins 多分支管道实施构建作业。最后阶段是将构建输出上传到 Artifactory。
通过界面配置独立作业时,有一个 "Discard old builds from Artifactory" 选项,它允许我只保留与 "Max # of builds to keep" 设置中指定的相同数量的构建。
我的 Jenkinsfile 中的上传阶段是这样配置的:https://wiki.jenkins-ci.org/display/JENKINS/Artifactory+-+Working+With+the+Pipeline+Jenkins+Plugin
...我的 Jenkinsfile 中还有以下内容,它会清理 Jenkins 工作区中的构建:
properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '10']]])
如何在我的 Jenkinsfile 中将 discardOldBuilds 设置为 true 以便我还可以清理存储在 Artifactory 中的构建?
进入官方 JenkinsFile https://github.com/jenkinsci/jenkins/blob/master/Jenkinsfile 有:
properties([[$class: 'jenkins.model.BuildDiscarderProperty', strategy:
[$class: 'LogRotator', numToKeepStr: '50', artifactNumToKeepStr: '20']
]])
自己使用 github 插件和 jenkins 多分支管道的示例:
#!groovy
node {
try {
properties([
[
$class: 'jenkins.model.BuildDiscarderProperty',
strategy: [
$class: 'LogRotator',
numToKeepStr: '10'
]
],
[
$class: 'GithubProjectProperty',
displayName: '',
projectUrlStr: 'https://github.com/xxxxxxx/xxxxx-xxxx/'
]
])
env.JAVA_HOME = tool 'JDK8'
def mvnHome = tool 'Maven'
stage 'Clean Workspace'
deleteDir()
stage 'Git Checkout Source'
checkout scm
stage 'Build Maven Module'
sh "${mvnHome}/bin/mvn clean install -DskipTests=true -DskipITs=true -U"
stage 'Unit Test Maven Module'
sh "${mvnHome}/bin/mvn test -DskipTests=false -DskipITs=true"
stage 'Integration Test Maven Module'
sh "${mvnHome}/bin/mvn verify -DskipTests=true -DskipITs=false"
stage 'Nexus Deploy Artifact'
sh "${mvnHome}/bin/mvn deploy:deploy -Pnexus-deploy"
stage 'Trigger Job xxxxxx-xxxxxx /master'
build job: 'xxxxxx-xxxxxx/master', propagate: false, wait: false
stage 'Send Success Email'
mail from: 'noreply-jenkins@xxxxxxx.io',
to: 'tools@xxxxxxx.io',
subject: "[JENKINS] ${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - [SUCCESS]!",
body: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - SUCCESS!"
}
catch (Exception ex) {
mail from: 'noreply-jenkins@xxxxxxx.io',
to: 'tools@xxxxxxx.io',
subject: "FAILURE - [JENKINS] ${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - [FAILURE]!",
body: "${env.JOB_NAME} - Build # ${env.BUILD_NUMBER} - FAILURE (${ex.message})!"
throw ex
}
}
在我的 Sonatype Nexus 中,我创建了一个清理工件的任务。
我不使用'Artifactory',但我相信你可以创建一个内部任务来清除它。
也可以手动去掉,在maven使用的情况下,可以按照这个例子:
How do you deal with maven-3 timestamped snapshots efficiently?
希望对你有所帮助。
计划在即将发布的 Jenkins Artifactory 插件版本中支持从管道脚本触发 Artifactory 构建保留。看到这个问题: https://www.jfrog.com/jira/browse/HAP-796
这已在最新的 Artifactory Jenkins 插件中修复。
根据文档,要在将构建信息发布到 Artifactory 时触发构建保留,请使用以下方法:
buildInfo retention maxBuilds: 10
buildInfo retention maxDays: 7
// deleteBuildArtifacts is false by default.
buildInfo retention maxBuilds: 10, maxDays: 7, doNotDiscardBuilds: ["3", "4"], deleteBuildArtifacts: true