Groovy script for Promotion of Artifactory Builds

Jenkins Scriptler Plugin - Groovy script for Promotion of Artifactory Builds

Jenkins has a very cool plugin: the Scriptler Plugin. Allowing you to write a Groovy script that you can later re-use as a Build Step

So, first step of course would be to install the Plugin …

Once installed you will see on the main Jenkins menu a new item: Scriptler

In the Remote Script catalogs you can find many good examples for scripts

Our recent need was for a script that will access the Artifactory Pro REST API and promote a builds artifacts to libs-release-local

Add a new Script

Press Add a new Script

Id should be a unique name to your Jenkins deployment e.g. scriptlerweb.70011.scriptlerweb.groovy

Name the name of the file, e.g. buildPromotewithArtifactoryRestAPI.groovy

Permission fill in as you see fit

Restriction check if you want the script to run on Master only

Define script parameters to be used later on when adding the Build Step

In our example the script has 5 parameters:

  • artifactoryURL
  • artifactoryUser
  • artifactoryPassword

Script

    import groovy.xml.*
    import groovy.json.*
    import jenkins.model.*;
    import hudson.model.Fingerprint.RangeSet;
    import hudson.model.Job;
    import hudson.model.Fingerprint
    
    def env = System.getenv()
    
    String jobName = (env['ORIG_JOB_NAME'] ?: "NOJOB")
    String buildNumber = (env['ORIG_BUILD_NUMBER'] ?: 0)
            
    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 promoteBuildArtifactstoRelease(String jobName, int buildNumber, Map<String, String> artifactorySettings){
    
        def artifactPromoteUri = "api/build/promote/${jobName}/${buildNumber.toString()}"
    
        def urlString = "${artifactorySettings['artifactoryURL']}/${artifactPromoteUri}"
    
        def query = '{"status":"tests passed","targetRepo":"libs-release-local"}'
    
    
        def url = new URL(urlString)
        def connection = url.openConnection()
        connection.setRequestMethod("POST")
    
        def basicAuth = "${artifactoryUser}:${artifactoryPassword}".getBytes().encodeBase64().toString()
    
        connection.setRequestProperty("Authorization", "Basic ${basicAuth}")
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setRequestProperty("Accept", "application/json");
    
        connection.doOutput = true
    
        def writer = new OutputStreamWriter(connection.outputStream)
        writer.write(query)
        writer.flush()
        writer.close()
        connection.connect()
    
        println connection.responseCode
        headerFields = connection.getHeaderFields()
        headerFields.each {println it}
        assert connection.responseCode == 200
    
    }
    
    promoteBuildArtifactstoRelease(jobName, buildNumber.toInteger(), artifactorySettings)

Submit

Using the Script

New or Configure Job

Add build step Scriptler script

Script choose your script from the pull down menu

Propagate Job params to script if you want to use the Job prameters in the script In this case, ORIG_JOB_NAME and ORIG_BUILD_NUMBER that are propagated from the STAGING build will be used in the script

Define script parameters in our example:

  • Name: artifactoryURL Value: http://artifactory.company.com
  • Name: artifactoryUser Value: jenkins
  • Name: artifactoryPassword Value: pa55wd
  • Name: jobName Value: $ORIG_JOB_NAME
  • Name: buildNumber Value: $ORIG_BUILD_NUMBER
Senior DevOps Engineer

DevOps Group
Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com