admin管理员组

文章数量:1289382

I have two GitHub workflows working together to automate cluster list updates in my repository:

  1. cluster-list.yaml workflow

    This workflow runs every 3 hours and:

    1. Retrieves a list of all clusters from a tool (ArgoCD).

    2. Compares it with the existing clusters-list.yaml file.

    3. If there are differences, it:

      • Creates a new branch.

      • Updates clusters-list.yaml.

      • Creates a pull request (PR) with the changes.

      • Labels the PR with "automerge".

    name: Check for new cluster
    
    on:
      workflow_dispatch:
      schedule:
        - cron: '0 */3 * * *'
    
    permissions:
      contents: write
      pull-requests: write
    
    jobs:
      check-for-new-cluster:
        runs-on: ubuntu-latest
        steps:
          - name: Check for new cluster
            uses: actions/checkout@v4
            with:
              ref: main
    
          - name: Install ArgoCD CLI
            run: |
              curl -sSL -o argocd-linux-amd64 
              sudo install -m 555 argocd-linux-amd64 /usr/local/bin/argocd
              sudo rm argocd-linux-amd64
    
          - name: Login to ArgoCD
            run: argocd login XXXXXX --username ${{ secrets.ARGOCD_USER }} --password ${{ secrets.ARGOCD_PW }} --insecure
    
          - name: Get clusters list
            run: |
              cd ./helm-charts
              argocd cluster list -o json | jq 'map({name, labels, annotations, shortName: (.name | gsub("-k8s"; ""))}) | sort_by(.name)' | yq eval '{"argocdClusters": .}' -P > clusters-list-new.yaml
    
          - name: Compare cluster list
            run: |
              cd ./helm-charts
              diff -u clusters-list.yaml clusters-list-new.yaml > clusters-list.diff || true
              if [ -s clusters-list.diff ]
              then
                echo "Cluster list has changed"
                cat clusters-list.diff
                branchname=$(echo "maint/update-clusters-list-$(date +'%Y%m%d%H%M%S')")
                cp clusters-list-new.yaml clusters-list.yaml
                git config user.name github-actions
                git config user.email [email protected]
                git checkout -b $branchname
                git add clusters-list.yaml
                git commit -m "JOP-000: Update clusters-list.yaml"
                git push origin $branchname
                gh pr create -B main -H $branchname -t "JOP-000: Update clusters-list.yaml" -b "JOP-000: Update clusters-list.yaml" --label automerge
              else
                echo "Cluster list has not changed"
              fi
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    
  2. automerge.yaml workflow

    This workflow should automatically merge PRs under specific conditions. It triggers on PR events (labeled, synchronize, opened, ready_for_review, and review_submission) and:

    • If only helm-charts/clusters-list.yaml is modified, it merges the PR without requiring review.
    • If other files are modified, it requires an approved review before merging.
    name: Automerge
    
    on:
      pull_request_target:
        types:
          - labeled
          - synchronize
          - opened
          - ready_for_review
      pull_request_review:
         types:
          - submitted
    
    permissions:
      contents: write
      pull-requests: write
    
    
    jobs:
      automerge:
        runs-on: ubuntu-latest
        steps:
          - name: Checkout
            uses: actions/checkout@v4
            with:
              ref: ${{ github.event.pull_request.head.ref }}
    
          - name: Check if checks passed and pr is correctly labeled
            run: |
              git config user.name github-actions
              git config user.email [email protected]
    
              # check if the PR is labeled automerge
              if ! gh pr view ${{ github.event.number }} --json labels --jq '.labels[].name' | grep -q automerge; then
                echo "PR is not labeled automerge, exiting"
                exit 0
              fi
              echo "PR is labeled automerge"
    
              # Get list of modified files
              MODIFIED_FILES=$(gh pr view ${{ github.event.number }} --json files --jq '.files[].path')
    
              # Check if only clusters-list.yaml in helm-charts is modified
              if [ "$(echo "$MODIFIED_FILES" | wc -l)" -eq 1 ] && [ "$MODIFIED_FILES" = "helm-charts/clusters-list.yaml" ]; then
                echo "Only helm-charts/clusters-list.yaml is modified, proceeding without review"
              else
                # For other files, check for review approval
                if ! gh pr view ${{ github.event.number }} --json reviews --jq '.reviews[].state' | grep -iq approved; then
                  echo "PR modifies other files and does not have an approved review, exiting"
                  exit 0
                fi
                echo "PR has an approved review"
              fi
    
              #merge
              gh pr merge ${{ github.event.number }} --auto --squash --delete-branch
            env:
              GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    

How the two workflows should work together:

  1. cluster-list.yaml detects new clusters and creates a PR with updates.

  2. This PR is labeled "automerge" by the workflow.

  3. automerge.yaml should trigger on this PR and automatically merge it (since it only modifies clusters-list.yaml).

  4. Other PRs (modifying different files) should still require review before merging.

The cluster-list.yaml workflow successfully creates the PR and applies the "automerge" label. However, the automerge.yaml workflow never runs for this PR, and the merge does not happen automatically.

I've:

  1. Confirmed that the automerge.yaml workflow is correctly configured to trigger on PR events.

  2. Manually checked that the PR is labeled "automerge."

  3. Verified that only helm-charts/clusters-list.yaml is modified in the PR.

Has anyone encountered a similar issue with GitHub Actions? How can I debug why the automerge.yaml workflow does not trigger for the PRs created by cluster-list.yaml?

本文标签: kubernetesAutomerge workflow not triggering on PRsStack Overflow