英文:
GitHub action fails with exit code 1 when echo statement is removed
问题
我有一个GitHub操作步骤,看起来像这样:
- if: ${{ steps.cache-images.outputs.cache-hit == 'true' }}
name: 加载保存的Docker镜像
run: |
if [[ -f docker-images-backup/apisix-images.tar ]]; then
[[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh before
docker load --input docker-images-backup/apisix-images.tar
make ci-env-up project_compose_ci=ci/pod/docker-compose.${{ steps.test_env.outputs.type }}.yml
echo "加载Docker镜像"
echo 测试类型:
[[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after && echo "执行完毕"
fi
echo "退出if"
如果要进行进一步的讨论或翻译,请提供更多上下文。
英文:
I have a GitHub action step which looks like this:
- if: ${{ steps.cache-images.outputs.cache-hit == 'true' }}
name: Load saved docker images
run: |
if [[ -f docker-images-backup/apisix-images.tar ]]; then
[[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh before
docker load --input docker-images-backup/apisix-images.tar
make ci-env-up project_compose_ci=ci/pod/docker-compose.${{ steps.test_env.outputs.type }}.yml
echo "loaded docker images"
echo test_type:
[[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after && echo "executed"
fi
echo "exited if"
Which fails consistently (with exit code 1) if I remove the last echo statement that I added to debug the same error.
I have tried running the script locally with different combinations of values of the variables in this script but it works perfectly fine.
I have studied a little about segfaults in c programming occcuring when removing/adding a print statement. I don't think this is a similar case but I wonder if shell scripting has similar kind of hazard.
答案1
得分: 2
GitHub操作在步骤完全执行时评估退出代码。当${{ steps.test_env.outputs.type }}
的值为first
时,[[ ${{ steps.test_env.outputs.type }} != first ]]
导致非零退出代码(退出代码1)。由于这个原因,GitHub操作以退出代码1失败。
当我在最后添加一个echo语句时,最后的退出代码变为零(由于前一个语句的非零退出状态被覆盖),因此操作不会失败。
为了解决这个问题,您可以简单地使用if语句实现代码的条件执行:
将以下代码替换为:
[[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after && echo "executed"
使用以下代码:
if [[ ${{ steps.test_env.outputs.type }} != first ]]; then
sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after
fi
英文:
So it turns out that GitHub actions evaluate the exit code when a step is executed completely. When the value of ${{ steps.test_env.outputs.type }}
is first
then [[ ${{ steps.test_env.outputs.type }} != first ]]
results in a non-zero exit code (exit code 1). Due to this, the GitHub action fails with exit code 1.
When I add an echo statement at the end, the last exit code becomes zero (and the non-zero exit status due to the previous statement gets covered) due to this the action does not fail.
To fix this you can simply implement conditional execution of code using an if statement:
Replace:
[[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after && echo "executed"
With:
if [[ ${{ steps.test_env.outputs.type }} != first ]]; then
sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after
fi
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论