带有用户输入的Jenkinsfile,选项中包含变量 [英] Jenkinsfile with user input with variable in choices

查看:612
本文介绍了带有用户输入的Jenkinsfile,选项中包含变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为新工作使用新的Jenkinsfile.

I want to use new Jenkinsfile for new job.

我在单独的存储库中有jenkinsfile:

I have jenkinsfile wich I have in solo repository:

  1. 我在bash中通过git ls-remote从另一个gitlab存储库中获得了分支.然后将它们存储在变量中:branch1,branch2,brach3....
  2. 然后我要在用户输入选择中使用这些变量

  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}"

  • 然后我将使用${env.BRANCHDEPLOY} for git部署所选分支.

  • 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.

    我只需要让用户从另一个gitlab存储库中选择要部署的分支即可.

    I just need to let user choose branch what he wants deploy 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}"
            }
        }
    }
    

    }

    这篇关于带有用户输入的Jenkinsfile,选项中包含变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆