From 833fec5b6d802ce57204945d51357de3d376b773 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Steenis Date: Thu, 16 Nov 2023 12:22:24 +0100 Subject: [PATCH] Simplify update README workflow --- .github/workflows/update-readme.yml | 43 +++++++++++------------------ 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/.github/workflows/update-readme.yml b/.github/workflows/update-readme.yml index a90ca472..d60e9aca 100644 --- a/.github/workflows/update-readme.yml +++ b/.github/workflows/update-readme.yml @@ -1,5 +1,8 @@ name: Update README -on: workflow_dispatch +on: + workflow_dispatch: + release: + types: [published] permissions: contents: write @@ -9,7 +12,7 @@ jobs: update-readme: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 - name: Update README file run: | tagstmpfile=$(mktemp) @@ -40,7 +43,7 @@ jobs: id: branch run: | BRANCH="githubaction-update-readme-$(date +%Y-%m-%d-%H-%M-%S)" - echo "::set-output name=branch::$BRANCH" + echo "branch=${BRANCH}" >> $GITHUB_OUTPUT git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" git config --global user.name "github-actions[bot]" git checkout -b "$BRANCH" @@ -51,28 +54,14 @@ jobs: id: cpr env: SOURCE_BRANCH: ${{ steps.branch.outputs.branch }} - uses: actions/github-script@v5.0.0 - with: - github-token: ${{ secrets.GITHUB_TOKEN }} - script: | - let body = 'Auto-generated by GitHub Actions' - - const { data: pr } = await github.rest.pulls.create({ - title: `[${{ github.ref_name }}] update README with latest`, - body: body, - owner: context.repo.owner, - repo: context.repo.repo, - base: "${{ github.ref_name }}", - head: `${ process.env.SOURCE_BRANCH }` - }); - await github.rest.issues.addLabels({ - ...context.repo, - issue_number: pr.number, - labels: ["status/auto-created"], - }); - console.log('Created new pull request'); - return pr.html_url; - - name: Check outputs - if: ${{ env.changes_exist == 'true' }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | - echo "Pull Request URL - ${{ steps.cpr.outputs.result }}" + PR_TITLE="[${GITHUB_REF_NAME}] update README with latest" + PR_BODY="Auto-generated by GitHub Actions" + EXISTING_PR=$(gh pr list --limit 500 --json title,url | jq --arg title "${PR_TITLE}" -r '.[] | select(.title==$title) | .url') + CREATED_PR=$(gh pr create --title "${PR_TITLE}" --body "${PR_BODY}" --label "status/auto-created" --base "${GITHUB_REF_NAME}" --head "${SOURCE_BRANCH}") + echo "Created pull request: ${CREATED_PR}" >> $GITHUB_STEP_SUMMARY + if [ -n "${EXISTING_PR}" ]; then + gh pr close --comment "Superseded by ${CREATED_PR}" --delete-branch "${EXISTING_PR}" + echo "Closed previous pull request: ${EXISTING_PR}" >> $GITHUB_STEP_SUMMARY + fi