I want to use new Jenkinsfile for new job.
I have jenkinsfile which I have in a solo repository:
I get branches from another GitLab repository by git ls-remote in bash. And I store them in variables: branch1, branch2, branch3....
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 the user choose the branch that they want deployed from another GitLab repository.
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}"
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With