[subaction/matrix] Generate multiple matrices when targets reference other targets as build contexts

未关闭
#435 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@jeliebig 已经在做这个了。

开始于 2026年7月1日。

  • #472 来自 @jeliebig —— 未关闭

评估

难度
4/5
预计耗时
3-5 天
新手友好度
48/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
冷清
技术栈
docker, github-actions, typescript
领域
ci-cd, devops

调研方向

从 matrix subaction 入手,追踪当前目标列表如何从 docker-bake.hcl 示例生成。通过生成按依赖关系分组的矩阵,以及 staged GitHub Actions workflow 可以使用的 jobs_amount 和 matrices 输出(且不会重复构建依赖项)来定义完成标准。

由索引模型根据 Issue 内容生成。

描述

kind/enhancement
Description

We'd like to use the matrix subaction to generate multiple matrices, since we have a couple of targets that depend on other targets and would like to avoid building the same target multiple times.


As an example, consider the following docker-bake.hcl (containing targets with Dockerfiles that can't be easily merged into a multi-stage Dockerfile):

group "default" {
    targets = ["apps", "special-app"]
}

target "base" {
    context = "basedir"
    tags = ["base:latest"]
}

target "apps" {
    name = "app_${version}"
    context = "appdir/${version}"
    matrix =  {
        version = ["1-0-0", "2-0-0", "2-1-5"]
    }
    tags = ["app:${version}"]
    contexts = {
        base = "target:base"
    }
}

target "special-app" {
    context = "appdir/special"
    tags = ["special-app:latest"]
    contexts = {
        app = "target:app_2-0-0"
    }
}

Now we commit and push a change to basedir/Dockerfile, which triggers a GitHub workflow that builds these targets on multiple runners using the matrix subaction and pushes them to our registry.

Currently the subaction generates the following matrix:

[{"target": "base"}, {"target": "app_1-0-0"}, {"target": "app_2-0-0"}, {"target": "app_2-1-5"}, {"target": "special-app"}]

This generates 5 workflow jobs running in parallel, but most of them spend time building dependent targets first, which other jobs are already building. This results in a lot of duplicate work which could be avoided.

Ideally the subaction would generate multiple matrices like this:

[
    [{"target": "base"}],
    [{"target": "app_1-0-0"}, {"target": "app_2-0-0"}, {"target": "app_2-1-5"}],
    [{"target": "special-app"}]
]

This way we could define multiple jobs that depend on the previous ones and supply them with one matrix each.

Snippet of an example workflow using multiple matrices
jobs:
  generate:
    runs-on: ubuntu-latest
    outputs:
      jobs_amount: ${{ steps.generate.outputs.jobs_amount }}
      matrices: ${{ steps.generate.outputs.matrices }}
    steps:
      - uses: actions/checkout@v6
      - id: generate
        uses: docker/bake-action/subaction/matrix@multiple-matrices

  stage_1:
    needs:
      - generate
    if: ${{ fromJson(needs.generate.outputs.jobs_amount) >= 1 }}
    strategy:
      matrix:
        include: ${{ toJson(fromJson(needs.generate.outputs.matrices)[0]) }}
    steps:
      # ...

  stage_2:
    needs:
      - generate
      - stage_1
    if: ${{ fromJson(needs.generate.outputs.jobs_amount) >= 2 }}
    strategy:
      matrix:
        include: ${{ toJson(fromJson(needs.generate.outputs.matrices)[1]) }}
    steps:
      # ...

  stage_3:
    needs:
      - generate
      - stage_2
    if: ${{ fromJson(needs.generate.outputs.jobs_amount) >= 3 }}
    strategy:
      matrix:
        include: ${{ toJson(fromJson(needs.generate.outputs.matrices)[2]) }}
    steps:
      # ...

We imagine that this usecase is common enough to be supported here and would like to contribute that feature, since we already made it for our own workflows.

Would you be interested in this feature?

主要语言
TypeScript
星标
302
派生
42
平均合并
1 天 2 小时
30 天内合并 PR
7

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

docker/bake-action 的其他 Issue

查看 docker/bake-action 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。