|
| 1 | +name: Release and publish extension |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - package.json |
| 9 | + |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + version: |
| 13 | + description: "Version to release" |
| 14 | + required: true |
| 15 | +jobs: |
| 16 | + check-version-change: |
| 17 | + outputs: |
| 18 | + changed: ${{ steps.check-version.outputs.result }} |
| 19 | + |
| 20 | + runs-on: ubuntu-latest |
| 21 | + |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v3 |
| 27 | + - name: Check if version has changed |
| 28 | + id: check-version |
| 29 | + uses: actions/github-script@v6 |
| 30 | + with: |
| 31 | + script: | |
| 32 | + const version = '${{ github.event.inputs.version }}' || require('./package.json').version; |
| 33 | + // Find a release for that version |
| 34 | + const release = await github.rest.repos.getReleaseByTag({ |
| 35 | + owner: context.repo.owner, |
| 36 | + repo: context.repo.repo, |
| 37 | + tag: `release-v${version}`, |
| 38 | + }).catch(() => null); |
| 39 | +
|
| 40 | + // If the release exists, the version has not changed |
| 41 | + if (release) { |
| 42 | + console.log(`Version ${version} has an existing release`); |
| 43 | + console.log(release.data.html_url); |
| 44 | + core.summary.addLink(`Release v${version}`, release.data.html_url); |
| 45 | + await core.summary.write(); |
| 46 | + return "false"; |
| 47 | + } |
| 48 | + console.log(`Version ${version} does not have a release`); |
| 49 | + return true; |
| 50 | +
|
| 51 | + release: |
| 52 | + needs: check-version-change |
| 53 | + if: ${{ needs.check-version-change.outputs.changed == 'true' }} |
| 54 | + |
| 55 | + runs-on: ubuntu-latest |
| 56 | + |
| 57 | + permissions: |
| 58 | + contents: write |
| 59 | + |
| 60 | + env: |
| 61 | + EXT_VERSION: "" # will be set in the workflow |
| 62 | + |
| 63 | + outputs: |
| 64 | + version: ${{ env.EXT_VERSION }} |
| 65 | + |
| 66 | + steps: |
| 67 | + - uses: actions/checkout@v3 |
| 68 | + |
| 69 | + - uses: actions/setup-node@v3 |
| 70 | + with: |
| 71 | + node-version: "16" |
| 72 | + |
| 73 | + - name: Parse version from package.json |
| 74 | + run: | |
| 75 | + echo "EXT_VERSION=$(node -p -e "require('./package.json').version")" >> $GITHUB_ENV |
| 76 | +
|
| 77 | + - run: npm ci |
| 78 | + |
| 79 | + - run: npm run package |
| 80 | + |
| 81 | + - uses: actions/upload-artifact@v3 |
| 82 | + with: |
| 83 | + name: vscode-github-actions-${{ env.EXT_VERSION }}.vsix |
| 84 | + path: ./vscode-github-actions-${{ env.EXT_VERSION }}.vsix |
| 85 | + |
| 86 | + - name: Create release and upload release asset |
| 87 | + uses: actions/github-script@v6 |
| 88 | + with: |
| 89 | + script: | |
| 90 | + const fs = require("fs"); |
| 91 | +
|
| 92 | + const release = await github.rest.repos.createRelease({ |
| 93 | + owner: context.repo.owner, |
| 94 | + repo: context.repo.repo, |
| 95 | + tag_name: "release-v${{ env.EXT_VERSION }}", |
| 96 | + name: "v${{ env.EXT_VERSION }}", |
| 97 | + draft: false, |
| 98 | + prerelease: false |
| 99 | + }); |
| 100 | +
|
| 101 | + const path = "./vscode-github-actions-${{ env.EXT_VERSION }}.vsix"; |
| 102 | + await github.rest.repos.uploadReleaseAsset({ |
| 103 | + owner: context.repo.owner, |
| 104 | + repo: context.repo.repo, |
| 105 | + release_id: release.data.id, |
| 106 | + data: fs.readFileSync(path).toString(), |
| 107 | + name: "vscode-github-actions-${{ env.EXT_VERSION }}.vsix", |
| 108 | + headers: { |
| 109 | + "content-type": "application/vsix", |
| 110 | + "content-length": fs.statSync(path).size |
| 111 | + } |
| 112 | + }); |
| 113 | +
|
| 114 | + core.summary.addLink(`Release v${version}`, release.data.html_url); |
| 115 | + await core.summary.write(); |
| 116 | +
|
| 117 | + publish: |
| 118 | + environment: publish |
| 119 | + |
| 120 | + needs: release |
| 121 | + |
| 122 | + runs-on: ubuntu-latest |
| 123 | + permissions: {} |
| 124 | + |
| 125 | + steps: |
| 126 | + - uses: actions/download-artifact@v3 |
| 127 | + with: |
| 128 | + name: vscode-github-actions-${{ needs.release.outputs.version }}.vsix |
| 129 | + |
| 130 | + - name: Publish to marketplace |
| 131 | + # https://github.com/HaaLeo/publish-vscode-extension/releases/tag/v1.2.0 |
| 132 | + uses: HaaLeo/publish-vscode-extension@c1a0486c5a3eed24e8c21d4e37889a7c4c60c443 |
| 133 | + with: |
| 134 | + pat: ${{ secrets.PUBLISHER_KEY }} |
| 135 | + registryUrl: https://marketplace.visualstudio.com |
| 136 | + extensionFile: ./vscode-github-actions-${{ needs.release.outputs.version }}.vsix |
0 commit comments