Rado Ratko
Rado Ratko

Reputation: 59

want to create single string parameter value extracted from git parameter tag

I have tag versions but I want to pick the latest tag and then let the people have that choice only because I want them to use latest tagged version when running the pipeline. from there I tried two things.
First: Tried to define another param that will take the git parameter and convert it to string eventually and use that value the problem is it fails like:

def version = params.TAG.toString()

parameters {
        gitParameter(name: 'TAG', tagFilter: '2.1*', type: 'PT_TAG', sortMode: 'DESCENDING_SMART', description: '[Mandatory] Which version will be deployed.')
        string(name: 'VERSION', defaultValue: version, description: '[Mandatory] Which version will be deployed.')
...
org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getAt java.lang.Object java.lang.String
14:48:01    at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectStaticMethod(StaticWhitelist.java:243)
14:48:01    at org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SandboxInterceptor.onGetArray(SandboxInterceptor.java:418)

Second approach I not sure how is it possible to set the git parameter somehow filter tags and show me only latest if someone know how?

the problem with the first approach is that I would like to hide the git parameter if I have second param i do not want to params carry same value or something?

Upvotes: 0

Views: 604

Answers (1)

Franco
Franco

Reputation: 193

I've used something like this in an ActiveChoices parameter (example):

list="git ls-remote --tags https://github.com/SAP/jenkins-library"
    .execute()
    .text
    .split()
    .toList()
    .findAll{
      it.startsWith("refs/tags/")
    }
    .findAll{
      it.contains("v")
    }
.collect{
  it.replace("refs/tags/", "")
  .replace("^{}","")
}

def mostRecentVersion(versions){
    return versions.collectEntries{ 
        [(it=~/\d+|\D+/).findAll().collect{it.padLeft(3,'0')}.join(),it]
    }.sort().values()[-1]
}

return [mostRecentVersion(list)]

This will return the latest Version based on the answer here as the single choice directly read from a git repository.

enter image description here

Upvotes: 0

Related Questions