jenkinsfile if 语句不使用通配符语句
jenkinsfile if statement not using wildcard statement
我试图根据 jenkinsfile 中的以下 if 语句计算出正确的语法,但是它没有按预期传递通配符并尝试匹配包括通配符符号的语法:
stage('stage c') {
steps {
container('tf-jenkins') {
script {
sh """
if [[ $(gcloud container clusters list --format='value(name)' --project ${project_id} --filter=d-kcl-${short_region} ) = *d-kcl* ]]; then
echo "cluster already exists, progressing to application deployment"
else
echo "no cluster found, a new cluster needs to be deployed"
fi
"""
}
}
使用上面的阶段块,返回以下内容:
+ gcloud container clusters list '--format=value(name)' --project prpject-id '--filter=d-kcl'
+ '[[' paas-gcp-d-kcl-euwe2 '=' '*paas-gcp-d-kcl*' ]]
+ echo 'no cluster found, a new cluster needs to be deployed'
no cluster found, a new cluster needs to be deployed
当 运行 在正常 shell 中时,这按预期工作,但在 jenkinsfile 中没有。
任何帮助将不胜感激
我建议使用 groovy 代码而不是 shell 脚本。
stage('stage c') {
steps {
container('tf-jenkins') {
script {
def commandOutput = sh (
script: "gcloud container clusters list --format='value(name)' --project ${project_id} --filter=d-kcl-${short_region}",
returnStdout: true,
label: "Check if container exists"
).trim()
if(commandOutput.contains("d-kcl")){
echo "cluster already exists, progressing to application deployment"
} else{
echo "no cluster found, a new cluster needs to be deployed"
}
}
}
}
}
我试图根据 jenkinsfile 中的以下 if 语句计算出正确的语法,但是它没有按预期传递通配符并尝试匹配包括通配符符号的语法:
stage('stage c') {
steps {
container('tf-jenkins') {
script {
sh """
if [[ $(gcloud container clusters list --format='value(name)' --project ${project_id} --filter=d-kcl-${short_region} ) = *d-kcl* ]]; then
echo "cluster already exists, progressing to application deployment"
else
echo "no cluster found, a new cluster needs to be deployed"
fi
"""
}
}
使用上面的阶段块,返回以下内容:
+ gcloud container clusters list '--format=value(name)' --project prpject-id '--filter=d-kcl'
+ '[[' paas-gcp-d-kcl-euwe2 '=' '*paas-gcp-d-kcl*' ]]
+ echo 'no cluster found, a new cluster needs to be deployed'
no cluster found, a new cluster needs to be deployed
当 运行 在正常 shell 中时,这按预期工作,但在 jenkinsfile 中没有。
任何帮助将不胜感激
我建议使用 groovy 代码而不是 shell 脚本。
stage('stage c') {
steps {
container('tf-jenkins') {
script {
def commandOutput = sh (
script: "gcloud container clusters list --format='value(name)' --project ${project_id} --filter=d-kcl-${short_region}",
returnStdout: true,
label: "Check if container exists"
).trim()
if(commandOutput.contains("d-kcl")){
echo "cluster already exists, progressing to application deployment"
} else{
echo "no cluster found, a new cluster needs to be deployed"
}
}
}
}
}