How to Run a Batch or cmd Step on Windows in GitHub Actions
Set shell: cmd to run classic batch scripts, and check errorlevel so a nonzero result fails the step.
Use shell: cmd for .bat/.cmd work. GitHub appends an errorlevel check to cmd steps, but for multi-command lines you should still test errorlevel yourself.
Steps
- Set the step
shell:tocmd. - Call the script with
call build.batso control returns to the step. - Check
if %errorlevel% neq 0 exit /b %errorlevel%after risky commands.
Workflow
.github/workflows/ci.yml
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v4
- name: Run a batch script
shell: cmd
run: |
call scripts\build.bat
if %errorlevel% neq 0 exit /b %errorlevel%Gotchas
- Use
callto invoke another .bat, otherwise control does not return and later lines are skipped. - cmd does not stop on the first failing command in a multi-line block; check
errorlevelexplicitly.
Related guides
How to Run a PowerShell Script Step on Windows in GitHub ActionsRun inline PowerShell or a .ps1 file on a windows-latest runner in GitHub Actions using shell: pwsh, passing…
How to Set the Default Shell to PowerShell or Bash on Windows in GitHub ActionsControl which shell runs your steps on a windows-latest runner in GitHub Actions with defaults.run.shell, cho…