我想知道如何在groovy Jenkinsfile中执行多个sh命令,具体取决于在“使用参数输入生成”中输入的参数数量 例如,如果我有这个:
现在我正在使用此代码执行以下sh命令,如果我输入一个URL,它就可以正常工作。:
def urlcf1 = params.URL1
def urlcf2 = params.URL2
node {
stage('purge URL content in cloudflare') {
withCredentials([usernamePassword(credentialsId: 'xxxxxxxxx-xxxxxxxx-yyyyyyyyy', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
sh ("""curl -X POST "https://api.clo.com/client/v4/zones/xxxxxxxxxxxxxxxxxxxxxxx/purge_cache" \
-H "X-Auth-Email: $USER" \
-H "X-Auth-Key: $PASSWORD" \
-H "Content-Type: application/json" \
--data '{"files":["${urlcf1}",{"url":"${urlcf1}","headers":{"Origin":"cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}' """)
}
}
}
但是如果我想拥有多个URL怎么办?使用参数构建“ URL 3”,“ URL 4”,“ URL 5”并根据我输入的URL数量,对该命令分别执行 次这些网址。例如,如果我访问2个URL并保留3个空,则命令仅使用这2个URL执行2次。 也许使用“ while”语句?还是“如果不为空”?我只用一个URL尝试了最后一个,但出现错误。这是代码:
def urlcf1 = params.URL1
def urlcf2 = params.URL2
node {
stage('purge URL content in cloudflare') {
withCredentials([usernamePassword(credentialsId: 'xxxxxxxxxxxxxxxx-xxxxxxxxxxxxx', usernameVariable: 'USER', passwordVariable: 'PASSWORD')]) {
if (urlcf1 =! null) {
sh ("""curl -X POST "https://api.cloudflare.com/client/v4/zones/xxxxxxxxxxxxxxxxxxxxx/purge_cache" \
-H "X-Auth-Email: $USER" \
-H "X-Auth-Key: $PASSWORD" \
-H "Content-Type: application/json" \
--data '{"files":["${urlcf1}",{"url":"${urlcf1}","headers":{"Origin":"cloudflare.com","CF-IPCountry":"US","CF-Device-Type":"desktop"}}]}' """)
else (echo "Value cannot be empty ")
}
}
}
}
并得到: