如何使用下游作业修复Jenkins字符串插值?

发布于 2025-01-30 03:23:49 字数 2349 浏览 2 评论 0 原文

我有一个父管路作业,可以采用参数,并将其传递到下游作业。我以多种方式实现了这一点,但是我一直在插入插值警告,即我要修复,但无法做到这一点。基于文档()应该有效,但是,这将传递给文字名称而不是值(例如,如果我将变量设置为secret_pwd,则将其称为' $ {secret_pwd}'它在下游作业上显示为$ {secret_pwd},而不是传递给参数

pipeline {
  agent any 
  parameters {
  password(defaultValue: "", description: 'The admin password', name: 'SUPER_SECRET_ADMIN_PWD')
  stages {
    stage("Stage1") {
      steps {
        build job: "secret_job/${env.BRANCH}", propagate: true, wait: true, parameters: [
          [$class: 'StringParameterValue', name: 'SUPER_SECRET_ADMIN_PWD',    value: "${params.SUPER_SECRET_ADMIN_PWD}" ]
        ]
      }
    }

值 在调用下游作业时

Warning: A secret was passed to "build" using Groovy String interpolation, which is insecure.
         Affected argument(s) used the following variable(s): [SUPER_SECRET_ADMIN_PWD]
         See https://jenkins.io/redirect/groovy-string-interpolation for details.
The parameter 'SUPER_SECRET_ADMIN_PWD' did not have the type expected by secret_job » secret_branch. Converting to Password Parameter.

我尝试过的其他方法是:

password(name: 'SUPER_SECRET_ADMIN_PWD', value: "${SUPER_SECRET_ADMIN_PWD}") = This works but still interpolation issue
password(name: 'SUPER_SECRET_ADMIN_PWD', value: "${SUPER_SECRET_ADMIN_PWD}") = This does NOT work as it passes ${SUPER_SECRET_ADMIN_PWD} as the value rather than the one entered into the parameter. HOWEVER the interpolation warning goes away
[$class: 'StringParameterValue', name: 'SUPER_SECRET_ADMIN_PWD',    value: '${params.SUPER_SECRET_ADMIN_PWD}' = This does NOT work as it passes ${SUPER_SECRET_ADMIN_PWD} as the value rather than the one entered into the parameter. HOWEVER the interpolation warning goes away

我还使用了$ {env.super_secret_admin_pwd}类似于$ {params.super_secret_admin_pwd}

请注意,我已经更改了下游作业来使用单个报价,我正在做一个简单的SH脚本如下所示,没有插值错误(虽然我以前有)。

stages{
  stage("test"){
    steps{
     script{
        sh '''
        echo ${SUPER_SECRET_ADMIN_PWD}
        '''
     }
   }
 }

如何解决插值并将密码参数传递给下游作业?

I have a parent pipeline job that takes parameters and passes them to a downstream job. I've achieves this in multiple ways with no issue however I keep getting a string interpolation warning that I am trying to fix, but am unable to do so. Based on the documentation (https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#string-interpolation) in most cases using single quotes should work, however this passes the literal name rather than the value (e.g if I set my variable as SECRET_PWD and call it like '${SECRET_PWD}' it shows up as ${SECRET_PWD} on the downstream job instead of the value passed to the parameter.

Here's what I've tried to so far:

Parent Pipeline

pipeline {
  agent any 
  parameters {
  password(defaultValue: "", description: 'The admin password', name: 'SUPER_SECRET_ADMIN_PWD')
  stages {
    stage("Stage1") {
      steps {
        build job: "secret_job/${env.BRANCH}", propagate: true, wait: true, parameters: [
          [$class: 'StringParameterValue', name: 'SUPER_SECRET_ADMIN_PWD',    value: "${params.SUPER_SECRET_ADMIN_PWD}" ]
        ]
      }
    }

This gives the following error in when calling the downstream job.

Warning: A secret was passed to "build" using Groovy String interpolation, which is insecure.
         Affected argument(s) used the following variable(s): [SUPER_SECRET_ADMIN_PWD]
         See https://jenkins.io/redirect/groovy-string-interpolation for details.
The parameter 'SUPER_SECRET_ADMIN_PWD' did not have the type expected by secret_job » secret_branch. Converting to Password Parameter.

Note: I am aware that the StringParameterValue is the reason for the first error. I have changed this in a few different ways to solve that but i still get the interpolation issue.

The other ways I've tried are:

password(name: 'SUPER_SECRET_ADMIN_PWD', value: "${SUPER_SECRET_ADMIN_PWD}") = This works but still interpolation issue
password(name: 'SUPER_SECRET_ADMIN_PWD', value: "${SUPER_SECRET_ADMIN_PWD}") = This does NOT work as it passes ${SUPER_SECRET_ADMIN_PWD} as the value rather than the one entered into the parameter. HOWEVER the interpolation warning goes away
[$class: 'StringParameterValue', name: 'SUPER_SECRET_ADMIN_PWD',    value: '${params.SUPER_SECRET_ADMIN_PWD}' = This does NOT work as it passes ${SUPER_SECRET_ADMIN_PWD} as the value rather than the one entered into the parameter. HOWEVER the interpolation warning goes away

I've also used ${env.SUPER_SECRET_ADMIN_PWD} similar to ${params.SUPER_SECRET_ADMIN_PWD}

Note that I've changed my downstream job to use single quotes and i'm doing a simple sh script something like below with no interpolation errors (I have them before though).

stages{
  stage("test"){
    steps{
     script{
        sh '''
        echo ${SUPER_SECRET_ADMIN_PWD}
        '''
     }
   }
 }

How do I go about solving interpolation and still passing the password parameter down to a downstream job?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文