Artifactory保留具有最大构建数

时间:2018-05-25 19:09:38

标签: jenkins artifactory

我正在使用带有Jenkins的Artifactory开源版本。我们将构建从Jenkins推送到Artifactory,其构建信息如下所示。

 def server = Artifactory.server('Artifactory_Server')  
 def targetArtifact = "Repo/${RepoName}/${archivePath}.zip" 

                        def uploadSpec = """{
                        "files": [
                                {
                                    "pattern": "${archivePath}.zip",            
                                    "target":  "${targetArtifact}"                              
                                }
                            ]
                            }"""    
def buildInfo = Artifactory.newBuildInfo()
buildInfo.retention maxBuilds: 2, maxDays: 2, doNotDiscardBuilds: ["5"], deleteBuildArtifacts: true                         

buildInfo.env.capture = true        

server.upload(uploadSpec)
server.publishBuildInfo(buildInfo)  

在将构建信息发布到Artifactory服务器时,不会删除或删除构建。应该怎么做才能删除构建

2 个答案:

答案 0 :(得分:0)

对于任何其他发现此问题的人,我都听说过构建保留设置仅适用于Artifactory专业版。我尚未验证它不能免费运行,但已验证以下与专业Artifactory一起使用的jenkinsfile可以正常工作:

node {
    // Obtain an Artifactory server instance, defined in Jenkins --> Manage:
    def server = Artifactory.server SERVER_ID
    def buildInfo = Artifactory.newBuildInfo()

    stage ('Collect env vars') {
        // By default the filter is configured to exclude "*password*,*secret*,*key*"
        buildInfo.env.filter.addInclude("*")
        buildInfo.env.filter.addExclude("DONT_COLLECT*")

        withEnv(['DO_COLLECT_FOO=BAR', 'DONT_COLLECT_FOO=BAR']) {
            buildInfo.env.collect()
        }
    }

    stage ('Access build info env vars') {
        echo buildInfo.env.vars['DO_COLLECT_FOO']   //BAR
        echo buildInfo.env.vars['DONT_COLLECT_FOO'] //null
    }

    stage ('Set build retention') {
        buildInfo.retention maxBuilds: 1, maxDays: 2, doNotDiscardBuilds: ["3"], deleteBuildArtifacts: true
    }

    stage ('Publish build info') {
        server.publishBuildInfo buildInfo
    }
}

这是来自JFrog examples github page

另一个重要的一点是,这些保留规则似乎是针对元数据(存储在artifactory-build-info中的json文件)的,而不是伪造的。

也有必要检查一下在Artifactory插件中配置的用户在Artifactory中是否具有足够的权限来进行清理。可以在Artifactory中执行以下操作: 工件->工件库浏览器-> artifactory-build-info->有效权限

答案 1 :(得分:0)

起初我也认为它不起作用,但后来我发现工件 (deleteBuildArtifacts: true) 和构建信息只有在您上传新的构建信息时才会被删除(再次触发詹金斯工作)。

我使用以下管道对其进行了测试:

#!groovy

node('master') {
    sh ("touch foo${BUILD_NUMBER}.txt")
    
    def server = Artifactory.server('artifactory')
    def uploadSpec = """{
            "files": [
                {
                    "pattern": "foo${BUILD_NUMBER}.txt",
                    "target": "<repo-name>"
                }
            ]
        }"""

    def buildInfo = server.upload spec: uploadSpec
    buildInfo.retention maxDays: 1, deleteBuildArtifacts: true // this will remove all artifacts + build-info older than 1 day (retrigger job)
    //buildInfo.retention maxBuilds: 1, deleteBuildArtifacts: true // this will only keep 1 artifact + build-info
    server.publishBuildInfo(buildInfo)
}

如下所述:https://www.jfrog.com/confluence/display/JFROG/Scripted+Pipeline+Syntax#ScriptedPipelineSyntax-TriggeringBuildRetention

Artifactory 版本:商业 7.10.2