Jenkins :版本1.525 Jenkins服务器URL :http ://my.jenkins.server.com:9040 Linux Red Hat 5.3
Artifactory :免费版本 Artifactory服务器网址 :http://my.artifactory.server:8081 / Artifactory
我可以在Jenkins中成功构建并将工件上传到各自存储库下的Artifactory服务器。
发生构建时,工件(ProjectA-1.0.0.25.tar.gz)进入libs-snapshot- local存储库下的Artifactory。这里的1.0.0表示应用程序/詹金斯工作的给定发行版的主要,次要和临时版本:在这种情况下为“ ProjectA”。可以说25是内部版本号
当ProjectA内部版本在Development中稳定时,我们会将该应用程序版本的给定内部版本升级到INT或任何其他更高环境(QA / PrePROD等)。
在此升级过程中,我们只需选择要升级的构建并使用Jenkins Promoted Build Plugin,我们就能成功完成。
现在,我们需要的是:
我们公司的想法是,一旦为应用程序升级了发行版的内部版本,我们拥有的所有其他内部版本/构件(在Jenkins / Artifactory中)-我们希望使用Groovy脚本永久删除。有人会问,如果我要升级其他版本,该怎么办?在我们的情况下,我们不想要那样。一个简单的规则是,如果有人升级ProjectA-1.0.0.25.tar.gz,则在Jenkins和Artifactory中删除build / artifact不是1.0.0.25的ProjectA的build /工件,并继续使用新发行版1.1.0
具有以下功能的脚本会很棒。 1.使用属性文件(jenkins.properties/artifactory.properties)-其中将包含一些有关主机名/用户名/密码等的变量。 2.使用REST API对给定的应用程序/作业和给定的版本执行删除操作(针对ex 1.0.0) 3.可以同时用于Jenkins / Artifactory删除-如果在命令提示符下,则使用此(Jenkins)属性文件-或(人工的)文件- 在两种情况下,应用程序及其发布值都相同。 4.我们知道,为了将构建升级为INT(使用Jenkins升级插件),我们将始终仅在libs-snapshot- release中从Jenkins服务器和Artifactory服务器中删除。 现在,如果有人(稍后)晋升为质量检查人员,那么人工仓库将是(libs-stage-local)
换句话说,我们应该调用Groovy脚本,传递一些变量/值(REST),并告知要删除的应用程序/作业以及它的构建发行版。然后,它将删除除用户将通过的版本以外的所有版本(即1.0.0.25)
我对Groovy都是新手,使用REST API为Jenkins / Artifactory进行了这一“删除”工作。如果某人已经具有执行这种活动的任何示例脚本,并且可以共享,那么我将根据自己的设置对其进行调整,看看是否可以在升级步骤中看到上述行为。我在获取该脚本的工作版本方面有些时间紧迫,不胜感激一些脚本代码可以完成相同的任务(而不是让别人告诉我要阅读大量的文档/链接,我知道那会让我成为Groovy中更好的编码员但这会延迟整个目的。
非常感谢。
最终答案 :这包括使用Artifactor的REST API调用从Artifactory中删除构建工件。此脚本将删除给定发行版/版本的Jenkins / Artifactory版本/工件(有时会随着时间的推移- 给定的Jenkins作业可以创建多个发行版/版本版本,例如:2.70.5.0.1、2.70.5.0.2、2.70.5.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个全新的版本开始构建。如果您必须删除除一个(甚至所有)以外的所有版本(请根据自己的需要稍微更改脚本)并且不更改较早/其他版本的版本,请使用以下脚本。
脚本编辑器目录链接 :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 ""