Jenkins Pipeline 作为 shell 脚本参数传递参数

huangapple go评论62阅读模式
英文:

Jenkins Pipeline passing parameter as shell script argument

问题

我有一个带有默认值的参数化的Jenkins Pipeline,我试图将该参数作为脚本参数传递,但似乎没有传递任何东西。以下是脚本:

pipeline {
   agent any

   stages {
      stage('Building') {
         steps {
            build job: 'myProject', parameters: [string(name: 'configuration', value: '${configuration}')]
         }
      }
      stage('Doing stuff') {
          steps {
            sh "~/scripts/myScript ${configuration}"
          }
      }
   }
}

它似乎对构建步骤起作用,但对于脚本来说不起作用。它返回一个错误,说我没有参数。
我尝试用${configuration}${params.configuration}$configuration来获取它。

正确的方法是如何访问参数并将其正确传递到脚本中?

谢谢。

英文:

I have a parameterized Jenkins Pipeline with a default value and I'm trying to pass that param as a script argument but it doesn't seem to pass anything. Here is the script :

pipeline {
   agent any

   stages {
      stage('Building') {
         steps {
            build job: 'myProject', parameters: [string(name: 'configuration', value: '${configuration}')]
         }
      }
      stage('Doing stuff') {
          steps {
            sh "~/scripts/myScript ${configuration}"
          }
      }
   }
}

It seems to work for the build step but not for the script. I returns an error saying I have no argument.
I tried to get it with ${configuration}, ${params.configuration} and $configuration.

What is the right way to access a param and pass it correctly to a script ?

Thanks.

答案1

得分: 1

以下是翻译好的部分:

实际上,您正在使用构建步骤来传递参数给Jenkins作业 'myProject'。

build job: 'myProject', parameters: [string(name: 'configuration', value: '${configuration}')]

如果您想在此作业中声明一个参数,您需要在 "parameters" 块中声明您的参数。

pipeline {
  agent any

  parameters {
    string(defaultValue: '', description: '', name: 'configuration')
  }

  stages {
    stage('Doing stuff') {
      steps {
        sh "~/scripts/myScript ${configuration}"
      }
    }
  }
}
英文:

Actually, you are using the build step, to pass a parameter to the Jenkins job 'myProject'.

build job: 'myProject', parameters: [string(name: 'configuration', value: '${configuration}')]

If you want to declare a Parameter in this job you need to declare your parameter in a "parameters" block.

pipeline {
  agent any

  parameters {
    string(defaultValue: '', description: '', name: 'configuration')
  }

  stages {
    stage('Doing stuff') {
        steps {
            sh "~/scripts/myScript ${configuration}"
        }
    }
  }
}

huangapple
  • 本文由 发表于 2020年1月6日 19:49:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/59611606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定