You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 07:09:53