yahavi
yahavi

Reputation: 6073

GitHub Actions: Run step on specific OS

I'm using GitHub Actions to run a workflow on multiple operating systems.

However, there is a specific step that I have to run only on Ubuntu:

runs-on: ${{ matrix.os }}
strategy:
    matrix:
        os: [ubuntu-latest, windows-latest, macos-latest]
steps:
    - name: Setup Ubuntu
      run : export DISPLAY="127.0.0.1:10.0"
      if: # --> What should be here? <--

How can I run steps only on specific operating systems?

Upvotes: 139

Views: 36332

Answers (2)

Samira
Samira

Reputation: 9761

GitHub provides RUNNER_OS variable now, which simplifies checks inside single step:

- name:  Install
  run:   |
         if [ "$RUNNER_OS" == "Linux" ]; then
              apt install important_linux_software
         elif [ "$RUNNER_OS" == "Windows" ]; then
              choco install important_windows_software
         else
              echo "$RUNNER_OS not supported"
              exit 1
         fi
  shell: bash

This might be better approach for more complex steps, where current OS is just one of many variables.

Do not use matrix.os because runner names are not a reliable way to determine the platform.

Upvotes: 222

friederbluemle
friederbluemle

Reputation: 37147

Came here via Google search.

In case anyone else is running into issues with line endings with a matrix build, and you want to avoid unnecessarily setting Git configuration on Linux and macOS runners, the following will work:

  - if: runner.os == 'Windows'
    run: |
      git config --global core.autocrlf false
      git config --global core.eol lf

Upvotes: 14

Related Questions