Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Actions: Run step on specific OS

I'm running a workflow on some 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? <-- 

I couldn't find any examples nor explanation on how to run steps on specific OSes.

Can somebody help?

like image 355
yahavi Avatar asked Sep 15 '19 16:09

yahavi


People also ask

Do GitHub Actions steps run in parallel?

Not at this moment. Only jobs can run in parallel, but steps always run sequentially.

How do I set an environment variable in GitHub Actions?

To set a custom environment variable, you must define it in the workflow file. The scope of a custom environment variable is limited to the element in which it is defined. You can define environment variables that are scoped for: The entire workflow, by using env at the top level of the workflow file.

Are GitHub Actions steps sequential?

By default, all steps in a single job execute sequentially. If you're trying to limit the number of parallel 'jobs' then you you can set a limit of 1 for the workflow by setting max-parallel: 1 within the jobs.

Can you schedule GitHub Actions?

You can configure your workflows to run when specific activity on GitHub happens, at a scheduled time, or when an event outside of GitHub occurs.


1 Answers

You can use either if: matrix.os == 'NAME_FROM_MATRIX' or if: runner.os == 'OS_TYPE'

For checking matrix context:

if: matrix.os == 'ubuntu-latest'

if: matrix.os == 'windows-latest'

if: matrix.os == 'macOS-latest'

For checking runner context:

if: runner.os == 'Linux'

if: runner.os == 'Windows'

if: runner.os == 'macOS'

Related documentation: runner context

UPDATE

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.

like image 121
Samira Avatar answered Sep 18 '22 16:09

Samira