gpt4 book ai didi

linux - 在 jenkins 管道中用 envsubst 替换文件变量

转载 作者:太空宇宙 更新时间:2023-11-04 11:56:18 25 4
gpt4 key购买 nike

我想在运行时从 jenkins 管道脚本替换具有 $variablename 的文件中的一些变量。似乎 envsubst 最适合我的用例。当我在 linux 服务器上通过命令行执行时它工作正常但是当我通过 sh 脚本中的 jenkins 管道执行时,没有任何反应。

声纳扫描仪.properties:

sonar.projectKey=项目:MavenTest$BRANCHNAMEsonar.projectName=MavenTest$BRANCHNAME

linux box 命令行示例:

$ export BRANCHNAME=develop

$ envsubst '$BRANCHNAME'

输出:

sonar.projectKey=Project:MavenTestdevelop

sonar.projectName=MavenTestdevelop

但是当我通过 jenkins 文件作为脚本执行时,文件中没有任何更改。

Jenkins 脚本:

 node {

stage('checkout'){

checkout([$class: 'GitSCM', branches: [[name: ':^(?!origin/master$|origin/develop$).*']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'c0ce73db-3864-4360-9c17-d87caf8a9ea5', url: 'http://172.16.4.158:17990/scm/ctoo/testmaven.git']]])

}

stage('initialize variables'){
// Configuring BRANCH_NAME variable
sh 'git name-rev --name-only HEAD > GIT_BRANCH'
sh label: '', script: 'cut -d \'/\' -f 3 GIT_BRANCH > BRANCH'
branchname = readFile('BRANCH').trim()
env.BRANCHNAME = branchname
}
stage('build & SonarQube analysis') {
withSonarQubeEnv('Sonar') {
sh "envsubst '$BRANCHNAME' <sonar-scanner.properties"
}
}
}

输出:[管道] sh(隐藏)

  • envsubst 重新测试

sonar.projectKey=Project:MavenTest$BRANCHNAME

sonar.projectName=MavenTest$BRANCHNAME

谁能帮帮我

最佳答案

嗨,我不知道 envbust,但这可以通过命令行将声纳参数传递给声纳来实现,请参见下面的示例:

withSonarQubeEnv('Sonar') {
sh "<sonarscanner path> -Dsonar.projectKey=Project:MavenTest$BRANCHNAME"
}

关于linux - 在 jenkins 管道中用 envsubst 替换文件变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54234973/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com