Jenkinsfile with user input with variable in choices

懵懂的女人 提交于 2020-01-02 09:09:17

问题


I want to use new Jenkinsfile for new job.

I have jenkinsfile wich I have in solo repository:

  1. I get branches from another gitlab repository by git ls-remote in bash. And I store them in variables: branch1, branch2, brach3....
  2. Then I want to use these variables in user input choices

    script {                
      env.BRANCHDEPLOY = input message: 'User input required',
      ok: 'Deploy!',
      parameters: [choice(name: 'Branch to deploy', choices: '${branch1}\n${branch2}\n${branch3}', description: 'What branch you wont deploy?')]
    }
    echo "${env.BRANCHDEPLOY}"
    
  3. Then I will use ${env.BRANCHDEPLOY} for git to deploy selected branch.

Problem is that I can't get to work it with variables in user choice.

I just need to let user choose branch what he wants deploy from another gitlab repository.


回答1:


You are just making the mistake of doing single quotes around your variables which have to be replaced by the script, So just change the single quotes around to double quotes and it works.

"${branch1}\n${branch2}\n${branch3}"

Example: Stage two prints the selected choice

pipeline {
agent any

environment{
    branch1 = 'stack'
    branch2 = 'over'
    branch3 = 'flow'
}

stages {
    stage('Stage-One') {
        steps {
            script {                
                env.BRANCHDEPLOY = input message: 'User input required',
                ok: 'Deploy!',
                parameters: [choice(name: 'Branch to deploy', choices: "${branch1}\n${branch2}\n${branch3}", description: 'What branch you wont deploy?')]
            }
        }
    }
    stage('Stage-Two'){
        steps{
            sh "echo ${BRANCHDEPLOY}"
        }
    }
}

}




回答2:


Did you try something like that, you should be able to pass tha var.

parameters {
    string(branchName: 'feature_x')
}
scm {
    checkout(
      branches: [[name: '*/$branchName']], 
....

 }


来源:https://stackoverflow.com/questions/44389837/jenkinsfile-with-user-input-with-variable-in-choices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!