如何在 GitHub Actions 工作流程中实现语义版本控制?

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

我想对我的 docker 镜像进行语义版本控制,这些镜像是通过 GitHub Action 构建并推送到 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/[email protected]
      with:
        name: 'MINOR'
        value: $((${{ secrets.MINOR }} + 1))
        repository: ${{ github.repository }}
        token: ${{ secrets.GH_PAT }}

不幸的是,这不起作用。

MINOR
秘密的初始值为
0
。如果第一次执行
build-push
作业,则使用
GHCR
语法将 docker 镜像完美推送到
ghcr.io/my-org/my-repo:0.0
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
docker github continuous-integration github-actions semantic-versioning
2个回答
1
投票

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

      - name: Autoincrement a new patch version
        run: |
          echo "NEW_PATCH_VERSION=$((${{ env.PATCH_VERSION }}+1))" >> $GITHUB_ENV
      - name: Update patch version
        uses: hmanzur/[email protected]
        with:
          name: 'PATCH_VERSION'
          value: ${{ env.NEW_PATCH_VERSION }}
          repository: ${{ github.repository }}
          token: ${{ secrets.REPO_ACCESS_TOKEN }}

1
投票

github有很多语义版本的action,可以根据个人的用例来使用。很少有关于提交消息的工作以及关于分支名称的其他工作。

我使用了https://github.com/marketplace/actions/semver-action,它适用于分支名称,并且支持

trunk-based
git-flow
分支模型

  - name: Calculate Semantic Version tag
    id: semver-tag
    uses: gandarez/[email protected]
    with:
      branching_model: "trunk-based"
      main_branch_name: "main"
      major_regex: "(?i)^(.+:)?(release/.+)"
      minor_regex: "(?i)^(.+:)?(feature/.+)"
      patch_regex: "(?i)^(.+:)?(hotfix/.+)"
      debug: "true"

它输出语义版本,任何后续作业都可以使用它来创建 git 标签,例如或 docker 镜像上的标签。

例如下面使用上一个作业的输出并创建一个标签

  - name: 🔖 Create Git Tag
    uses: rickstaa/[email protected]
    with:
      tag: ${{ steps.semver-tag.outputs.semver_tag }}

请注意:如果您的 PR 使用 Squash Commit 消息,则此操作将不起作用,因为此 GitHub 操作会从最新提交的 Git 日志中检索源分支名称

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