GitHub Actions即使收到非零退出代码也会将作业完成为成功

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

GitHub Actions completes job as success even it receives non-zero exit code

问题

我创建了 test.bat 以便从GitHub Actions执行。我遇到的问题是,即使从 test.bat 收到非零退出代码,GitHub Actions 也会将作业标记为成功。

以下是GitHub Actions的YAML文件。

jobs:
  run_test:
    runs-on: [self-hosted, Windows]
    steps:
    - run: ./test.bat

以下是执行测试的 test.bat 文件。

REM 运行测试,并在测试失败时返回非零退出代码。
test.exe -fail

REM 现在,假设测试失败,它返回退出代码 4。
echo %error_level%

现在,即使收到非零退出代码(为4),GitHub Actions也将作业标记为成功。

如果我删除 echo %error_level%,那么GitHub Actions会正确将作业标记为失败。

为什么?

英文:

I made my test.bat to be executed from GitHub Actions. The issue I have is that GitHub Actions completes job as success even it receives non-zero exit code from the test.bat.

Here is the YAML file for GitHub Actions.

<!-- language: lang-yml -->

jobs: 
  run_test:
    runs-on: [self-hosted,Windows]
    steps: 
    - run: ./test.bat

Here is the test.bat file that executes test.

<!-- language: lang-bat -->

REM This runs test and returns non-zero exit code when test fails.
test.exe -fail     

REM Now, assume test failed, and it retruns exit code 4.
echo %error_level%

Now, GitHub Actions completes the job as success even it receves non-zero exit code (which is 4).

If I remove echo %error_level% then GitHub Action completes the job as fail, properly.

Why?

答案1

得分: 1

The echo command succeeds successfully and returns an exit code of 0.

Make sure you either capture the ERRORLEVEL:

REM This runs the test and returns a non-zero exit code when the test fails.
test.exe -fail     
set return_code=%ERRORLEVEL%
REM Now, assume the test failed, and it returns an exit code of 4.
echo %return_code%
exit %return_code%

Or do an if statement and return your own exit code.

REM This runs the test and returns a non-zero exit code when the test fails.
test.exe -fail

IF NOT %ERRORLEVEL% == 0 (
    echo %ERRORLEVEL%
    exit 1  
)     
英文:

The echo command succeeds sucessfully and returns an exit code of 0.

Make sure you either capture the ERRORLEVEL:

REM This runs test and returns non-zero exit code when test fails.
test.exe -fail     
set return_code=%ERRORLEVEL%
REM Now, assume test failed, and it returns exit code 4.
echo %return_code%
exit %return_code%

Or do an if statement and return your own exit code.

REM This runs test and returns non-zero exit code when test fails.
test.exe -fail

IF NOT %ERRORLEVEL% == 0 (
    echo %ERRORLEVEL%
    exit 1  
)     

huangapple
  • 本文由 发表于 2023年3月3日 18:20:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75625810.html
匿名

发表评论

匿名网友

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

确定