Jenkins 构建失败,因为变量没有值
Jenkins build failed because of an no value to a variable
我有一个如下所示的 Jenkins 文件:
stage("Deploy artifact for k8s sync")
{
sh '''
ns_exists=`kubectl get ns | grep ${target_cluster}`
if [ -z "$ns_exists" ]
then
echo "No namespace ${consider_namespace} exists in the cluster ${target_cluster}"
echo "Creating namespace ${consider_namespace} in the cluster ${target_cluster}"
kubectl apply "some yaml file"
else
if [ "${consider_kinds}" = "all" ] || [ "${consider_kinds}" = "serviceaccounts" ]
then
echo "Applying source serviceaccounts on target cluster ${target_cluster}"
kubectl "some yaml file"
fi
if [ "${consider_kinds}" = "all" ] || [ "${consider_kinds}" = "secrets" ]
then
echo "Applying source secrets on target cluster ${target_cluster}"
kubectl "some yaml file"
fi
if [ "${consider_kinds}" = "all" ] || [ "${consider_kinds}" = "configmaps" ]
then
echo "Applying source configmaps on target cluster ${target_cluster}"
kubectl apply -f ${BUILD_NUMBER}-${source_cluster}-${consider_namespace}-configmaps.yaml
fi
但是,当我 运行 时,它失败并出现如下错误:
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy artefact for k8s sync) (hide)
[Pipeline] sh
+ kubectl get ns
+ grep test-central-eks
+ ns_exists=
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
想知道如何解决它以及为什么它首先会失败?
据我所知,有几件事导致此失败。
从你的管道提取物中我可以看到你正在使用脚本中未定义的变量,所以我猜这些是某种环境变量或来自上一步。为了能够使用它,您需要进行字符串插值,在您的情况下,需要一个三重双引号 https://groovy-lang.org/syntax.html#_triple_double_quoted_string
stage("Deploy artifact for k8s sync")
{
sh """
另一方面,当不匹配时,grep 退出代码为 1,如果您需要在不匹配的情况下继续,您可以放置一个条件为真的块命令
ns_exists=`kubectl get ns | {grep ${target_cluster} || true; }`
最后,解决此问题的一个好方法是通过在 sh sh
块的开头添加 set -x
来在 shell 块中使用调试重播管道。
另请注意,不会打印 stderr,如果您需要它,您需要重定向到 stdout 并从 jenkins sh
的 returnStdout: true
选项或自定义文件中读取标准输出被重定向
我有一个如下所示的 Jenkins 文件:
stage("Deploy artifact for k8s sync")
{
sh '''
ns_exists=`kubectl get ns | grep ${target_cluster}`
if [ -z "$ns_exists" ]
then
echo "No namespace ${consider_namespace} exists in the cluster ${target_cluster}"
echo "Creating namespace ${consider_namespace} in the cluster ${target_cluster}"
kubectl apply "some yaml file"
else
if [ "${consider_kinds}" = "all" ] || [ "${consider_kinds}" = "serviceaccounts" ]
then
echo "Applying source serviceaccounts on target cluster ${target_cluster}"
kubectl "some yaml file"
fi
if [ "${consider_kinds}" = "all" ] || [ "${consider_kinds}" = "secrets" ]
then
echo "Applying source secrets on target cluster ${target_cluster}"
kubectl "some yaml file"
fi
if [ "${consider_kinds}" = "all" ] || [ "${consider_kinds}" = "configmaps" ]
then
echo "Applying source configmaps on target cluster ${target_cluster}"
kubectl apply -f ${BUILD_NUMBER}-${source_cluster}-${consider_namespace}-configmaps.yaml
fi
但是,当我 运行 时,它失败并出现如下错误:
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Deploy artefact for k8s sync) (hide)
[Pipeline] sh
+ kubectl get ns
+ grep test-central-eks
+ ns_exists=
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE
想知道如何解决它以及为什么它首先会失败?
据我所知,有几件事导致此失败。
从你的管道提取物中我可以看到你正在使用脚本中未定义的变量,所以我猜这些是某种环境变量或来自上一步。为了能够使用它,您需要进行字符串插值,在您的情况下,需要一个三重双引号 https://groovy-lang.org/syntax.html#_triple_double_quoted_string
stage("Deploy artifact for k8s sync")
{
sh """
另一方面,当不匹配时,grep 退出代码为 1,如果您需要在不匹配的情况下继续,您可以放置一个条件为真的块命令
ns_exists=`kubectl get ns | {grep ${target_cluster} || true; }`
最后,解决此问题的一个好方法是通过在 sh sh
块的开头添加 set -x
来在 shell 块中使用调试重播管道。
另请注意,不会打印 stderr,如果您需要它,您需要重定向到 stdout 并从 jenkins sh
的 returnStdout: true
选项或自定义文件中读取标准输出被重定向