在推广/推广步骤中删除詹金斯构建 [英] Delete jenkins builds during Promote / promotion step

查看:26
本文介绍了在推广/推广步骤中删除詹金斯构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Jenkins:版本 1.525
Jenkins 服务器 URL:http://my.jenkins.server.com:9040
Linux 红帽 5.3

Jenkins: Version 1.525
Jenkins Server URL: http://my.jenkins.server.com:9040
Linux Red Hat 5.3

Artifactory:免费版
Artifactory 服务器 URL:http://my.artifactory.server:8081/Artifactory

我能够成功地在 Jenkins 中构建并将工件上传到我的 Artifactory 服务器下的相应存储库.

I'm successfully able to build in Jenkins and upload artifacts to my Artifactory server under a respective repository.

当构建发生时,工件 (ProjectA-1.0.0.25.tar.gz) 转到 libs-snapshot-local 存储库下的 Artifactory.这里 1.0.0 表示应用程序/jenkins 作业的给定版本的主要、次要和临时版本:在这种情况下为ProjectA".假设 25 是内部版本号


当 ProjectA 构建在开发中稳定后,我们会将该应用程序版本的给定构建提升到 INT 或任何其他更高的环境(QA/PrePROD 等).

When a build occurs, the artifacts (ProjectA-1.0.0.25.tar.gz) goes to Artifactory under libs-snapshot-local repository. Here 1.0.0 is denoting Major, minor and interim version of a given release for an application/jenkins job: "ProjectA" in this case. Lets say 25 is the build number


When ProjectA build gets stable in Development, we promote a given build of that application release to INT or any other higher environment (QA/PrePROD etc).

在这个推广过程中,我们只要选择推广哪个build,使用Jenkins Promoted Build Plugin就可以成功.

During this promotion process, we just select which build to promote and using Jenkins Promoted Build Plugin, we are able to do it successfully.

现在,我们需要的是:

  1. 在升级过程中,我想调用一个 Groovy 脚本,它会在ProjectA-1.0.0.25.tar.gz"成功晋升为INT.促销部分现在工作正常;我只需要一个 Groovy 脚本,它将删除 Jenkins 中的 Jenkins 构建(1.0.0.1 到 1.0.0.24 和 >= 1.0.0.25)及其来自 Artifactory 存储库(libs-snapshot-local)的相关工件.

我们公司的想法是,一旦为应用程序提升了发布版本的构建,我们拥有的所有其他构建/工件(在 Jenkins/Artifactory 中) - 我们希望使用 Groovy 脚本永久删除.有人会问,如果我想推广一个不同的构建怎么办#;好吧,在我们的情况下,我们不希望那样.简单的规则是,如果有人推广 ProjectA-1.0.0.25.tar.gz,那么在 Jenkins 和 Artifactory 中删除 ProjectA 的构建/工件,其中构建/工件不是 1.0.0.25,并继续使用新版本 1.1.0

Idea in our company is, once a release version's build is promoted for an application, all other builds / artifacts we have (in Jenkins/Artifactory) - we want to delete forever using a Groovy script. Someone will ask, what if I want to promote a different build#; well in our case, we dont want that. Simple rule is, if someone promotes ProjectA-1.0.0.25.tar.gz, then, delete ProjectA's builds / artifacts in Jenkins and Artifactory where build/artifact is other than 1.0.0.25 and continue with new release 1.1.0


具有以下功能的脚本会很棒.
1. 使用属性文件(jenkins.properties/artifactory.properties)——如果有的话,它会包含一些关于主机名/用户名/密码等的变量.
2. 使用 REST API 对给定的应用程序/作业和给定的版本(例如 1.0.0)执行删除
3. 可用于 Jenkins/Artifactory 删除 - 如果在命令提示符下,我说使用这个 (Jenkins) 属性文件 - 或那个 (Artifatory) - 因为在这两种情况下,应用程序及其发布值将相同.
4. 我们知道,为了将构建提升到 INT(使用 Jenkins 提升插件),我们将始终仅在 libs-snapshot-release 从 Jenkins 服务器和 Artifactory 服务器中删除.
现在,如果有人升级为 QA(稍后),那么 artifactory 存储库将是 (libs-stage-local)

The script with the following capability would be great.
1. Use property files (jenkins.properties / artifactory.properties) - which will contains some variables about hostname/username/password etc if any.
2. Use REST API to perform the deletion for a given application/job and given release (for ex 1.0.0)
3. Can be used for both Jenkins/Artifactory deletion - if at command prompt, I say use this (Jenkins) property file -or that (Artifatory) - as in both cases, application and its release value will be same.
4. We know that for promoting a build to INT (using Jenkins promote plugin), we'll always delete from Jenkins server and from Artifactory server only at libs-snapshot-release.
Now if someone does promotion to QA (at a later time), then artifactory repository will be (libs-stage-local)

换句话说,我们应该调用 Groovy 脚本,传递一些变量/值 (REST) 并告诉要删除哪个应用程序/作业以及它的构建发布版本.然后,它将删除除用户将通过的构建之外的所有构建(即 1.0.0.25)

In other words, we should call the Groovy script, pass some variables/values (REST) and tell which application/job to delete and what build release version it's. Then, it'll delete all builds except the one which a user will pass (i.e. 1.0.0.25)

我是 Groovy 的新手/使用 REST API 为 Jenkins/Artifactory 执行这项删除"工作.如果有人已经有任何执行此类活动的示例脚本并且您可以共享,我将根据我的设置对其进行调整,看看我是否可以在升级步骤中看到上述行为.我有一些时间来获取这个脚本的工作版本,希望能有一些脚本代码做同样的任务(而不是伟大的人告诉我去查看大文档/链接,我知道这会让我成为一个更好的 Groovy 编码员但它会延迟这篇文章的整个目的).

I'm new to both Groovy / using REST API for doing this "deletion" piece of work for Jenkins/Artifactory. If someone already has any sample script that does this kind of activity and if you can share, I'll tweak it according to my settings and see if I can see the above mentioned behaviour during promotion step. I have some time crunch in getting a working version of this script, would appreciate some script code doing the same task (instead of great people telling me to go through big documentations/links, I know that'll make me a better coder in Groovy but it'll delay the whole purpose for this post).

非常感谢.

推荐答案

最终答案:这包括使用 Artifactor 的 REST API 调用从 Artifactory 中删除构建工件.此脚本将删除给定发布/版本的 Jenkins/Artifactory 构建/工件(有时随着时间的推移 - 给定的 Jenkins 作业可以创建多个发布/版本构建,例如:2.75.0.1、2.75.0.2、2.75.0.3,....,2.75.0.54, 2.76.0.1, 2.76.0.2, ..., 2.76.0.16, 2.76.1.1, 2.76.1.2, ...., 2.76.1.5).在这种情况下,对于该作业的每个新版本,我们从 1 个新版本开始构建#.如果您必须删除除一个/甚至所有版本之外的所有版本(根据您自己的需要稍微更改脚本)并且不要更改旧版本/其他版本版本,请使用以下脚本.

FINAL Answer: This includes deleting the build artifacts from Artifactory as well using Artifactor's REST API call. This script will delete Jenkins/Artifactory builds/artifacts of a given Release/Version (as sometimes over the time - a given Jenkins job can create multiple release / version builds for ex: 2.75.0.1, 2.75.0.2, 2.75.0.3,....,2.75.0.54, 2.76.0.1, 2.76.0.2, ..., 2.76.0.16, 2.76.1.1, 2.76.1.2, ...., 2.76.1.5). In this case, for every new release of that job, we start the build# from 1 fresh. If you have to delete the all builds except one / even all (change the script a little bit for your own needs) and don't change older/other release builds, then use the following script.

Scriptler 目录链接:http://scriptlerweb.appspot.com/script/show/103001

享受吧!

/*** BEGIN META {
  "name" : "Bulk Delete Builds except the given build number",
  "comment" : "For a given job and a given build numnber, delete all builds of a given release version (M.m.interim) only and except the user provided one. Sometimes a Jenkins job use Build Name setter plugin and same job generates 2.75.0.1 and 2.76.0.43",
  "parameters" : [ 'jobName', 'releaseVersion', 'buildNumber' ],
  "core": "1.409",
  "authors" : [
     { name : "Arun Sangal - Maddys Version" }
  ]
} END META **/

import groovy.json.*
import jenkins.model.*;
import hudson.model.Fingerprint.RangeSet;
import hudson.model.Job;
import hudson.model.Fingerprint;

//these should be passed in as arguments to the script
if(!artifactoryURL) throw new Exception("artifactoryURL not provided")
if(!artifactoryUser) throw new Exception("artifactoryUser not provided")
if(!artifactoryPassword) throw new Exception("artifactoryPassword not provided")
def authString = "${artifactoryUser}:${artifactoryPassword}".getBytes().encodeBase64().toString()
def artifactorySettings = [artifactoryURL: artifactoryURL, authString: authString]

if(!jobName) throw new Exception("jobName not provided")
if(!buildNumber) throw new Exception("buildNumber not provided")

def lastBuildNumber = buildNumber.toInteger() - 1;
def nextBuildNumber = buildNumber.toInteger() + 1;

def jij = jenkins.model.Jenkins.instance.getItem(jobName);

def promotedBuildRange = new Fingerprint.RangeSet()
promotedBuildRange.add(buildNumber.toInteger())
def promoteBuildsList = jij.getBuilds(promotedBuildRange)
assert promoteBuildsList.size() == 1
def promotedBuild = promoteBuildsList[0]
// The release / version of a Jenkins job - i.e. in case you use "Build name" setter plugin in Jenkins for getting builds like 2.75.0.1, 2.75.0.2, .. , 2.75.0.15 etc.
// and over the time, change the release/version value (2.75.0) to a newer value i.e. 2.75.1 or 2.76.0 and start builds of this new release/version from #1 onwards.
def releaseVersion = promotedBuild.getDisplayName().split("\.")[0..2].join(".")

println ""
println("- Jenkins Job_Name: ${jobName} -- Version: ${releaseVersion} -- Keep Build Number: ${buildNumber}");
println ""

/** delete the indicated build and its artifacts from artifactory */
def deleteBuildFromArtifactory(String jobName, int deleteBuildNumber, Map<String, String> artifactorySettings){
    println "     ## Deleting >>>>>>>>>: - ${jobName}:${deleteBuildNumber} from artifactory"
                                def artifactSearchUri = "api/build/${jobName}?buildNumbers=${deleteBuildNumber}&artifacts=1"
                                def conn = "${artifactorySettings['artifactoryURL']}/${artifactSearchUri}".toURL().openConnection()
                                conn.setRequestProperty("Authorization", "Basic " + artifactorySettings['authString']);
                                conn.setRequestMethod("DELETE")
    if( conn.responseCode != 200 ) {
        println "Failed to delete the build artifacts from artifactory for ${jobName}/${deleteBuildNumber}: ${conn.responseCode} - ${conn.responseMessage}"
    }
}

/** delete all builds in the indicated range that match the releaseVersion */
def deleteBuildsInRange(String buildRange, String releaseVersion, Job theJob, Map<String, String> artifactorySettings){
    def range = RangeSet.fromString(buildRange, true);
    theJob.getBuilds(range).each {
        if ( it.getDisplayName().find(/${releaseVersion}.*/)) {
            println "     ## Deleting >>>>>>>>>: " + it.getDisplayName();
            deleteBuildFromArtifactory(theJob.name, it.number, artifactorySettings)
            it.delete();
        }
    }
}

//delete all the matching builds before the promoted build number
deleteBuildsInRange("1-${lastBuildNumber}", releaseVersion, jij, artifactorySettings)

//delete all the matching builds after the promoted build number
deleteBuildsInRange("${nextBuildNumber}-${jij.nextBuildNumber}", releaseVersion, jij, artifactorySettings)

println ""
println("- Builds have been successfully deleted for the above mentioned release: ${releaseVersion}")
println "" 

这篇关于在推广/推广步骤中删除詹金斯构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆