.github/workflows/build-release.yaml #11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
on: | |
push: | |
branches: | |
- main | |
paths: | |
- "CHANGELOG.md" | |
workflow_dispatch: | |
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 = '${{ github.event.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: `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: | |
needs: check-version-change | |
if: ${{ needs.check-version-change.outputs.changed == 'true' }} | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
packages: read | |
env: | |
EXT_VERSION: "" # will be set in the workflow | |
outputs: | |
version: ${{ env.EXT_VERSION }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
with: | |
submodules: recursive | |
fetch-depth: 0 | |
- uses: actions/setup-node@v3 | |
with: | |
node-version: '18' | |
cache: npm | |
- name: Install Node.js dependencies | |
run: npm ci | |
- name: Build | |
run: npm run compile | |
- name: Package | |
run: npm run package | |
- name: Parse version from package.json | |
run: | | |
echo "EXT_VERSION=$(node -p -e "require('./package.json').version")" >> $GITHUB_ENV | |
- uses: actions/upload-artifact@v3 | |
with: | |
name: templ-${{ env.EXT_VERSION }}.vsix | |
path: ./templ-${{ env.EXT_VERSION }}.vsix | |
- 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: "v${{ env.EXT_VERSION }}", | |
name: "v${{ env.EXT_VERSION }}", | |
draft: false, | |
prerelease: false, | |
generate_release_notes: true | |
}); | |
const path = "./templ-${{ env.EXT_VERSION }}.vsix"; | |
await github.rest.repos.uploadReleaseAsset({ | |
owner: context.repo.owner, | |
repo: context.repo.repo, | |
release_id: release.data.id, | |
data: fs.readFileSync(path), | |
name: "templ-${{ env.EXT_VERSION }}.vsix", | |
headers: { | |
"content-type": "application/vsix", | |
"content-length": fs.statSync(path).size | |
} | |
}); | |
core.summary.addLink(`Release v${{ env.EXT_VERSION }}`, release.data.html_url); | |
await core.summary.write(); | |
publish: | |
environment: publish | |
needs: release | |
runs-on: ubuntu-latest | |
permissions: {} | |
steps: | |
- uses: actions/download-artifact@v3 | |
with: | |
name: templ-${{ needs.release.outputs.version }}.vsix | |
- name: Publish to marketplace | |
# https://github.com/HaaLeo/publish-vscode-extension/releases/tag/v1.4.0 | |
uses: HaaLeo/publish-vscode-extension@dfe4f6ad46624424fe24cb5bca79839183399045 | |
with: | |
pat: ${{ secrets.PUBLISHER_KEY }} | |
registryUrl: https://marketplace.visualstudio.com | |
extensionFile: ./templ-${{ needs.release.outputs.version }}.vsix | |
open-vsx-publish: | |
name: Publish to Open VSX Registry | |
needs: release | |
environment: publish-open-vsx | |
runs-on: ubuntu-latest | |
env: | |
OPEN_VSX_TOKEN: ${{ secrets.OPEN_VSX_TOKEN }} | |
steps: | |
- name: Download artifact | |
uses: actions/download-artifact@v3 | |
with: | |
name: templ-${{ needs.release.outputs.version }}.vsix | |
- name: Publish to Registry | |
run: | | |
npx ovsx publish -p $OPEN_VSX_TOKEN *.vsix |