GitHub action失败,退出代码为1,当删除echo语句时。

huangapple go评论97阅读模式
英文:

GitHub action fails with exit code 1 when echo statement is removed

问题

我有一个GitHub操作步骤,看起来像这样:

  1. - if: ${{ steps.cache-images.outputs.cache-hit == 'true' }}
  2. name: 加载保存的Docker镜像
  3. run: |
  4. if [[ -f docker-images-backup/apisix-images.tar ]]; then
  5. [[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh before
  6. docker load --input docker-images-backup/apisix-images.tar
  7. make ci-env-up project_compose_ci=ci/pod/docker-compose.${{ steps.test_env.outputs.type }}.yml
  8. echo "加载Docker镜像"
  9. echo 测试类型:
  10. [[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after && echo "执行完毕"
  11. fi
  12. echo "退出if"

如果要进行进一步的讨论或翻译,请提供更多上下文。

英文:

I have a GitHub action step which looks like this:

  1. - if: ${{ steps.cache-images.outputs.cache-hit == 'true' }}
  2. name: Load saved docker images
  3. run: |
  4. if [[ -f docker-images-backup/apisix-images.tar ]]; then
  5. [[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh before
  6. docker load --input docker-images-backup/apisix-images.tar
  7. make ci-env-up project_compose_ci=ci/pod/docker-compose.${{ steps.test_env.outputs.type }}.yml
  8. echo "loaded docker images"
  9. echo test_type:
  10. [[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after && echo "executed"
  11. fi
  12. 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语句实现代码的条件执行:

将以下代码替换为:

  1. [[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after && echo "executed"

使用以下代码:

  1. if [[ ${{ steps.test_env.outputs.type }} != first ]]; then
  2. sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after
  3. 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:

  1. [[ ${{ steps.test_env.outputs.type }} != first ]] && sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after && echo "executed"

With:

  1. if [[ ${{ steps.test_env.outputs.type }} != first ]]; then
  2. sudo ./ci/init-${{ steps.test_env.outputs.type }}-test-service.sh after
  3. fi

huangapple
  • 本文由 发表于 2023年2月18日 12:50:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定