【问题标题】:How to capture the Batch script error during a run in Github Actions workflow如何在 Github Actions 工作流程中捕获批处理脚本错误
【发布时间】:2022-01-10 11:55:18
【问题描述】:

当批处理脚本作为工作流中的作业运行时,它会以错误级别退出,但整体作业状态显示为成功,而不是失败。如何捕获错误并将工作流状态设为失败。

附加 Github Action 工作流 yaml 文件的 sn-ps

- name: Setup Release directory
  id: Release_directory
  run: .\\Compile_testsuite.bat
        
- name: Check on failures
  if: (${{ success() }} || ${{ failure() }}) && (${{ steps.Release_directory.outcome }} == 'failure')
  run: echo ${{steps.Release_directory.outcome}} #exit 1

附上Batch脚本的代码sn-p

@echo off
rem Get Current Path
set defaultPath=%CD%
set defaultPath=%defaultPath: =%

set /p version=<Version.txt
set ReleasePath=%defaultPath%\Release_%version%

set runPath=%ReleasePath%\run
set workloadsPath=%ReleasePath%\workloads
set commonPath=%workloadsPath%\common
set resultsPath=%ReleasePath%\results
echo %defaultPath%

if not exist %ReleasePath% mkdir %ReleasePath%
if not exist %runPath% mkdir %runPath%
if not exist %workloadsPath% mkdir %workloadsPath%
if not exist %resultsPath% mkdir %resultsPath%
if not exist %commonPath% mkdir %commonPath%

set Identifier=false

rem Iterate over directory
for /D %%w in (*) do (
    SETLOCAL ENABLEDELAYEDEXPANSION
        set workloadPath=%defaultPath%\%%~nxw

        for /f "tokens=1 delims=_" %%a in ("%%~nxw") DO ( 
            set var=%%a

            if "!var!" == "Excel" ( set "Identifier=true")
            if "!var!" == "Word" ( set "Identifier=true")
            if "!var!" == "Outlook" ( set "Identifier=true")
            if "!var!" == "Powerpoint" ( set "Identifier=true")
        )


        if "!Identifier!" == "true" (
            REM Print Workload path
            echo !workloadPath!
            cd !workloadPath!
            nuget install !workloadPath!\%%~nxw\packages.config -o !workloadPath!\packages\
            echo !workloadPath!\%%~nxw.sln
            msbuild !workloadPath!\%%~nxw.sln /t:Rebuild /p:Configuration=Release /p:Platform="Any CPU"
            rem echo "ERROR_LEVEL" !errorlevel!
            if not !errorlevel! == 0 ( exit /B !errorlevel!)
            set exePath=!workloadPath!\%%~nxw\bin\Release\Executable

            if not exist !workloadsPath!\%%~nxw\bin mkdir !workloadsPath!\%%~nxw\bin
            if not exist !workloadsPath!\%%~nxw\input mkdir !workloadsPath!\%%~nxw\input
            copy !exePath! !workloadsPath!\%%~nxw\bin
            copy !workloadPath!\%%~nxw\bin\Release\input !workloadsPath!\%%~nxw\input

            cd %defaultPath%
        )

在 Github Actions 中运行的工作流的输出

我正在尝试使用 "msbuild" 运行构建 vs 代码解决方案。如果构建不成功,批处理脚本应该终止。所以我添加了 if 条件来检查 msbuild 的状态。 当我运行工作流时,我正在尝试检查解决方案的构建,如果构建成功,我需要使用下一个任务运行应用程序。

【问题讨论】:

  • 如果你要发布代码,它需要是minimal reproducible example。您不能只发布for 循环的一部分并期望我们了解它是如何工作的。一旦您的一个循环失败,您似乎正在使用exit /b !errorlevel!,但我们不知道退出的是什么,因为您尚未发布批处理文件的其余部分。据我们所知,您只需要使用exit !errorlevel!
  • 我觉得 if 语法有问题,试试:... ${{ steps.Release_directory.outcome == 'failure' }} )
  • @Compo 已添加部分批处理脚本
  • @Saikrishna,您添加了更多内容,但仍未包含任何相关部分。您对%%~nxw 的使用明确表明您包含的所有内容都只是for 循环命令的一部分。此外,您还没有提供任何信息来显示您如何传播变量 IdentifierworkloadsPathexePathdefaultPath。我们不知道您在何处、何时或如何使用延迟扩展,是否有任何代码在 called 标签或脚本中,等等。我们无法复制您的问题,除非我们可以匹配您的代码和你的环境。
  • @Compo 的基本思想是,我必须在 git 操作中运行 vs 的 ms office 应用程序。我正在检查构建的步骤,如果它成功完成,那么我想在下一步运行应用程序。我已经为批处理脚本添加了其余代码。我只是在检查 msbuild 的错误,仅此一项是最重要的。

标签: batch-file github-actions


【解决方案1】:

我已将输出定向到一个新文件,并检查了文件中的失败关键字。如果发生任何错误,我将终止在工作流中运行。

【讨论】:

  • 我建议github-actions 看不到批次的errorlevel,而只检测批次是否成功完成。也许您可以尝试通过将数字除以 0 或尝试执行不存在的程序来强制批处理错误。我对github-actions一无所知,所以这真的是在黑暗中开枪。
猜你喜欢
  • 1970-01-01
  • 2011-11-01
  • 2020-04-02
  • 2020-01-09
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 2020-08-20
  • 2020-03-23
相关资源
最近更新 更多