【问题标题】:Trying to simplify GitHub workflow (Docker image builds)尝试简化 GitHub 工作流程(Docker 镜像构建)
【发布时间】: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


    【解决方案1】:

    目前,我认为没有更好的方法。

    您可以尝试将${{ secrets.REGISTRY_URL }}/company/project 替换为环境变量。类似${{ env.IMAGE }}:latest 并在工作级别设置一次:

    containerize:
        needs: build
        runs-on: ubuntu-latest
        env:
          IMAGE: ${{ secrets.REGISTRY_URL }}/company/project
    

    但这并没有太大的改进

    【讨论】:

      猜你喜欢
      • 2020-07-08
      • 2017-08-26
      • 1970-01-01
      • 2023-02-20
      • 2020-10-06
      • 2022-08-18
      • 1970-01-01
      • 2021-09-30
      • 2021-08-01
      相关资源
      最近更新 更多