【问题标题】:GitHub action fails with exit code 1 when echo statement is removed删除 echo 语句后,GitHub 操作失败,退出代码为 1
【发布时间】:2023-02-18 12:56:47
【问题描述】:

我有一个 GitHub 操作步骤,如下所示:

      - 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"

如果我删除为调试相同错误而添加的最后一条 echo 语句,它会始终失败(退出代码为 1)。

我已经尝试使用此脚本中变量值的不同组合在本地运行脚本,但它工作得很好。

我研究了一些关于在删除/添加打印语句时发生的 c 编程中的段错误。我不认为这是一个类似的案例,但我想知道 shell 脚本是否有类似的危险。

【问题讨论】:

    标签: bash shell github-actions


    【解决方案1】:

    所以事实证明,当一个步骤完全执行时,GitHub 操作会评估退出代码。当 ${{ steps.test_env.outputs.type }} 的值为 first 时,[[ ${{ steps.test_env.outputs.type }} != first ]] 会产生非零退出代码(退出代码 1)。因此 GitHub 操作失败,退出代码为 1。

    要解决此问题,您可以简单地使用 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
    

    【讨论】:

      猜你喜欢
      • 2021-03-26
      • 2020-10-10
      • 2022-01-16
      • 2020-04-19
      • 1970-01-01
      • 2017-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多