Skip to content

Commit bfd4097

Browse files
authored
Publish extension on merge to main (#507)
* Add continuous delivery of extension * Add comment * Only deploy if tests passed * Pr feedback
1 parent 4116475 commit bfd4097

File tree

4 files changed

+107
-3
lines changed

4 files changed

+107
-3
lines changed

.github/workflows/deploy.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Deploy
2+
3+
on:
4+
workflow_run:
5+
workflows: [Run Tests]
6+
types: [completed]
7+
branches: [main]
8+
9+
jobs:
10+
deploy:
11+
runs-on: ubuntu-latest
12+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
13+
environment: production
14+
steps:
15+
- uses: actions/checkout@v2
16+
- uses: actions/setup-node@v1
17+
with:
18+
node-version: 16
19+
cache: yarn
20+
- run: yarn install --frozen-lockfile
21+
- run: yarn run compile
22+
- run: yarn run prepare-for-extension-publish
23+
- name: Publish to Open VSX Registry
24+
uses: HaaLeo/publish-vscode-extension@v1
25+
id: publishToOpenVSX
26+
with:
27+
pat: ${{ secrets.OPEN_VSX_TOKEN }}
28+
- name: Publish to Visual Studio Marketplace
29+
uses: HaaLeo/publish-vscode-extension@v1
30+
with:
31+
pat: ${{ secrets.VS_MARKETPLACE_TOKEN }}
32+
registryUrl: https://marketplace.visualstudio.com
33+
extensionFile: ${{ steps.publishToOpenVSX.outputs.vsixPath }}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,13 +476,15 @@
476476
"watch": "tsc -watch -p ./",
477477
"pretest": "yarn run compile && yarn run lint && yarn run esbuild",
478478
"lint": "eslint src --ext ts",
479-
"test": "env CURSORLESS_TEST=true node ./out/test/runTest.js"
479+
"test": "env CURSORLESS_TEST=true node ./out/test/runTest.js",
480+
"prepare-for-extension-publish": "node ./out/scripts/prepareForExtensionPublish.js"
480481
},
481482
"devDependencies": {
482483
"@types/glob": "^7.1.3",
483484
"@types/js-yaml": "^4.0.2",
484485
"@types/mocha": "^8.0.4",
485486
"@types/node": "^16.11.3",
487+
"@types/semver": "^7.3.9",
486488
"@types/sinon": "^10.0.2",
487489
"@types/vscode": "^1.61.0",
488490
"@typescript-eslint/eslint-plugin": "^4.9.0",
@@ -494,6 +496,7 @@
494496
"js-yaml": "^4.1.0",
495497
"mocha": "^8.1.3",
496498
"npm-license-crawler": "^0.2.1",
499+
"semver": "^7.3.5",
497500
"sinon": "^11.1.1",
498501
"typescript": "^4.4.4",
499502
"vscode-test": "^1.4.1"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import * as semver from "semver";
2+
import { exec } from "child_process";
3+
import { promisify } from "util";
4+
import { writeFile } from "fs/promises";
5+
6+
const execAsync = promisify(exec);
7+
8+
/**
9+
* Prepares the directory for extension publication. Does the following:
10+
*
11+
* 1. Changes the package version so that the patch number is the number of
12+
* commits on the current branch
13+
* 2. Writes a file called `build-info.json` for provenance
14+
*/
15+
async function main() {
16+
const { major, minor } = semver.parse(process.env.npm_package_version)!;
17+
18+
const commitCount = await runCommand("git rev-list --count HEAD");
19+
20+
const newVersion = `${major}.${minor}.${commitCount}`;
21+
22+
await runCommand(`npm --no-git-tag-version version ${newVersion}`);
23+
24+
// These are automatically set for Github actions
25+
// See https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
26+
const repository = process.env["GITHUB_REPOSITORY"];
27+
const runId = process.env["GITHUB_RUN_ID"];
28+
29+
if (repository == null) {
30+
throw new Error("Missing environment variable GITHUB_REPOSITORY");
31+
}
32+
33+
if (runId == null) {
34+
throw new Error("Missing environment variable GITHUB_RUN_ID");
35+
}
36+
37+
await writeFile(
38+
"build-info.json",
39+
JSON.stringify({
40+
gitSha: (await runCommand("git rev-parse HEAD")).trim(),
41+
buildUrl: `https://github.com/${repository}/actions/runs/${runId}`,
42+
})
43+
);
44+
}
45+
46+
async function runCommand(command: string) {
47+
const { stdout, stderr } = await execAsync(command);
48+
49+
if (stderr) {
50+
throw new Error(stderr);
51+
}
52+
53+
return stdout;
54+
}
55+
56+
main();

yarn.lock

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,11 @@
136136
resolved "https://registry.yarnpkg.com/@types/node/-/node-16.11.3.tgz#fad0b069ec205b0e81429c805d306d2c12e26be1"
137137
integrity sha512-aIYL9Eemcecs1y77XzFGiSc+FdfN58k4J23UEe6+hynf4Wd9g4DzQPwIKL080vSMuubFqy2hWwOzCtJdc6vFKw==
138138

139+
"@types/semver@^7.3.9":
140+
version "7.3.9"
141+
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-7.3.9.tgz#152c6c20a7688c30b967ec1841d31ace569863fc"
142+
integrity sha512-L/TMpyURfBkf+o/526Zb6kd/tchUP3iBDEPjqjb+U2MAJhVRxxrmr2fwpe08E7QsV7YLcpq0tUaQ9O9x97ZIxQ==
143+
139144
"@types/sinon@^10.0.2":
140145
version "10.0.2"
141146
resolved "https://registry.yarnpkg.com/@types/sinon/-/sinon-10.0.2.tgz#f360d2f189c0fd433d14aeb97b9d705d7e4cc0e4"
@@ -1139,9 +1144,9 @@ levn@^0.4.1:
11391144
prelude-ls "^1.2.1"
11401145
type-check "~0.4.0"
11411146

1142-
"license-checker@git+https://github.com/mwittig/license-checker.git#d546e3f738e14c62e732346fa355162d46700893":
1147+
"license-checker@git+https://github.com/mwittig/license-checker#d546e3f738e14c62e732346fa355162d46700893":
11431148
version "1.0.0"
1144-
resolved "git+https://github.com/mwittig/license-checker.git#d546e3f738e14c62e732346fa355162d46700893"
1149+
resolved "git+https://github.com/mwittig/license-checker#d546e3f738e14c62e732346fa355162d46700893"
11451150
dependencies:
11461151
chalk "~0.5.1"
11471152
mkdirp "^0.3.5"
@@ -1569,6 +1574,13 @@ semver@^7.2.1, semver@^7.3.2:
15691574
dependencies:
15701575
lru-cache "^6.0.0"
15711576

1577+
semver@^7.3.5:
1578+
version "7.3.5"
1579+
resolved "https://registry.yarnpkg.com/semver/-/semver-7.3.5.tgz#0b621c879348d8998e4b0e4be94b3f12e6018ef7"
1580+
integrity sha512-PoeGJYh8HK4BTO/a9Tf6ZG3veo/A7ZVsYrSA6J8ny9nb3B1VrpkuN+z9OE5wfE5p6H4LchYZsegiQgbJD94ZFQ==
1581+
dependencies:
1582+
lru-cache "^6.0.0"
1583+
15721584
15731585
version "5.0.1"
15741586
resolved "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-5.0.1.tgz"

0 commit comments

Comments
 (0)