Github 操作:运行 踩到特定 OS

Github Actions: Run step on specific OS

我正在运行在某些操作系统上设置工作流。

但是,有一个特定步骤我必须 运行 仅在 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? <--

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

有人可以帮忙吗?

您可以使用 if: matrix.os == 'NAME_FROM_MATRIX'if: runner.os == 'OS_TYPE'

用于检查矩阵上下文:

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

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

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

用于检查跑步者上下文:

if: runner.os == 'Linux'

if: runner.os == 'Windows'

if: runner.os == 'macOS'

相关文档:runner context

更新

GitHub 现在提供 RUNNER_OS 变量,这简化了单步检查:

- 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

对于更复杂的步骤,这可能是更好的方法,其中当前 OS 只是众多变量之一。