Skip to content

Commit 166f9f9

Browse files
committed
fix: Integrate semantic-release with build process
The version is driven by semantic-release. In order to use it in the package (both exported to NPM and as header metadata in the signature), we build the package in the `postversion` step of NPM scripts. - The CD no longer needs its own build step - Version is read from package.json (when updated) However, in order for the package to export its version as as standalone string (rather than runtime reading of wrong environment variables or whatnot), a pre-build script is used to generate the version tag, also containing the short Git SHA-1 (see `generateVersion.ts`).
1 parent f657e9a commit 166f9f9

File tree

8 files changed

+2965
-95
lines changed

8 files changed

+2965
-95
lines changed

.github/workflows/cd.yml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ jobs:
2626
${{ runner.os }}-yarn-
2727
- run: yarn install --ignore-scripts
2828
name: Install dependencies
29-
- run: yarn build
30-
name: Build bundle
3129

3230
# Continuous Delivery Pipeline --
3331

@@ -37,16 +35,6 @@ jobs:
3735
env:
3836
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3937
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
40-
- run: ./node_modules/.bin/ts-node ./scripts/generateVersion.ts
41-
if: steps.semantic.outputs.new-release-published == 'true'
42-
name: Generate version file
43-
- run: yarn build
44-
if: steps.semantic.outputs.new-release-published == 'true'
45-
name: Re-build bundle with version info
46-
- run: yarn sign
47-
if: steps.semantic.outputs.new-release-published == 'true'
48-
name: Sign bundle
49-
env:
5038
SIGNATURE_SECRET_KEY: ${{ secrets.SIGNATURE_SECRET_KEY }}
5139
- uses: dswistowski/surge-sh-action@341bcbd
5240
name: Deploy to Surge

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ jobs:
3232
name: Install dependencies
3333
- run: yarn ci
3434
name: Run integration tests
35+
env:
36+
SIGNATURE_SECRET_KEY: ${{ secrets.SIGNATURE_SECRET_KEY }}
3537
# - uses: coverallsapp/github-action@832e70b
3638
# name: Report code coverage
3739
# with:

.npmignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,6 @@ lib/main.*
1111
.dependabot/
1212
scripts/
1313
coverage/
14+
15+
# Generated at build time
16+
./src/version.ts

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@
2525
"test": "jest --coverage",
2626
"test:watch": "jest --watch",
2727
"build:clean": "rm -rf ./dist ./lib ./coverage",
28+
"build:version": "ts-node ./scripts/generateVersion.ts",
2829
"build:ts": "tsc",
2930
"build:bundle": "parcel build ./src/main.ts --out-file analytics.js --detailed-report 20 --experimental-scope-hoisting",
3031
"build:cors": "echo '*' > ./dist/CORS",
31-
"build": "run-s build:clean build:ts build:bundle build:cors",
32-
"gen:version": "ts-node ./scripts/generateVersion.ts",
33-
"sign": "ts-node ./scripts/sign.ts",
34-
"ci": "run-s build"
32+
"build:sign": "ts-node ./scripts/sign.ts",
33+
"build": "run-s build:clean build:version build:ts build:bundle build:sign build:cors",
34+
"ci": "run-s build",
35+
"postversion": "run-s build"
3536
},
3637
"dependencies": {
3738
"@chiffre/analytics-core": "^1.0.1",
@@ -47,6 +48,7 @@
4748
"jest": "^25.1.0",
4849
"npm-run-all": "^4.1.5",
4950
"parcel-bundler": "^1.12.4",
51+
"semantic-release": "^17.0.4",
5052
"ts-jest": "^25.2.1",
5153
"ts-node": "^8.6.2",
5254
"typescript": "^3.8.3"

scripts/generateVersion.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import path from 'path'
33

44
export function generateVersion() {
5-
const version = process.env.RELEASE_VERSION || '0.0.0'
5+
const version = process.env.npm_package_version || '0.0.0'
66
const gitSha1 = (process.env.GITHUB_SHA || 'local').slice(0, 8)
77
const tag = `${version}-${gitSha1}`
88
const body = `export const version = '${tag}'`

scripts/sign.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import crypto from 'crypto'
44
import { b64, utf8 } from '@47ng/codec'
55
import { parseSecretKey, signUtf8String } from '@chiffre/crypto-sign'
66

7-
function hashFile(filePath: string) {
8-
const contents = fs.readFileSync(filePath)
7+
function sha256(text: string | Buffer) {
98
const hash = crypto.createHash('sha256')
10-
hash.update(contents)
9+
hash.update(text)
1110
return `sha256:${b64.encode(hash.digest())}`
1211
}
1312

13+
function hashFile(filePath: string) {
14+
const contents = fs.readFileSync(filePath)
15+
return sha256(contents)
16+
}
17+
1418
export interface Metadata {
1519
version: string
1620
gitSha1: string
@@ -33,7 +37,8 @@ export function generateHeader(
3337
fileHash: hash
3438
}
3539
const json = JSON.stringify(header)
36-
const signature = signUtf8String(json, sk)
40+
const jsonHash = sha256(json)
41+
const signature = signUtf8String(jsonHash, sk)
3742
return `/*chiffre:sig ${signature}*/\n/*chiffre:header ${json}*/\n`
3843
}
3944

@@ -44,7 +49,7 @@ export function run() {
4449
return // Already signed
4550
}
4651
const meta: Metadata = {
47-
version: process.env.RELEASE_VERSION || '0.0.0',
52+
version: process.env.npm_package_version || '0.0.0-local',
4853
gitSha1: process.env.GITHUB_SHA || 'local',
4954
buildUrl: `https://github.com/chiffre-io/analytics-tracker/actions/runs/${process.env.GITHUB_RUN_ID}`
5055
}

src/version.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export const version = '0.0.0-local'
1+
export const version = '0.0.0-semantically-released-local'

0 commit comments

Comments
 (0)