From 9165a41c29aa5410b01938fe457d7c54f906d70f Mon Sep 17 00:00:00 2001 From: Tim Man Date: Thu, 11 Jan 2024 18:41:28 +0800 Subject: [PATCH] Chore/refactor workflows (#743) * chore: trigger create rc release on PR to main * chore: move release asset uploads for rc to build * chore: split workflows into 2 for build and 1 for release * chore: remove extra build deps step * chore: add back missing env vars * chore: comments and remove unnecessary conditionals * chore: add git checkout where needed * chore: clean up and remove create release step since it was moved * chore: clean up workflow file * chore: more cleanup * chore: minor naming * chore: minor naming * chore: minot naming * chore: more naming --- .github/workflows/build-rc.yml | 132 ++++++++++++++++++++++++ .github/workflows/build.yml | 66 ++++++------ .github/workflows/create-release-pr.yml | 13 ++- .github/workflows/release.yml | 98 ++++++++++-------- scripts/.gitignore | 1 + scripts/create-release-pr.sh | 35 ++----- scripts/find-tag.sh | 29 ++++++ 7 files changed, 269 insertions(+), 105 deletions(-) create mode 100644 .github/workflows/build-rc.yml create mode 100755 scripts/find-tag.sh diff --git a/.github/workflows/build-rc.yml b/.github/workflows/build-rc.yml new file mode 100644 index 000000000..cfe31363e --- /dev/null +++ b/.github/workflows/build-rc.yml @@ -0,0 +1,132 @@ +name: Build & Publish release candidate +## +# This workflow builds new release candidates (create release + upload asset): +# - for a new release PR and +# - for every push to the release PR head branch +# +# It should also keep the release PR description in sync with the latest release candidate +# +on: + pull_request: + branches: + - main + - develop +jobs: + test: + if: ${{ startsWith(github.head_ref, 'release/') }} + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + always-auth: true + node-version: 18 + registry-url: https://npm.pkg.github.com + scope: '@secretkeylabs' + cache: npm + - name: Install dependencies + env: + NODE_AUTH_TOKEN: ${{ secrets.GH_PACKAGE_REGISTRY_TOKEN }} + run: npm ci + - name: Test + run: | + npx eslint . + npx tsc --noEmit + npm test + publish-rc: + # TODO also keep the develop PR description up to date + if: ${{ github.base_ref == 'main' }} + needs: test + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + issues: write + env: + GH_TOKEN: ${{ github.token }} + outputs: + upload_url: ${{ steps.publish-prerelease.outputs.UPLOAD_URL }} + filename: ${{ steps.publish-prerelease.outputs.FILENAME }} + steps: + - uses: actions/checkout@v4 + - id: publish-prerelease + name: Publish release candidate as prerelease + env: + SOURCE_BRANCH: ${{ github.head_ref }} + TARGET_COMMITISH: ${{ github.event.pull_request.head.sha }} + run: | + # find the next rc tag + gh api \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/{owner}/{repo}/releases > releases.json + # get $TAG from branch name, e.g. v0.25.0 + TAG=$(echo $SOURCE_BRANCH | sed 's/release\/\(.*\)/\1/') + # export $NEXT_TAG using releases.json and $TAG, e.g. v0.25.0-rc.0 + cd scripts + ./find-tag.sh + # publish the release as prerelease rc + gh api \ + --method POST \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/{owner}/{repo}/releases \ + -f tag_name="$NEXT_TAG" \ + -f target_commitish="$TARGET_COMMITISH" \ + -f name="$NEXT_TAG" \ + -F draft=false \ + -F prerelease=true \ + -F generate_release_notes=true > release.json + # save output for upload + echo "FILENAME=xverse-web-extension.$NEXT_TAG.zip" >> $GITHUB_OUTPUT + echo "UPLOAD_URL=$(cat release.json | jq -r .upload_url)" >> $GITHUB_OUTPUT + - id: update-description + name: Update PR description with release notes + env: + PR_ID: ${{ github.event.pull_request.number }} + run: | + # update PR description + cat release.json | jq -r .body > body.md + echo -e "\n\nRelease candidate: $(cat release.json | jq -r .html_url)" >> body.md + echo -e "\nTo publish this rc as latest: Merge Commit this PR" >> body.md + gh api \ + --method PATCH \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/{owner}/{repo}/pulls/$PR_ID \ + -F 'body=@body.md' + build-rc: + needs: publish-rc + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 + with: + always-auth: true + node-version: 18 + registry-url: https://npm.pkg.github.com + scope: '@secretkeylabs' + cache: npm + - name: Install dependencies + env: + NODE_AUTH_TOKEN: ${{ secrets.GH_PACKAGE_REGISTRY_TOKEN }} + run: npm ci + - name: Build & zip + env: + TRANSAC_API_KEY: ${{ secrets.TRANSAC_API_KEY }} + MOON_PAY_API_KEY: ${{ secrets.MOON_PAY_API_KEY }} + MIX_PANEL_TOKEN: ${{ secrets.MIX_PANEL_TOKEN }} + run: | + npm run build + zip -rj build.zip ./build + - name: Upload release asset + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + UPLOAD_URL: ${{needs.publish-rc.outputs.upload_url}} + FILENAME: ${{needs.publish-rc.outputs.filename}} + with: + upload_url: $UPLOAD_URL + asset_path: build.zip + asset_name: $FILENAME + asset_content_type: application/zip diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 2faef5aa0..c6ab6ce9d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -1,11 +1,16 @@ -name: Build - +name: Build & Test for feature PR +## +# This workflow tests, builds, and uploads the extension code for each PR +# +# It should also keep an updated comment on the PR showing where the upload is +# on: pull_request: - branches: [main, develop] - + branches: + - develop jobs: build: + if: ${{ !startsWith(github.head_ref, 'release/') }} runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -13,9 +18,10 @@ jobs: uses: actions/setup-node@v4 with: always-auth: true - node-version: '18.x' + node-version: 18 registry-url: https://npm.pkg.github.com scope: '@secretkeylabs' + cache: npm - name: Install dependencies env: NODE_AUTH_TOKEN: ${{ secrets.GH_PACKAGE_REGISTRY_TOKEN }} @@ -34,7 +40,7 @@ jobs: - name: Save Filename run: | BRANCH_NAME=$(echo ${{ github.head_ref }} | sed 's/\//-/g') - GIT_SHA_SHORT=$(git rev-parse --short ${{ github.sha }}) + GIT_SHA_SHORT=$(git rev-parse --short ${{ github.event.pull_request.head.sha }}) echo "FILENAME=xverse-extension.$BRANCH_NAME.$GIT_SHA_SHORT" >> $GITHUB_ENV - name: Upload Archive uses: actions/upload-artifact@v3 @@ -46,8 +52,14 @@ jobs: comment-on-pr: needs: build runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + env: + GH_TOKEN: ${{ github.token }} steps: - - name: Get Artifact URL + - uses: actions/checkout@v4 + - name: Get artifact URL env: OWNER: ${{ github.repository_owner }} REPO: ${{ github.event.repository.name }} @@ -56,33 +68,25 @@ jobs: ARTIFACT_URL="https://github.com/$OWNER/$REPO/actions/runs/$WORKFLOW_ID" echo "ARTIFACT_URL=$ARTIFACT_URL" >> $GITHUB_ENV - name: Delete old bot comments - if: ${{ github.event_name == 'pull_request' }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} PR_ID: ${{ github.event.pull_request.number }} - REPO: ${{ github.repository }} run: | - curl \ - -H "Accept: application/vnd.github.v3+json" \ - -H "Authorization: token $GITHUB_TOKEN" \ - https://api.github.com/repos/$REPO/issues/$PR_ID/comments \ + gh api \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/{owner}/{repo}/issues/$PR_ID/comments \ | jq ".[] | select(.user.login==\"github-actions[bot]\") | .id" \ - | xargs -I %q curl \ - -L \ - -X DELETE \ - -H "Accept: application/vnd.github.v3+json" \ - -H "Authorization: token $GITHUB_TOKEN"\ - https://api.github.com/repos/$REPO/issues/comments/%q + | xargs -I %q gh api \ + --method DELETE \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/{owner}/{repo}/issues/comments/%q - name: Post test package PR comment - if: ${{ github.event_name == 'pull_request' }} env: - VERSION: ${{ steps.published-version.outputs.version }} - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - GITHUB_URL: ${{ github.event.pull_request.comments_url }} + PR_ID: ${{ github.event.pull_request.number }} run: | - curl \ - -X POST \ - -H "Accept: application/vnd.github.v3+json" \ - -H "Authorization: token $GITHUB_TOKEN" \ - $GITHUB_URL \ - -d "{\"body\":\"> Test with build here: ${{ env.ARTIFACT_URL }}\"}" + gh api \ + --method POST \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/{owner}/{repo}/issues/$PR_ID/comments \ + -f body="Test with build here: $ARTIFACT_URL" diff --git a/.github/workflows/create-release-pr.yml b/.github/workflows/create-release-pr.yml index 3b84416ef..b0d85aed2 100644 --- a/.github/workflows/create-release-pr.yml +++ b/.github/workflows/create-release-pr.yml @@ -1,5 +1,9 @@ -name: Create release PR - +name: Create release PRs +## +# This workflow initiates the release process (create release PRs): +# - creates a release branch with version bump +# - creates a release PR to main & develop +# on: workflow_dispatch: inputs: @@ -12,7 +16,6 @@ on: - patch - minor - major - jobs: create-release-pr: runs-on: ubuntu-latest @@ -21,8 +24,8 @@ jobs: pull-requests: write issues: write steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: 18 cache: npm diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 547fe3bed..90a4247f8 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,50 +1,66 @@ -name: Release - +name: Publish latest release +## +# This workflow creates a latest release with the same target_commitish +# as the highest rc matching the release PR version +# +# It should also update the release PR description +# It should also attach the highest rc asset to the latest release +# on: - release: - types: [created] - + pull_request: + branches: + - main + types: + - closed jobs: - build: + publish-latest: + if: ${{ github.event.pull_request.merged == true && startsWith(github.head_ref, 'release/')}} runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: write + issues: write + env: + GH_TOKEN: ${{ github.token }} steps: - uses: actions/checkout@v4 - - name: Use Node.js - uses: actions/setup-node@v4 - with: - always-auth: true - node-version: '18.x' - registry-url: https://npm.pkg.github.com - scope: '@secretkeylabs' - - name: Install dependencies + - id: create-latest-release + name: Create latest release env: - NODE_AUTH_TOKEN: ${{ secrets.GH_PACKAGE_REGISTRY_TOKEN }} - run: npm ci - - name: Test + SOURCE_BRANCH: ${{ github.head_ref }} run: | - npx eslint . - npx tsc --noEmit - npm test - - name: Build + # find the target commitish of the latest release matching our tag + TAG=$(echo $SOURCE_BRANCH | sed 's/release\/\(.*\)/\1/') + gh api \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/{owner}/{repo}/releases > releases.json + TARGET_COMMITISH=$(cat releases.json | jq '.[] | select(.tag_name | match("$TAG")) | .target_commitish' | head -1) + # publish the latest release + gh api \ + --method POST \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/{owner}/{repo}/releases \ + -f tag_name="$TAG" \ + -f target_commitish="$TARGET_COMMITISH" \ + -f name="$TAG" \ + -F generate_release_notes=true > release.json + # TODO attach the rc asset to latest release + # save output for upload + # echo "FILENAME=xverse-web-extension.$TAG.zip" >> $GITHUB_OUTPUT + # echo "UPLOAD_URL=$(cat release.json | jq -r .upload_url)" >> $GITHUB_OUTPUT + - id: update-description + name: Update PR description with release notes env: - TRANSAC_API_KEY: ${{ secrets.TRANSAC_API_KEY }} - MOON_PAY_API_KEY: ${{ secrets.MOON_PAY_API_KEY }} - MIX_PANEL_TOKEN: ${{ secrets.MIX_PANEL_TOKEN }} - run: npm run build --if-present - - name: Save Filename - id: save-filename + PR_ID: ${{ github.event.pull_request.number }} run: | - echo "FILENAME=xverse-extension.$(echo ${{github.ref_name}}| sed 's/\//-/').zip" >> $GITHUB_OUTPUT - - name: Create Archive - run: | - zip -rj build.zip ./build - - name: Upload Release Asset - if: ${{ github.event.release.upload_url }} - uses: actions/upload-release-asset@v1 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - with: - upload_url: ${{ github.event.release.upload_url }} - asset_path: build.zip - asset_name: ${{ steps.save-filename.outputs.FILENAME }} - asset_content_type: application/zip + # update PR description + cat release.json | jq -r .body > body.md + echo -e "\n\nPublished latest release: $(cat release.json | jq -r .html_url)" >> body.md + gh api \ + --method PATCH \ + -H "Accept: application/vnd.github+json" \ + -H "X-GitHub-Api-Version: 2022-11-28" \ + /repos/{owner}/{repo}/pulls/$PR_ID \ + -F 'body=@body.md' diff --git a/scripts/.gitignore b/scripts/.gitignore index 62c82f717..3f4dae6b5 100644 --- a/scripts/.gitignore +++ b/scripts/.gitignore @@ -1,3 +1,4 @@ release.json pr-*.json body.md +releases.json diff --git a/scripts/create-release-pr.sh b/scripts/create-release-pr.sh index 84e3721d7..267c41b0d 100755 --- a/scripts/create-release-pr.sh +++ b/scripts/create-release-pr.sh @@ -1,5 +1,12 @@ #! /bin/bash +## +# create-release-pr.sh for xverse-web-extension +# +# NOTE: make sure you git commit your work before running this locally. +# Alternatively trigger it from the github action +# + if [[ -z "$BUMP" ]]; then echo "BUMP is required. major|minor|patch" exit 1 @@ -23,23 +30,6 @@ git merge origin/main -s ours git push --set-upstream origin $BRANCH -echo -e "\n--- Create draft release for $TAG ---" - -gh api \ - --method POST \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - /repos/{owner}/{repo}/releases \ - -f tag_name=$TAG \ - -f target_commitish="$BRANCH" \ - -f name=$TAG \ - -F draft=true \ - -F prerelease=true \ - -F generate_release_notes=true > release.json - -cat release.json | jq -r .body > body.md -echo -e "\n\nDraft release: $(cat release.json | jq -r .html_url)" >> body.md - for b in main develop; do echo -e "\n--- Create PR to $b ---" @@ -53,17 +43,6 @@ for b in main develop; do -f head="$BRANCH" \ -f base="$b" > pr-$b.json - echo -e "\n--- Update PR to $b with description ---" - - PR_ID=$(cat pr-$b.json | jq -r .number) - - gh api \ - --method PATCH \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - /repos/{owner}/{repo}/pulls/$PR_ID \ - -F 'body=@body.md' - # clean up temp files # rm pr-$b.json done diff --git a/scripts/find-tag.sh b/scripts/find-tag.sh new file mode 100755 index 000000000..c190ca360 --- /dev/null +++ b/scripts/find-tag.sh @@ -0,0 +1,29 @@ +#! /bin/bash + +## +# find-tag.sh +# +# a util for looking through a list of github releases and exporting the next tag +# +if [[ -z "$TAG" ]]; then + echo "TAG is required. e.g. v0.26.0" + exit 1 +fi + +if cat releases.json | jq '.[].tag_name' | grep $TAG; then + echo found releases matching $TAG + LATEST_TAG=$(cat releases.json | jq '.[].tag_name' | grep $TAG | head -1) + LATEST_RC=$(echo $LATEST_TAG | grep rc | sed 's/.*-rc\(.*\)/\1/') + if [[ -z "$LATEST_RC" ]]; then + echo $TAG was already released + exit 1; + elif [[ -n "$LATEST_RC" ]]; then + NEXT_TAG="$TAG-rc.$($LATEST_RC +1)" + fi +else + echo no releases matching $TAG yet + NEXT_TAG="$TAG-rc.0" +fi + +echo next tag will be $NEXT_TAG +export NEXT_TAG=$NEXT_TAG