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

后端 未结 13 937
离开以前
离开以前 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:38

    For Me I use the input stage param:

    1. I start my pipeline by checking out the git project.
    2. I use a awk commade to generate a barnch.txt file with list of all branches
    3. In stage setps, i read the file and use it to generate a input choice params

    When a user launch a pipeline, this one will be waiting him to choose on the list choice.

    pipeline{
    agent any
    
    stages{
    
        stage('checkout scm') {
            steps {
                script{
                    git credentialsId: '8bd8-419d-8af0-30960441fcd7', url: 'ssh://jenkins@git.company.com:/usr/company/repositories/repo.git'
                    sh 'git branch -r | awk \'{print $1}\' ORS=\'\\n\' >>branch.txt'
                }
    
            }
        }
         stage('get build Params User Input') {
            steps{
                script{
    
                    liste = readFile 'branch.txt'
                    echo "please click on the link here to chose the branch to build"
                    env.BRANCH_SCOPE = input message: 'Please choose the branch to build ', ok: 'Validate!',
                            parameters: [choice(name: 'BRANCH_NAME', choices: "${liste}", description: 'Branch to build?')]
    
    
                }
            }
        } 
        stage("checkout the branch"){
            steps{
                echo "${env.BRANCH_SCOPE}"
                git  credentialsId: 'ea346a50-8bd8-419d-8af0-30960441fcd7', url: 'ssh://jenkins@git.company.com/usr/company/repositories/repo.git'
                sh "git checkout -b build ${env.BRANCH_NAME}"
            }
        }
        stage(" exec maven build"){
            steps{
                withMaven(maven: 'M3', mavenSettingsConfig: 'mvn-setting-xml') {
                   sh "mvn clean install "
                }
            }
        }
        stage("clean workwpace"){
            steps{
                cleanWs()
            }
        }
    }
    

    }

    And then user will interact withim the build :

    enter image description here

    enter image description here

提交回复
热议问题