配置GitHub Action实现按指定Tag发布Docker镜像至DockerHub
解决GitHub Release未标记latest及DockerHub镜像缺少YY.MM.DD标签的问题
修改后的完整Workflow配置
# 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=raw,value=${{ github.event.release.tag_name }} # 添加YY.MM.DD格式标签 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 }} - name: Mark GitHub Release as latest uses: actions/github-script@v6 with: script: | await github.rest.repos.updateRelease({ owner: context.repo.owner, repo: context.repo.repo, release_id: context.payload.release.id, make_latest: true })
关键修改说明
添加DockerHub的YY.MM.DD格式标签
在Docker meta步骤的tags列表中新增一行:type=raw,value=${{ github.event.release.tag_name }}因为你的Release标签本身就是
YY.MM.DD格式,直接引用事件中的标签名即可生成对应Docker镜像标签。将GitHub Release标记为latest
添加Mark GitHub Release as latest步骤,使用actions/github-script调用GitHub API,将当前发布的Release设置为仓库的最新版本。该API会自动取消之前的latest标记,确保只有当前Release被标记为latest。
内容的提问来源于stack exchange,提问作者jokob-sk
相关产品推荐
相关产品推荐

