我们使用 GitLab 作为 terraform 锁定文件。我们经常会遇到获取锁等错误。这显然是他们的能力问题。如果我们重新运行该作业,它几乎总是会通过。
所以我想设置一些查看日志的东西,如果它看到特定的错误文本,则重新运行作业。看看他们的文档等,我不认为这是一种可能性。但我无法想象其他人还没有实现一些东西来做到这一点。
虽然 GitLab 不提供日志消息检测作为重试功能,但可以使用可用的功能(包括
retry:exit_codes:
)构建此解决方案 - 或者甚至完全在脚本中进行重试处理。
简短的版本是这样的:您使用脚本逻辑来捕获特定的错误消息,当找到时,您可以使用特定的状态代码退出(您将在
retry:exit_codes:
中指定),或者直接在中重试脚本或命令作业脚本逻辑。
显然有很多方法可以处理这个问题,bash(或类似的 shell)中的一种特定实现可能如下所示:
retry:exit_codes:
列表中set -e
(或使用 set +e
)set -e
)myjob:
variables:
# sometimes required for correct exit code detection with some executors
# uncomment if your script always exits with exit code 1
# FF_USE_NEW_BASH_EVAL_STRATEGY: "1"
retry:
max: 4
exit_codes:
- 42
script:
- ./myscript.sh
这是未经测试的代码,但应该解释基本思想:
#!/usr/bin/env bash
# myscript.sh
set +e
set -o pipefail
err_msg="Error locking state"
special_code=42
terraform apply 2>&1 | tee -a output.txt
exit_code=$?
if [[ $exit_code -ne 0 ]]; then
if grep -q "$err_msg" < output.txt; then
exit $special_code
else
exit $exit_code
fi
fi
set -e
# ...
或者,只需完全在 bash 中处理重试逻辑,例如,如 Unix stackexchange 问题中所述:在出现特定错误消息时重新运行命令。