如何在Jenkinsfile中将Artifactory discardOldBuilds设置为true?

时间:2016-06-22 16:58:14

标签: jenkins artifactory jenkins-pipeline

我正在使用Jenkins Multibranch Pipelines实现构建作业。最后一步是将构建输出上传到Artifactory。

通过界面配置独立作业时,可以选择"从Artifactory中删除旧版本"这使得我只能保持与#34; Max#of builds中指定的相同数量的构建保持"设置。

我的Jenkins文件中的上传阶段配置如下:https://wiki.jenkins-ci.org/display/JENKINS/Artifactory+-+Working+With+the+Pipeline+Jenkins+Plugin

...我的Jenkins文件中还有以下内容,它清理了Jenkins工作区中的构建:

properties([[$class: 'BuildDiscarderProperty', strategy: [$class: 'LogRotator', numToKeepStr: '10']]])

如何在我的Jenkinsfile中将discardOldBuilds设置为true,这样我还可以清理存储在Artifactory中的构建?

3 个答案:

答案 0 :(得分:4)

进入官方JenkinsFile https://github.com/jenkinsci/jenkins/blob/master/Jenkinsfile有:

properties([[$class: 'jenkins.model.BuildDiscarderProperty', strategy: 
            [$class: 'LogRotator', numToKeepStr: '50', artifactNumToKeepStr: '20']
           ]])

使用github插件和jenkins multibranch管道自己使用的一个例子:

#!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中,我创建了一个清理工件的任务。 enter image description here

我不使用“Artifactory'”,但我相信你可以创建一个内部任务来清除它。

您也可以手动删除,在使用maven的情况下,您可以按照以下示例:

How do you deal with maven-3 timestamped snapshots efficiently?

我希望我帮助过你。

答案 1 :(得分:4)

这已在最新的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

答案 2 :(得分:3)

触发Artifactory计划在即将发布的Jenkins Artifactory插件中支持从Pipeline脚本构建保留。看到这个问题: https://www.jfrog.com/jira/browse/HAP-796