创建 GitHub 操作以使用当前发布标签发布到 DockerHub

问题描述 投票:0回答:1

我想要以下工作流程:

  • 在 GitHub 上起草新版本
  • YY.MM.DD
  • 的格式添加发布标签
  • 发布版本

应该发生的是

  • 使用上述发布标签创建发布,格式为
    YY.MM.DD
  • GitHub上的发布被标记为
    latest
    YY.MM.DD
  • 一个 docker 镜像被创建并发布到 DockerHub
  • DockerHub上的docker镜像包含标签
    latest
    ,当前sha和发布标签格式为
    YY.MM.DD

目前我有这个工作流程,它向 DockerHub 发布新版本和图像,但我缺少的是:

  • GitHub 上的发布未标记为
    latest
  • DockerHub 上的 docker 镜像不包含格式为
    YY.MM.DD
  • 的发布标签

任何帮助将不胜感激!

# This workflow uses actions that are not certified by GitHub.
# They are provided by a third-party and are governed by
# separate terms of service, privacy policy, and support
# documentation.

# GitHub recommends pinning actions to a commit SHA.
# To get a newer version, you will need to update the SHA.
# You can also reference a tag or branch, but the action may change without warning.

name: Publish Docker image

on:
  release:
    types: [published]

jobs:
  docker:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Set up QEMU
        uses: docker/setup-qemu-action@v2

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v2

      - name: Set up dynamic build ARGs
        id: getargs        
        run: echo "version=$(cat ./stable/VERSION)" >> $GITHUB_OUTPUT

      - name: Docker meta
        id: meta
        uses: docker/metadata-action@v3
        with:
          # list of Docker images to use as base name for tags
          images: |
            jokobsk/pi.alert
          # generate Docker tags based on the following events/attributes
          tags: |
            type=raw,value=latest
            type=schedule
            type=ref,event=branch
            type=ref,event=pr
            type=semver,pattern={{version}}
            type=semver,pattern={{major}}.{{minor}}
            type=semver,pattern={{major}}
            type=sha
      - name: Login to DockerHub
        if: github.event_name != 'pull_request'
        uses: docker/login-action@v2
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push
        uses: docker/build-push-action@v3
        with:
          context: .
          platforms: linux/amd64,linux/arm64,linux/arm/v7
          push: ${{ github.event_name != 'pull_request' }}
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}
docker github github-actions dockerhub
1个回答
0
投票

请检查:

https://blog.derlin.ch/github-actions-reusable-workflow-docker-images

作为奖励,我还希望能够推送到 Docker Hub,但仅限于特定情况(例如,从发布中,而不是从 PR 构建中)并且使用不同的标签集(没有 main-{sha}) . 这种条件性令人失望,因为默认情况下不支持它。玩了一会儿之后,我设计了一个 hack 来实现这个“如果”,而无需复制整个工作流程。 如果您有兴趣,请查看更新后的工作流程:https://github.com/derlin/rickroller/blob/e67546a90218c8300676c1270699cccdb5f7e053/.github/workflows/reusable_docker-build-and-push.yaml 让我知道如果您想发表评论,请发表评论!

希望有帮助。 菲利普·纳西门托

© www.soinside.com 2019 - 2024. All rights reserved.