使用Bash从Jenkins管道创建.tfvars文件



我想创建"。tfvars"动态文件使用Bash脚本与詹金斯参数作为参数,这是我所做的已经:

Jenkins管道文件

pipeline {
agent { label "${params.environment}_slave" }

parameters {
string(name: 'branch', defaultValue:"main")
choice(name: 'environment',choices: ['nonprod','prod'],description:'Describe where you want this pipeline to run')
booleanParam(name: 'bool', defaultValue:"false")
string(name: 'string', defaultValue:"value")
text(name: 'blabla', defaultValue:'''testtest-apinmlflow''')
string(name: 'int', defaultValue:"1234")
}
environment {
SCM_URL = "https://my_git/my_repo"
}

stages {
stage("Test if prerequisites have been executed") {
steps {
git branch: "$params.branch" ,url: "${SCM_URL}"
sh "chmod +x -R ${env.WORKSPACE}"
sh "./script.sh "${params}""
}
}
}
}
Bash脚本:
params=$1
modified=${params:1:-1}    
res=$(echo $modified | sed 's/:/=/g')

while IFS='=' read -r key value; do
array["$key"]="$value"
done <<< "$res"

for key in "${!array[@]}"; do
echo "$key=${array[$key]}" >> terraforms.tfvars;
done
printf "terraforms.tfvars ======== n"
cat terraforms.tfvars

,当我在Jenkins中运行所有内容时,结果如下:

+ chmod +x -R /home/jenkins/workspace/my_repo
[Pipeline] sh
+ ./script.sh '[environment:nonprod, bool:false, string:value, blabla:test
test-api
mlflow, branch:main, int:1234]'
terraforms.tfvars ======== 
0=nonprod, bool=false, string=value, blabla=test test-api mlflow, branch=main, int=1234

我不明白为什么我有0=nonprod而不是environment=nonprod

有什么想法吗?或者对整件事的建议?

thank you very much

我有0=nonprod,因为我没有首先实例化数组:

declare -A array

相关内容

  • 没有找到相关文章

最新更新