Github 操作 - 用于并行图像构建的多个工作流

我在 GitHub 中有一个包含多个服务的 monorepo。
我想在两个条件下同时构建它们(使用 GitHub actions):

  • 标记 - 使用标记名称 (service-a:v1.0.0) 构建图像
  • 主分支 - 使用标记 latest 和缓存构建图像。

我找不到如何创建通用工作流和修改参数(如函数)的方法。
我能想到的唯一解决方案是创建一个像这样的复合动作:

runs:
  using: "composite"
  steps:
    - uses: actions/checkout@v2
      with:
        lfs: true

    - uses: docker/setup-qemu-action@v1

    - uses: docker/setup-buildx-action@v1

    - uses: docker/login-action@v1
      with:
        registry: ${{ secrets.registry }}
        username: ${{ secrets.username }}
        password: ${{ secrets.password }}

    - name: Environment details
      id: github_ref
      run: |
        echo ::set-output name=SOURCE_TAG::${GITHUB_REF#refs/tags/}

    - uses: docker/build-push-action@v2
      with:
        context: ${{ inputs.context }}
        push: true
        file: ${{ inputs.dockerfile }}
        tags: ${{ secrets.registry }}/${{ inputs.image }}:{{ steps.github_ref.outputs.SOURCE_TAG }}
        build-args: ${{ inputs.build-args }}
        cache-from: ${{ inputs.cache-from }}
        cache-to: ${{ inputs.cache-to }}

但是现在我遇到了一个问题,我需要在 2 个工作流中为服务指定所有作业,例如:

主分支工作流程:

on:
  push:
    branches:
      - main

jobs:
  build-service-a:
    timeout-minutes: 15
    runs-on: ubuntu-latest
    steps:
      - uses: ./.github/actions/build-image
        with:
          dockerfile: service-a/Dockerfile
          context: .
          image: service-a

  build-service-b:
    timeout-minutes: 15
    runs-on: ubuntu-latest
    steps:
      - uses: ./.github/actions/build-image
        with:
          dockerfile: service-b/Dockerfile
          context: .
          image: service-b

标签分支工作流程:


on:
  release:
    types:
      - created

jobs:
  build-service-a:
    timeout-minutes: 15
    runs-on: ubuntu-latest
    steps:
      - uses: ./.github/actions/build-image
        with:
          dockerfile: service-a/Dockerfile
          context: .
          image: service-a
          cache-to: ...
          cache-from: ...

  build-service-b:
    timeout-minutes: 15
    runs-on: ubuntu-latest
    steps:
      - uses: ./.github/actions/build-image
        with:
          dockerfile: service-b/Dockerfile
          context: .
          image: service-b
          cache-to: ...
          cache-from: ...

如您所见,我有多个重复项:

  • 由于 tagbranch main 运行政策,工作流重复
  • 复合动作的执行有很多样板

减少这些重复并重用构建操作的最佳方法是什么? (并行)

niksosf 回答:Github 操作 - 用于并行图像构建的多个工作流

如果你使用矩阵,你会得到稍微好一点的结果:

jobs:
  build:
    runs-on: ubuntu-latest
    strategy:
      matrix:
       include:
         - dockerfile: service-a/Dockerfile
           image: "service-a"
           cache-to: ...
           cache-from: ...
         - dockerfile: service-b/Dockerfile
           image: "service-b"
           cache-to: ...
           cache-from: ...
    steps:
      - uses: ./.github/actions/build-image
        with:
          dockerfile: ${{ matrix.dockerfile }}
          context: .
          image: : ${{ matrix.image }}
          cache-to: ${{ matrix.cache-to}}
          cache-from: ${{ matrix.cache-from}}

这不会使您免于 The workflows are duplicated because of the tag and branch main running policy,但会减少工作流程中的双板代码。

本文链接:https://www.f2er.com/2979.html

大家都在问