如何在GitHub Actions中仅为Linux Runner指定container配置段
解决方案
要让作业在Windows环境自动忽略container配置,可利用GitHub Actions的条件字段语法,仅在Linux runner下加载容器配置:
修改后的YAML配置如下:
name: My Build on: push jobs: job1: strategy: matrix: os: ["windows-latest", "ubuntu-latest"] runs-on: ${{ matrix.os }} ${{ if eq(matrix.os, 'ubuntu-latest') }}: container: image: catthehacker/ubuntu:runner-latest options: --privileged steps: - run: | echo "${{ matrix.os }}"
原理说明
- 通过
${{ if eq(matrix.os, 'ubuntu-latest') }}:条件判断,仅当运行环境为Linux时,才会解析下方的container配置段。 - Windows runner环境下该条件不成立,
container配置会被完全跳过,不会触发容器相关的错误。
内容的提问来源于stack exchange,提问作者Manolo Carrasco Moñino
相关产品推荐
相关产品推荐

