From d501539f8e26fd0c01abe6186d30d797c7d114fa Mon Sep 17 00:00:00 2001 From: Christopher Schleiden Date: Thu, 23 Mar 2023 11:18:08 -0700 Subject: [PATCH] Split release/publish workflos --- .github/workflows/publish-npm.yml | 45 ------------- .github/workflows/publish.yml | 51 ++++++++++++++ .github/workflows/release.yml | 106 ++++++++++++++++++++++++++++++ 3 files changed, 157 insertions(+), 45 deletions(-) delete mode 100644 .github/workflows/publish-npm.yml create mode 100644 .github/workflows/publish.yml create mode 100644 .github/workflows/release.yml diff --git a/.github/workflows/publish-npm.yml b/.github/workflows/publish-npm.yml deleted file mode 100644 index 580290e..0000000 --- a/.github/workflows/publish-npm.yml +++ /dev/null @@ -1,45 +0,0 @@ -name: Publish npm packages - -on: - # For now auto-publish every change to the default branch - push: - branches: [main] - workflow_dispatch: - inputs: - bump: - description: How to increase the version - type: choice - default: patch - options: - - patch - - minor - - major - version: - description: The version to publish - type: string - default: '' - -jobs: - publish: - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-node@v3 - with: - node-version: 16 - registry-url: 'https://npm.pkg.github.com' - scope: '@actions' - - - run: npm ci - env: - NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - - run: | - git config user.name github-actions - git config user.email github-actions@github.com - - - name: Publish packages - run: npx lerna publish ${{ inputs.version || inputs.bump || 'patch' }} --yes - env: - NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..d9ff3ba --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,51 @@ +name: Create release PR + +run-name: Create release PR for v${{ github.event.inputs.version }} + +on: + workflow_dispatch: + inputs: + version: + required: true + description: "Version to bump `package.json` to (format: x.y.z)" + +jobs: + create-release-pr: + name: Create release PR + + runs-on: ubuntu-latest + + permissions: + contents: write + pull-requests: write + + steps: + - uses: actions/checkout@v3 + + - uses: actions/setup-node@v3 + with: + node-version: "16" + + - name: Bump version and push + run: | + git config --global user.email "github-actions@github.com" + git config --global user.name "GitHub Actions" + + git checkout -b release/${{ inputs.version }} + + npx lerna version ${{ inputs.version }} --yes --no-push --no-git-tag-version + + git add package.json package-lock.json + git commit -m "Release extension version ${{ inputs.version }}" + + git push --set-upstream origin release/${{ inputs.version }} + + - name: Create PR + run: | + gh pr create \ + --title "Release version ${{ inputs.version }}" \ + --body "Release version ${{ inputs.version }}" \ + --base main \ + --head release/${{ inputs.version }} + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..e17dec0 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,106 @@ +name: Release and publish extension + +on: + push: + branches: + - main + paths: + - package.json + + workflow_dispatch: + inputs: + version: + description: "Version to release" + required: true + +jobs: + check-version-change: + outputs: + changed: ${{ steps.check-version.outputs.result }} + + runs-on: ubuntu-latest + + permissions: + contents: read + + steps: + - uses: actions/checkout@v3 + - name: Check if version has changed + id: check-version + uses: actions/github-script@v6 + with: + script: | + const version = '${{ inputs.version }}' || require('./package.json').version; + // Find a release for that version + const release = await github.rest.repos.getReleaseByTag({ + owner: context.repo.owner, + repo: context.repo.repo, + tag: `release-v${version}`, + }).catch(() => null); + + // If the release exists, the version has not changed + if (release) { + console.log(`Version ${version} has an existing release`); + console.log(release.data.html_url); + core.summary.addLink(`Release v${version}`, release.data.html_url); + await core.summary.write(); + return "false"; + } + console.log(`Version ${version} does not have a release`); + return true; + + release: + environment: publish + + needs: check-version-change + if: ${{ needs.check-version-change.outputs.changed == 'true' }} + + runs-on: ubuntu-latest + + permissions: + contents: write + + env: + EXT_VERSION: "" # will be set in the workflow + + outputs: + version: ${{ env.EXT_VERSION }} + + steps: + - uses: actions/checkout@v3 + + - uses: actions/setup-node@v3 + with: + node-version: 16.x + cache: "npm" + registry-url: "https://npm.pkg.github.com" + + - name: Parse version from package.json + run: | + echo "EXT_VERSION=$(node -p -e "require('./package.json').version")" >> $GITHUB_ENV + + - run: npm ci + env: + NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Publish packages + run: | + lerna publish from-git + + - name: Create release and upload release asset + uses: actions/github-script@v6 + with: + script: | + const fs = require("fs"); + + const release = await github.rest.repos.createRelease({ + owner: context.repo.owner, + repo: context.repo.repo, + tag_name: "release-v${{ env.EXT_VERSION }}", + name: "v${{ env.EXT_VERSION }}", + draft: false, + prerelease: false + }); + + core.summary.addLink(`Release v${{ env.EXT_VERSION }}`, release.data.html_url); + await core.summary.write(); \ No newline at end of file