【问题标题】:Passing parameters down a Jenkins pipeline将参数传递到 Jenkins 管道
【发布时间】:2019-12-19 04:46:38
【问题描述】:

我无法让 Jenkins 将字符串参数传递到管道中。

当我运行管道时,我输入了$ServiceName 的字符串值,作业继续,但它没有将此参数传递给管道中的第一个作业(NEWSERVICE - 添加新)。在“构建”阶段的詹金斯文件中,我尝试了params.ServiceName$params.ServiceNameenv.ServiceName$env.ServiceName$env:ServiceName。没有运气。

我需要将参数传递给 NEWSERVICE 作业中的 Powershell 构建过程(目前只是用 $env:ServiceName 呼应参数 - 但它总是空的)任何帮助将不胜感激。

pipeline {
    agent any   
    parameters{
        string(name:  'ServiceName',
            defaultValue: '',
            description: '',)
    }
    stages {                

        stage('Add new Service'){
            steps {

                build(job: "NEWSERVICE - Add New", parameters: [string(name: 'ServiceName', value: params.ServiceName)])

                }
            }
    }
}

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    如果您希望在 powershell 脚本中使用 Jenkins env var - 我一直都这样做! - 必须首先在 Jenkins 中设置环境变量。换一种说法, Jenkins 上下文中的 env.myVar 在 PowerShell 上下文中将显示为 $env:myVar。但是您需要将其设置为 env var,local Jenkins 变量对子脚本不可见(除非作为参数传入)。我在这里有详细的记录:https://stackoverflow.com/a/62538778/556078

    【讨论】:

      【解决方案2】:

      在管道中你需要像这样传递字符串参数:

              parameters: [
                  [$class: 'StringParameterValue', name: 'ServiceName', value: ServiceName]
              ], 
      

      请参阅此内容以了解在从 Jenkinsfile 调用作业时传递的不同类型的变量。 https://jenkins.io/doc/pipeline/steps/pipeline-build-step/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-01-11
        • 1970-01-01
        • 1970-01-01
        • 2020-06-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多