【发布时间】:2021-05-25 14:55:27
【问题描述】:
我的目标是构建 docker 镜像并根据 git 分支和标签对其进行标记。以下规则应适用于只有一个 GitHub Actions 工作流。
规则:
- 推送开发分支创建一个带有标签的新图像:最新
- 推送到主分支创建一个带有标签的新图像:stable
- 主分支上的标签创建一个带有给定 git 标签的新图像作为 docker 图像标签 f.e。 git 标签:1.0.0 --> 镜像标签:registry/company/project:1.0.0
我的问题是:你有没有更好的(更少的代码重复)方法来解决以下 GitHub Actions 配置的问题?
env:
RELEASE_VERSION: ${GITHUB_REF#refs/*/}
... (previous jobs)
containerize:
needs: build
runs-on: ubuntu-latest
steps:
... (previous steps)
- name: Build and push Docker image to Registry with latest as image version
if: github.ref == 'refs/heads/develop'
uses: docker/build-push-action@v2
with:
context: .
file: ./docker/Dockerfile
push: true
tags: ${{ secrets.REGISTRY_URL }}/company/project:latest
- name: Build and push Docker image to Registry with stable as image version
if: github.ref == 'refs/heads/main'
uses: docker/build-push-action@v2
with:
context: .
file: ./docker/Dockerfile
push: true
tags: ${{ secrets.REGISTRY_URL }}/company/project:stable
- name: Build and push Docker image to Registry with given tag as image version
if: ${{ env.RELEASE_VERSION }} != '' && github.ref == 'refs/heads/main'
uses: docker/build-push-action@v2
with:
context: .
file: ./docker/Dockerfile
push: true
tags: ${{ secrets.REGISTRY_URL }}/company/project:${{ env.RELEASE_VERSION }}
【问题讨论】:
标签: docker github-actions