Github Actions: 在特定的操作系统上运行步骤

我在运行一些操作系统的工作流程。

然而,有一个特定的步骤,我只能在 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? <--

我找不到任何关于如何在特定操作系统上运行步骤的例子或解释。

有人能帮忙吗?

18328 次浏览

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.