Gitlab CI/CD 创建和使用自定义用户函数
Gitlab CI/CD create and use custom user functions
我尝试在我的 gitlab 配置文件中创建这样的函数:
deploy:
stage: dev
services:
- docker:dind
script:
- myFunction () { api_pl_tmp=$(curl -s --header "PRIVATE-TOKEN: $TOKEN_VAR" "https://git.example.ru/api/v4/projects/1/pipelines/latest" | jq .) }
- while myFunction; do
- if [ $(echo $api_pl_tmp | jq -r .status) = "success" ]
- then
- export PROJECT_CURRENT=$($api_pl_tmp | jq -r '{id:.id,sha:.sha[0:8]}' | base64)
- break
- fi
- if [ $(echo $api_pl_tmp | jq -r .status) = "failed" ]
- then
- echo "Error: Frontend can't be deployed!"
- exit 1
- fi
- if [ $(echo $api_pl_tmp | jq -r .status) = "running" ]
- then
- echo "Wait 5 sec... Frontend deploying!"
- sleep 5
- else
- echo Unknow status $(echo $api_pl_tmp | jq -r .status)
- exit 1
- fi
- done
但它不起作用,gitlab return 我的错误消息:
This GitLab CI configuration is invalid: jobs:deploy-to-dev:script
config should be a string or a nested array of strings up to 10 levels
deep
我该如何解决这个问题或者我的自定义函数有错误?
-
将命令与中间的命令分开。将您的命令作为一个命令,而不是多个。请记住它们是用空格连接的。
您的脚本的问题在于冒号 - 请参阅 https://gitlab.com/gitlab-org/gitlab-foss/-/issues/30097。
deploy:
stage: dev
services:
- docker:dind
script:
- "colon=:"
- myFunction () {
api_pl_tmp=$(curl -s --header "PRIVATE-TOKEN$colon $TOKEN_VAR" \
"https$colon//git.example.ru/api/v4/projects/1/pipelines/latest" | jq .);
}
- while myFunction; do
if [ $(echo $api_pl_tmp | jq -r .status) = "success" ]; then
export PROJECT_CURRENT=$($api_pl_tmp | jq -r '{id:.id,sha:.sha[0:8]}' | base64);
break;
fi;
if [ $(echo $api_pl_tmp | jq -r .status) = "failed" ]; then
echo "Error$colon Frontend cant be deployed";
exit 1;
fi;
if [ $(echo $api_pl_tmp | jq -r .status) = "running" ]; then
echo "Wait 5 sec... Frontend deploying!";
sleep 5;
else
echo Unknow status $(echo $api_pl_tmp | jq -r .status);
exit 1;
fi;
done
另外 jq .) }
还缺少一个 ;
。首先检查您自己 shell 中的脚本。使用 https://shellcheck.net 检查您的脚本。
另外 $($api_pl_tmp
缺少回显,而且引用有 很多 问题。使用一致的缩进并尝试编写可读代码以尽量减少拼写错误。
我尝试在我的 gitlab 配置文件中创建这样的函数:
deploy:
stage: dev
services:
- docker:dind
script:
- myFunction () { api_pl_tmp=$(curl -s --header "PRIVATE-TOKEN: $TOKEN_VAR" "https://git.example.ru/api/v4/projects/1/pipelines/latest" | jq .) }
- while myFunction; do
- if [ $(echo $api_pl_tmp | jq -r .status) = "success" ]
- then
- export PROJECT_CURRENT=$($api_pl_tmp | jq -r '{id:.id,sha:.sha[0:8]}' | base64)
- break
- fi
- if [ $(echo $api_pl_tmp | jq -r .status) = "failed" ]
- then
- echo "Error: Frontend can't be deployed!"
- exit 1
- fi
- if [ $(echo $api_pl_tmp | jq -r .status) = "running" ]
- then
- echo "Wait 5 sec... Frontend deploying!"
- sleep 5
- else
- echo Unknow status $(echo $api_pl_tmp | jq -r .status)
- exit 1
- fi
- done
但它不起作用,gitlab return 我的错误消息:
This GitLab CI configuration is invalid: jobs:deploy-to-dev:script config should be a string or a nested array of strings up to 10 levels deep
我该如何解决这个问题或者我的自定义函数有错误?
-
将命令与中间的命令分开。将您的命令作为一个命令,而不是多个。请记住它们是用空格连接的。
您的脚本的问题在于冒号 - 请参阅 https://gitlab.com/gitlab-org/gitlab-foss/-/issues/30097。
deploy:
stage: dev
services:
- docker:dind
script:
- "colon=:"
- myFunction () {
api_pl_tmp=$(curl -s --header "PRIVATE-TOKEN$colon $TOKEN_VAR" \
"https$colon//git.example.ru/api/v4/projects/1/pipelines/latest" | jq .);
}
- while myFunction; do
if [ $(echo $api_pl_tmp | jq -r .status) = "success" ]; then
export PROJECT_CURRENT=$($api_pl_tmp | jq -r '{id:.id,sha:.sha[0:8]}' | base64);
break;
fi;
if [ $(echo $api_pl_tmp | jq -r .status) = "failed" ]; then
echo "Error$colon Frontend cant be deployed";
exit 1;
fi;
if [ $(echo $api_pl_tmp | jq -r .status) = "running" ]; then
echo "Wait 5 sec... Frontend deploying!";
sleep 5;
else
echo Unknow status $(echo $api_pl_tmp | jq -r .status);
exit 1;
fi;
done
另外 jq .) }
还缺少一个 ;
。首先检查您自己 shell 中的脚本。使用 https://shellcheck.net 检查您的脚本。
另外 $($api_pl_tmp
缺少回显,而且引用有 很多 问题。使用一致的缩进并尝试编写可读代码以尽量减少拼写错误。