我在 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: ...
如您所见,我有多个重复项:
- 由于
tag
和branch main
运行政策,工作流重复 - 复合动作的执行有很多样板
减少这些重复并重用构建操作的最佳方法是什么? (并行)