【问题标题】:Github Actions: Run step on specific OSGithub Actions:在特定操作系统上运行步骤
【发布时间】:2020-01-16 15:51:30
【问题描述】:

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

但是,我必须在 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? <--

我找不到任何示例或说明如何在特定操作系统上运行步骤。

有人可以帮忙吗?

【问题讨论】:

    标签: github-actions


    【解决方案1】:

    您可以使用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
    

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

    【讨论】:

    • 使用if: matrix.os == ubuntu-latest 我得到:无法识别的命名值:'ubuntu-latest'。位于表达式中的第 14 位:matrix.os == ubuntu-latest。但是if: runner.os == 'Linux' 为我工作。谢谢!
    • @yahavi if matrix.os == 'ubuntu-latest' 应该可以工作 - 你也需要引号。
    • 另外,注意它必须是单引号,双引号是语法错误。
    • Windows环境下if语句中'if'后面缺少'('
    • @decades 你没有在你的步骤中添加shell: bash。如果没有该行,工作流将使用默认 shell(在 Windows 上为 PowerShell),您将收到您提供的错误消息。 shell: bash 在这里不是用于装饰,而是为了确保步骤将在所有平台上正确运行。见docs.github.com/en/actions/reference/…
    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 2020-12-07
    • 2020-01-01
    • 2020-12-15
    • 2021-11-16
    • 2020-05-25
    • 2020-03-24
    • 2020-01-28
    相关资源
    最近更新 更多