簡體   English   中英

如何在 GitHub Actions 工作流中實現語義版本控制?

[英]How to implement semantic versioning in GitHub Actions workflow?

我想對我的 docker 映像進行語義版本控制,這些映像是由 GitHub 操作構建並推送到 GitHub 容器注冊表的。

我在這里找到了一個令人滿意的解決方案: https://stackoverflow.com/a/69059228/12877180

根據解決方案我復制了以下YAML。

name: Docker CI

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

env:
  REGISTRY: ghcr.io

jobs:
  build-push:
    # needs: build-test
    name: Buid and push Docker image to GitHub Container registry
    runs-on: ubuntu-latest
    permissions:
      packages: write
      contents: read

    steps:
    - name: Checkout the repository
      uses: actions/checkout@v2

    - name: Login to GitHub Container registry
      uses: docker/login-action@v1
      env:
        USERNAME: ${{ github.actor }}
        PASSWORD: ${{ secrets.GITHUB_TOKEN }}
      with:
        registry: ${{ env.REGISTRY }}
        username: ${{ env.USERNAME }}
        password: ${{ env.PASSWORD }}

    - name: Get lowercase repository name
      run: |
        echo "IMAGE=${REPOSITORY,,}">>${GITHUB_ENV}
      env:
        REPOSITORY: ${{ env.REGISTRY }}/${{ github.repository }}

    - name: Build and export the image to Docker
      uses: docker/build-push-action@v2
      with:
        context: .
        file: ./docker/Dockerfile
        target: final
        push: true
        tags: |
          ${{ env.IMAGE }}:${{ secrets.MAJOR }}.${{ secrets.MINOR }}
        build-args: |
          ENVIRONMENT=production

    - name: Update Patch version
      uses: hmanzur/actions-set-secret@v2.0.0
      with:
        name: 'MINOR'
        value: $((${{ secrets.MINOR }} + 1))
        repository: ${{ github.repository }}
        token: ${{ secrets.GH_PAT }}

不幸的是,這不起作用。

MINOR secret 的初始值為0 如果build-push作業是第一次執行,則 docker 映像將使用ghcr.io/my-org/my-repo:0.0語法完美地推送到GHCR 然后, build-push作業的目的是將MINOR密鑰增加1

如果在新事件之后再次執行操作作業build-push ,則在嘗試使用遞增標簽構建 docker 圖像時出現錯誤。

/usr/bin/docker buildx build --build-arg ENVIRONMENT=production --tag ghcr.io/my-org/my-repo:***.*** --target final --iidfile /tmp/docker-build-push-HgjJR7/iidfile --metadata-file /tmp/docker-build-push-HgjJR7/metadata-file --file ./docker/Dockerfile --push .
error: invalid tag "ghcr.io/my-org/my-repo:***.***": invalid reference format
Error: buildx failed with: error: invalid tag "ghcr.io/my-org/my-repo:***.***": invalid reference format

您需要在 bash 命令中增加版本,如下所示:

      - name: Autoincrement a new patch version
        run: |
          echo "NEW_PATCH_VERSION=$((${{ env.PATCH_VERSION }}+1))" >> $GITHUB_ENV
      - name: Update patch version
        uses: hmanzur/actions-set-secret@v2.0.0
        with:
          name: 'PATCH_VERSION'
          value: ${{ env.NEW_PATCH_VERSION }}
          repository: ${{ github.repository }}
          token: ${{ secrets.REPO_ACCESS_TOKEN }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM