我怎样才能让分支名称连接到 github 操作管道中的标签?

How can i get the branch name connected to a tag in github actions pipeline?

我正在尝试使用 github 操作构建 docker 图像,如果标记提交发生并且在暂存和主要分支上有推送或拉取请求,则会触发它。我还使用 kustomize 来根据提交自定义遗留清单。但是每次我尝试标记提交以触发管道时我都会遇到问题,它在提交 kustomize.yaml 的部分失败并出现此错误

Run ad-m/github-push-action@master
Push to branch refs/tags/v0.3.8
To https://github.com/ahmedappout08/robo-demo.git
 ! [rejected]        HEAD -> v0.3.8 (already exists)
error: failed to push some refs to 'https://github.com/ahmedappout08/robo-demo.git'
hint: Updates were rejected because the tag already exists in the remote.
Error: Invalid exit code: 1
    at ChildProcess.<anonymous> (/home/runner/work/_actions/ad-m/github-push-action/master/start.js:29:21)
    at ChildProcess.emit (events.js:210:5)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5) {
  code: 1
}
Error: Invalid exit code: 1
    at ChildProcess.<anonymous> (/home/runner/work/_actions/ad-m/github-push-action/master/start.js:29:21)
    at ChildProcess.emit (events.js:210:5)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:283:5)

这是 Ci.yaml 文件:

name: Docker

on:
  
  push:
    branches:
      - main
      - staging
    tags:
      - v*
  pull_request:
    branches:
      - main
      - staging

env:
  # TODO: Change variable to your image's name.
  IMAGE_NAME: robo-demo 
jobs:
  # Run tests.
  # See also https://docs.docker.com/docker-hub/builds/automated-testing/

  # Push image to GitHub Packages.
  # See also https://docs.docker.com/docker-hub/builds/
  push:
    # Ensure test job passes before pushing image.

    runs-on: ubuntu-latest


    steps:
      - name: Checkout master
        uses: actions/checkout@main

      

      - name: Build image
        run: docker build . --file Dockerfile --tag $IMAGE_NAME

      - name: Log into registry
        run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login docker.pkg.github.com -u ${{ github.actor }} --password-stdin
      
      - name: Push image tag
        if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/staging' }} == false
        run: |
          IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME
          # Change all uppercase to lowercase
          IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
          # Strip git ref prefix from version
          #VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),,')
          VERSION=$GITHUB_SHA
          # Strip "v" prefix from tag name
          [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
          # Use Docker `latest` tag convention
          echo IMAGE_ID=$IMAGE_ID
          #echo VERSION=${GITHUB_REF##*/}
          echo VERSION=$GITHUB_SHA
          docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
          docker push $IMAGE_ID:$VERSION
      - name: push image main & staging
        if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/staging' }}
        run: |
          IMAGE_ID=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME
          # Change all uppercase to lowercase
          IMAGE_ID=$(echo $IMAGE_ID | tr '[A-Z]' '[a-z]')
          # Strip git ref prefix from version
          VERSION=$(echo "${{ github.ref }}" | sed -e 's,.*/\(.*\),,')
          # Strip "v" prefix from tag name
          [[ "${{ github.ref }}" == "refs/tags/"* ]] && VERSION=$(echo $VERSION | sed -e 's/^v//')
          # Use Docker `latest` tag convention
          echo IMAGE_ID=$IMAGE_ID
          echo VERSION=${GITHUB_REF##*/}
          docker tag $IMAGE_NAME $IMAGE_ID:$VERSION
          docker push $IMAGE_ID:$VERSION

      - name: congrats
        run: |
          echo "Image Built on Branch" ${GITHUB_REF##*/}
      - name: Setup Kustomize
        if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/staging' }} == false
        uses: imranismail/setup-kustomize@v1
        with:
          kustomize-version: "3.6.1"
      - name: Update Kubernetes resources
        if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/staging' }} == false
        run: |
          cd k8s-deployment/feature-version
          kustomize edit set image robo-image=docker.pkg.github.com/${{ github.repository }}/$IMAGE_NAME:$GITHUB_SHA
          cat kustomization.yaml
      - name: Commit files
        if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/staging' }} == false
        run: |
          git config --local user.email "action@github.com"
          git config --local user.name "GitHub Action" 
          git commit -am "Bump docker tag"
      - name: Push changes
        if: ${{ github.ref == 'refs/heads/main' || github.ref == 'refs/heads/staging' }} == false
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }} 

我不知道如何解决这个问题以将提交推送到连接到标签的分支本身,因为我需要在每次构建后更新 kustomize.yaml

I don't how to fix that to push the commit to the branch itself connected to the tag as i need to update the kustomize.yaml there after each building

就Git本身而言,分支名称和标签名称之间没有任何联系。

在 Git 级别,any 名称(任何引用)只是存储哈希 ID 的名称。 Branch 名称特别被限制为仅存储 commit 哈希 ID,而标签名称可以存储任何内部 Git 对象的哈希 ID .如果一个标签存储了提交哈希 ID,则称其为 lightweight 标签,如果存储标签的哈希 ID,则称其为 annotated 标签目的。然后,此标记对象通常存储某些提交的哈希 ID。

您可以使用 git branch --points-at 查找 select 某个特定提交的分支名称,并使用 git tag --points-at 查找 select 某个特定提交的标签名称。有关详细信息,请参阅 git branch and git tag 文档。

请注意,您可以在此处给 git tag commit 哈希 ID,它仍然会找到带注释的标签。从技术上讲,无论您是命名标签对象本身还是它的目标提交,它都会找到带注释的标签:

$ git rev-parse v2.3.0
42de6ed0c4c5c2a184b25ffeb4936af8226ccad1
$ git rev-parse v2.3.0^{commit}
9874fca7122563e28d699a911404fc49d2a24f1c
$ git tag --points-at 42de6ed0c4c5c2a184b25ffeb4936af8226ccad1
v2.3.0
$ git tag --points-at 9874fca7122563e28d699a911404fc49d2a24f1c
v2.3.0

但是,如果您要查找哪些 分支 名称(可能有 none 到许多),请确定一些特定的 commit 你通过标签找到的,你肯定需要先将标签名称解析为提交哈希 ID,然后再 运行 git branch --points-at.

尽管如此,我觉得你的总体计划似乎毫无根据。请记住,标签名称目的 是始终标识一个特定的提交。原始哈希 ID 也可以做到这一点,但标签名称提供了原始哈希 ID 没有的两个重要特征:

  • 它至少是人类可读的,并且可以具有语义,例如 semver 部分。

  • 它使您能够 签署 提交而无需实际签署每个提交。也就是说,您可以为 tagged 提交提供 GPG 或其他数字签名,而不必在每次提交时都使用 GPG 或其他数字签名(这在大多数情况下弊大于利:仅签署标签,虽然在某种意义上不太安全,但更 可用 ,获得有效安全性的关键是平衡痛苦程度与完成工作水平,因此人们实际上使用它)。

同时,分支名称的目的是允许某些东西——人类、计算机,有时两者——找到一些特定的但 可变 提交。因此,我们为某个提交链的 tip commit 分配了一个分支名称,也就是说 这是我们迄今为止最好的提交 。然后,随着我们的改进,我们将 new 提交添加到链中,并 移动分支名称 .

这对于正式发布流程意味着您不会标记发布候选,除非它面向更广泛的受众.您可以使用 分支名称 标记此候选发布版。然后你会构建它并测试它,如果它通过了内部测试,你可以 then 将它标记为候选发布,也许 after 更新一些某种构建定制。

换句话说,您永远不会自定义标记版本。您总是从 branch 进行自定义——也许是特定的分支模式,但是一个分支。然后您可以标记自定义版本,前提是它已准备好候选。如果它足够好,它会被授权为经过签名和验证的发布或“广泛的 RC”(在本地组织之外的东西)(这样组织外的人就可以看到它是可信的,以及被谁信任)。当然,无论谁添加签名,都应该确保信任链——每一步所做的验证——是有效的和完整的。