From 5e3086796a3cbc01308bc2d00d0bbe2c14dff6f9 Mon Sep 17 00:00:00 2001 From: Huaichao Wang Date: Tue, 27 Jun 2023 18:28:57 +0800 Subject: [PATCH] refactor: create PR and merge to main after release --- .github/workflows/integration.yml | 2 +- .github/workflows/release.yml | 38 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index ea61150..503126d 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -1,5 +1,5 @@ name: Integration -run-name: ${{ github.actor }} is testing out GitHub Actions 🚀 +run-name: ${{ github.actor }} is testing on ${{ github.ref_name }} with ${{ github.sha }} on: [ push ] concurrency: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 10a053f..8e0364b 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,6 +1,7 @@ # reference: https://semantic-release.gitbook.io/semantic-release/recipes/ci-configurations/github-actions name: Release +run-name: ${{ github.actor }} is performing release on ${{ github.ref_name }} with ${{ github.sha }} 🚀 on: [ workflow_dispatch ] permissions: @@ -16,6 +17,12 @@ jobs: pull-requests: write # to be able to comment on released pull requests id-token: write # to enable use of OIDC for npm provenance steps: + - name: Ensure the branch is 'release/main' + if: github.ref_name != 'release/main' + run: | + echo "release can only run on branch release/main" + exit 1 + - name: Checkout uses: actions/checkout@v3 with: @@ -37,3 +44,34 @@ jobs: GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} run: npx semantic-release + + # reference: + # * https://docs.github.com/en/rest/pulls/pulls?apiVersion=2022-11-28#create-a-pull-request + # * https://docs.github.com/en/rest/guides/getting-started-with-the-rest-api?apiVersion=2022-11-28#authentication-example-for-github-actions + # * https://docs.github.com/en/actions/using-workflows/using-github-cli-in-workflows + # grant PR permission to GitHub Actions: `Allow GitHub Actions to create and approve pull requests` + # * https://github.com/huaichaow/marked-toc-extension/settings/actions + - name: Create PR to main branch + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + echo "Creating PR" + + pr_number="$(gh api \ + --method POST \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/huaichaow/marked-toc-extension/pulls \ + -f title='regular release' \ + -f body='Please pull these release!' \ + -f head='release/main' \ + -f base='main' --jq '.number')" + + echo "Merging PR ${pr_number}" + + gh api \ + --method PUT \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/huaichaow/marked-toc-extension/pulls/${pr_number}/merge \ + -f merge_method=rebase