command-linegithub-actionswindows-server

Exit command in GitHub Actions workflow on windows-latest


I'm attempting to translate a simple GitHub Actions workflow from running on ubuntu-latest to windows-latest.

---
name: Run extract_members.py on a schedule

on:
  workflow_dispatch:
  schedule:
    - cron:  '0 6 * * *'

jobs:
  extract:
    runs-on: windows-latest
    steps:
      - name: Check out repo
        uses: actions/checkout@v4
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.12'
      - name: Install requirements
        run: pip install -r requirements.txt
      - name: Run script
        run: python github_actions_test.py
      - name: Commit and push if the data has changed
        run: |-
          git config user.name "Automated"
          git config user.email "actions@users.noreply.github.com"
          git add -A
          git commit -m "Updated data" || exit 0
          git push
...

But where there are no changes to commit the workflow errors, with the following information: The term 'exit' is not recognized as a name of a cmdlet, function, script file, or executable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

I thought exit was a legitimate command on Windows Server, as it is on Ubuntu. What should I use instead?


Solution

  • Run on windows uses powershell core (pwsh), not the windows commandline by default.

    So the line should probably read something along these lines:

    & git commit test
    if ($LASTEXITCODE -ne 0) { exit 0; }
    

    where exit 0; could also be return; to just skip the rest of the script.

    If you want to run it as a windows shell script, add:

    - name: Commit and push if the data has changed
      run: |-
         git config user.name "Automated"
         git config user.email "actions@users.noreply.github.com"
         git add -A
         git commit -m "Updated data" || exit 0
         git push
      shell: cmd                     # <-- Specify the Windows Commandline explicitly
    

    If you want all your run: blocks to use cmd as default shell, you can set the defaults:

    defaults:
      run:
        shell: cmd