问题
I want to use new Jenkinsfile for new job.
I have jenkinsfile wich I have in solo repository:
- I get branches from another gitlab repository by git ls-remote in bash. And I store them in variables: branch1, branch2, brach3....
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}"
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