【问题标题】:Passing credentials from Jenkins credential manager to property file which is present in Jenkins workspace with sed -i使用 sed -i 将凭证从 Jenkins 凭证管理器传递到 Jenkins 工作区中存在的属性文件
【发布时间】:2021-10-16 02:42:23
【问题描述】:
environment {
TEST_CRED = credentials('testcred')
}
stage("Test creds") {
steps {
sh """#!/bin/bash
sed -i '/:TESTCRED:/ c\\:TESTCRED: ${TEST_CRED_USR}'
${WORKSPACE}/test/data/properties.txt
"""}
}
以这种方式发送时,我可以发送凭据,但问题是我收到警告
已检测到的以下步骤可能对敏感变量进行了不安全的插值(点击此处查看说明):
如何避免警告?
我在 sh 之后尝试了三个单引号,但是当我这样做时,我的管道脚本失败了
【问题讨论】:
标签:
jenkins-pipeline
jenkins-groovy
jenkins-cli
【解决方案1】:
一个简短的答案是,去掉 sh 命令中凭据周围的双引号,警告应该消失。然而,
您的代码有两个问题。 1)您实际上是通过直接使用环境变量来滥用凭据。 2)您正在使用双引号字符串(内插字符串)中的凭据。我会推荐以下方法。
pipeline {
stages {
stage( "Test creds" ) {
steps {
script {
withCredentials([usernamePassword(credentialsId: 'testcred', passwordVariable: 'password', usernameVariable: 'username')]) {
sh script: 'sed -i \\\'/:TESTCRED:/ c\\:TESTCRED: ${username}\\\:${password}\\\'' + "${WORKSPACE}/test/data/properties.txt", label: "test credentials"
}
}
}
}
}
}