多个 Jenkins 参数化构建值将值传递给 Jenkinsfile 用于声明性管道

Multiple Jenkins parameterized build value pass value to Jenkinsfile for declarative pipeline

您好,我的 Jenkins 项目是参数化构建的。我有 3 个变量。 1 个选择和 2 个字符串参数。选择参数是do_you_want_to_deploy,字符串参数是git_tag和git_branch。我想知道如何将此值传递给 jenkinsfile?

在freestyle项目中,我选择了'Extra Variables'然后得到了Key和Value。所以我输入 deploy_location 的键,值为 ${do_you_want_to_deplo}。键为 which_tag,值为 ${git_tag}。键为 which_ranch,值为 ${git_branch}。我正在为ansible表演。我怎样才能添加 verbos -vvv 呢?这用于流水线项目。下面是我的代码

                ansiblePlaybook(
                    vaultCredentialsId: 'VaultId',
                    inventory: 'host-inventory.yml',
                    playbook: 'myPlaybook.yml'
                )
``

I also need pass same value to downstream project. How can this be done?

Hi my Jenkins project is parameterised build. I have 3 variables. 1 choice and 2 string parameter. The choise perameter is do_you_want_to_deploy and string parameter is git_tag and git_branch. I want to know how can i pass this value to a jenkinsfile?

在 Jenkinsfile 中有 parameters block 定义变量。根据您的用例,参数定义可能如下所示。在这里,根据您的解释选择,我假设您需要一个切换,但如果您需要一个项目列表,请使用 choice 参数类型。

pipeline {
...
  parameters {
    booleanParam(name: 'do_you_want_to_deploy', defaultValue: false, description: 'Description of do_you_want_to_deploy')
    string(name: 'git_tag', defaultValue: '', description: 'Description of git_tag')
    string(name: 'git_branch', defaultValue: '', description: 'Description of git_branch')
  }
  stages {
    stage('Example') {
      steps {
        ansiblePlaybook(
          ...
        )
      }
    }
  }
}

In freestyle project, I selecft 'Extra Variables' and then got Key and Value. So key i put deploy_location, value is ${do_you_want_to_deplo}. Key is which_tag, value is ${git_tag}. Key is which_ranch, value is ${git_branch}. I am performing for ansible. How can i add verbos -vvv as well?

Ansible 插件有一个选项 extraVars,可用于从管道传递多个变量。还有另一个名为 extras 的选项,它接受一个字符串,可用于传递其他变量、开关等。

加在一起,ansiblePlaybook可能如下所示,

ansiblePlaybook (
  vaultCredentialsId: 'VaultId',
  inventory: 'host-inventory.yml',
  playbook: 'myPlaybook.yml',
  extras: '-vvv',
  extraVars: [
    deploy_location: params.do_you_want_to_deploy,
    which_tag: params.git_tag,
    which_branch: params.git_branch
  ]
)

I also need pass same value to downstream project. How can this be done?

从上面ansiblePlaybook的例子可以看出,可以通过params对象访问参数。