From 980f91f7cdd26c0224219a4232cde172484ca8ba Mon Sep 17 00:00:00 2001 From: Daniel Roux Date: Tue, 7 May 2024 14:44:06 +0200 Subject: [PATCH] Update gha workflows to fix .changelog logic Trigger k8s after a .changelog entry was for dependabot's PRs --- .github/workflows/.dependabot_misc.yml | 46 --------------- .github/workflows/changelog.yml | 81 ++++++++++++++++++++++++-- .github/workflows/k8s.yml | 19 +++--- 3 files changed, 89 insertions(+), 57 deletions(-) delete mode 100644 .github/workflows/.dependabot_misc.yml diff --git a/.github/workflows/.dependabot_misc.yml b/.github/workflows/.dependabot_misc.yml deleted file mode 100644 index 79fce8cf..00000000 --- a/.github/workflows/.dependabot_misc.yml +++ /dev/null @@ -1,46 +0,0 @@ -name: Dependabot Release Notes - -on: - pull_request: - types: [opened, synchronize, reopened] - -# https://docs.github.com/en/actions/using-jobs/assigning-permissions-to-jobs#defining-access-for-the-github_token-scopes -permissions: - pull-requests: write - contents: write - -jobs: - generate-release-note: - if: github.actor == 'dependabot[bot]' - runs-on: ubuntu-latest - - steps: - - name: Checkout - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 - with: - fetch-depth: 0 - - name: Generate .changelog entry - env: - PR_NUMBER: ${{ github.event.pull_request.number }} - PR_TITLE: ${{ github.event.pull_request.title }} - PR_BODY: ${{ github.event.pull_request.body_text }} - run: | - FIRST_COMMIT=$(git log -10 --pretty=format:%H origin/main..HEAD | tail -1) - FIRST_COMMIT_BODY=$(git log -1 --pretty=format:%B $FIRST_COMMIT) - mkdir -p .changelog - echo "backticksrelease-note:enhancement" > .changelog/$PR_NUMBER.txt - echo "ci/dependabot: $PR_TITLE" >> .changelog/$PR_NUMBER.txt - echo "" >> .changelog/$PR_NUMBER.txt - echo "$FIRST_COMMIT_BODY" >> .changelog/$PR_NUMBER.txt - echo "backticks" >> .changelog/$PR_NUMBER.txt - sed -i 's/backticks/```/g' .changelog/$PR_NUMBER.txt - - name: Commit and Push - env: - GIT_USER: ${{ secrets.GIT_USER }} - GIT_TOKEN: ${{ secrets.GIT_TOKEN }} - run: | - git config --local user.email "dependabot[bot]@users.noreply.github.com" - git config --local user.name "dependabot[bot]" - git add .changelog - git commit --amend --no-edit - git push --force "https://x-access-token:$GITHUB_TOKEN@github.com/${{ github.repository }}.git" HEAD:${{ github.event.pull_request.head.ref }} diff --git a/.github/workflows/changelog.yml b/.github/workflows/changelog.yml index 7fdc7ee5..f004a9f9 100644 --- a/.github/workflows/changelog.yml +++ b/.github/workflows/changelog.yml @@ -4,22 +4,95 @@ name: Changelog Entry on: pull_request: - types: [opened, synchronize, labeled] + types: [opened, synchronize, labeled, reopened] # Runs on PRs to main and all release branches branches: - main +permissions: + pull-requests: write + contents: write + jobs: + generate-release-note: + if: github.actor == 'dependabot[bot]' + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 + with: + fetch-depth: 0 + ref: ${{ github.event.pull_request.head.ref }} + - name: Generate .changelog entry + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + PR_TITLE: ${{ github.event.pull_request.title }} + PR_BODY: ${{ github.event.pull_request.body_text }} + run: | + FIRST_COMMIT=$(git log -10 --pretty=format:%H origin/main..HEAD | tail -1) + FIRST_COMMIT_BODY=$(git log -1 --pretty=format:%B $FIRST_COMMIT) + mkdir -p .changelog + echo "backticksrelease-note:enhancement" > .changelog/$PR_NUMBER.txt + echo "ci/dependabot: $PR_TITLE" >> .changelog/$PR_NUMBER.txt + echo "" >> .changelog/$PR_NUMBER.txt + echo "$FIRST_COMMIT_BODY" >> .changelog/$PR_NUMBER.txt + echo "backticks" >> .changelog/$PR_NUMBER.txt + sed -i 's/backticks/```/g' .changelog/$PR_NUMBER.txt + - name: Commit and Push + env: + GIT_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_TOKEN: ${{ secrets.GCR_SK8L_CR_PAT }} + run: | + echo $GIT_USER + echo "---x" + echo $GIT_TOKEN + echo "x---" + echo $GH_TOKEN + echo "x---x" + git branch + echo "---" + git log -1 + echo "---" + git config --local user.email "dependabot[bot]@users.noreply.github.com" + git config --local user.name "dependabot[bot]" + git add .changelog + git commit --amend --no-edit + echo "---" + git log -1 + git push --force "https://x-access-token:$GITHUB_TOKEN@github.com/${{ github.repository }}.git" HEAD:${{ github.event.pull_request.head.ref }} + echo '{"pull_request_number": ${{ github.event.pull_request.number }}}' | gh workflow run k8s.yml --ref ${{ github.event.pull_request.head.ref }} --json - # checks that a .changelog entry is present for a PR + changelog-check-dpbot: + runs-on: ubuntu-latest + needs: [generate-release-note] + steps: + - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 + with: + # ref: ${{ github.event.pull_request.head.sha }} + ref: ${{ github.event.pull_request.head.ref }} + fetch-depth: 0 # by default the checkout action doesn't checkout all branches + - name: Check for changelog entry in diff + run: | + # check if there is a diff in the .changelog directory + changelog_files=$(git --no-pager diff --name-only HEAD "$(git merge-base HEAD "origin/${{ github.event.pull_request.base.ref }}")" -- .changelog/${{ github.event.pull_request.number }}.txt) + + # If we do not find a file in .changelog/, we fail the check + if [ -z "$changelog_files" ]; then + # Fail status check when no .changelog entry was found on the PR + echo "Did not find a .changelog entry and the 'pr/no-changelog' label was not applied." + exit 1 + else + echo "Found .changelog entry in PR!" + fi changelog-check: # If there a `pr/no-changelog` label we ignore this check - if: "!contains(github.event.pull_request.labels.*.name, 'pr/no-changelog') || github.event.pull_request.user.login == 'dependabot'" + if: "!contains(github.event.pull_request.labels.*.name, 'pr/no-changelog')" runs-on: ubuntu-latest - steps: - uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4 with: - ref: ${{ github.event.pull_request.head.sha }} + # ref: ${{ github.event.pull_request.head.sha }} + ref: ${{ github.event.pull_request.head.ref }} fetch-depth: 0 # by default the checkout action doesn't checkout all branches - name: Check for changelog entry in diff run: | diff --git a/.github/workflows/k8s.yml b/.github/workflows/k8s.yml index 9ebfbf27..7e67daa6 100644 --- a/.github/workflows/k8s.yml +++ b/.github/workflows/k8s.yml @@ -2,6 +2,11 @@ name: k8s on: pull_request: workflow_dispatch: + workflow_call: + inputs: + pull_request_number: + required: true + type: number env: GO_VERSION: "1.22.2" @@ -11,22 +16,22 @@ permissions: jobs: docker-img: - name: Build Docker image dev-${{ github.event.pull_request.number }} + name: Build Docker image dev-${{ inputs.pull_request_number || github.event.pull_request.number }} uses: ./.github/workflows/docker.yml - if: ${{ github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch' }} + if: ${{ (inputs.pull_request_number != null && inputs.pull_request_number != '') || github.event_name == 'workflow_dispatch' }} strategy: matrix: k8s: - image: "kindest/node:v1.29.2@sha256:51a1434a5397193442f0be2a297b488b6c919ce8a3931be0ce822606ea5ca245" version: v1.29.2 with: - image_tag: ${{ github.event.pull_request.number }}-${{ matrix.k8s.version }} - pull_request_number: ${{ github.event.pull_request.number }} + image_tag: ${{ inputs.pull_request_number || github.event.pull_request.number }}-${{ matrix.k8s.version }} + pull_request_number: ${{ inputs.pull_request_number || github.event.pull_request.number }} k8s_version: ${{ matrix.k8s.version }} k8s_image: ${{ matrix.k8s.image }} secrets: inherit k8s-tests: - name: k8s ${{ matrix.k8s.version }}/pr#${{ github.event.pull_request.number }} tests + name: k8s ${{ matrix.k8s.version }}/pr#${{ inputs.pull_request_number || github.event.pull_request.number }} tests needs: [docker-img] strategy: matrix: @@ -43,8 +48,8 @@ jobs: - version: "v0.22.0" uses: ./.github/workflows/k8s-test.yml with: - image_tag: ${{ github.event.pull_request.number }}-${{ matrix.k8s.version }} - pull_request_number: ${{ github.event.pull_request.number }} + image_tag: ${{ inputs.pull_request_number || github.event.pull_request.number }}-${{ matrix.k8s.version }} + pull_request_number: ${{ inputs.pull_request_number || github.event.pull_request.number }} kind_version: ${{ matrix.kind.version }} k8s_version: ${{ matrix.k8s.version }} k8s_image: ${{ matrix.k8s.image }}