Dynamically Fill Jenkins Choice Parameter With Git Branches In a Specified Repo

后端 未结 13 909
离开以前
离开以前 2020-11-29 16:41

I have a parameterized Jenkins job which requires the input of a specific Git branch in a specific Git repo. Currently this parameter is a string parameter.

Is ther

13条回答
  •  [愿得一人]
    2020-11-29 17:37

    Yes, I wrote a little groovy script which does the trick You should add a 'Dynamic Choice Parameter' to your job and customize the following groovy script to your needs :

    #!/usr/bin/groovy
    
    def gitURL = "git repo url"
    def command = "git ls-remote --heads --tags ${gitURL}"
    
    def proc = command.execute()
    proc.waitFor()              
    
    if ( proc.exitValue() != 0 ) {
       println "Error, ${proc.err.text}"
       System.exit(-1)
    }
    
    def text = proc.in.text
    # put your version string match
    def match = //
    def tags = []
    
    text.eachMatch(match) { tags.push(it[1]) }
    tags.unique()
    tags.sort( { a, b ->
             def a1 = a.tokenize('._-')
             def b1 = b.tokenize('._-')
             try {
                for (i in 1..<[a1.size(), b1.size()].min()) { 
                     if (a1[i].toInteger() != b1[i].toInteger()) return a1[i].toInteger() <=> b1[i].toInteger()
                }
                return 1
             } catch (e) {
                return -1;
             }
    } )
    tags.reverse()
    

    I my case the version string was in the following format X.X.X.X and could have user branches in the format X.X.X-username ,etc... So I had to write my own sort function. This was my first groovy script so if there are better ways of doing thing I would like to know.

提交回复
热议问题