Skip to content

Commit 61eec07

Browse files
authored
[infra] add canary deploys on push to main (#61)
* add canary deploys on push to main * fix canary script
1 parent f4641f8 commit 61eec07

File tree

6 files changed

+107
-14
lines changed

6 files changed

+107
-14
lines changed

.github/workflows/canary.yml

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Deploy Canary
2+
3+
# Deploys are manually triggered
4+
on:
5+
push:
6+
branches: [main]
7+
8+
jobs:
9+
deploy:
10+
name: 'Deploy'
11+
environment: auto canary
12+
timeout-minutes: 15
13+
runs-on: ubuntu-latest # TODO: this should probably run on multiple OSes
14+
15+
steps:
16+
- uses: actions/checkout@v3
17+
with:
18+
# Ensure that git uses your token with admin access to the repo
19+
token: ${{ secrets.GH_TOKEN }}
20+
21+
- name: Prepare repository
22+
# Fetch full git history and tags
23+
run: git fetch --unshallow --tags
24+
25+
- name: Setup Node.js environment
26+
uses: actions/setup-node@v3
27+
with:
28+
node-version: 18
29+
30+
- name: Install pnpm
31+
uses: pnpm/action-setup@v2
32+
33+
- name: Install dependencies
34+
run: pnpm install --frozen-lockfile
35+
shell: bash
36+
37+
- name: Deploy Canary
38+
if: ${{ ! inputs.prerelease }}
39+
run: ./scripts/publish-canary.sh
40+
env:
41+
GH_TOKEN: ${{ secrets.GH_TOKEN }}
42+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/deploy.yml

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
name: Deploy
22

33
# Deploys are manually triggered
4-
on:
5-
workflow_dispatch:
6-
inputs:
7-
prerelease:
8-
description: 'Deploy as prerelease'
9-
required: true
10-
type: boolean
4+
on: workflow_dispatch
115

126
jobs:
137
deploy:
@@ -44,10 +38,3 @@ jobs:
4438
env:
4539
GH_TOKEN: ${{ secrets.GH_TOKEN }}
4640
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
47-
48-
- name: Deploy Prerelease
49-
if: ${{ inputs.prerelease }}
50-
run: pnpm auto next -vv
51-
env:
52-
GH_TOKEN: ${{ secrets.GH_TOKEN }}
53-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@types/react": "^18.0.24",
5858
"@types/react-dom": "^18.0.6",
5959
"@types/react-test-renderer": "^18.0.0",
60+
"@types/semver": "^7.3.13",
6061
"@types/tmp": "^0.2.3",
6162
"@typescript-eslint/eslint-plugin": "^5.38.0",
6263
"@typescript-eslint/parser": "^5.38.0",
@@ -80,6 +81,7 @@
8081
"react": "^18.2.0",
8182
"react-test-renderer": "^18.2.0",
8283
"rimraf": "^4.1.2",
84+
"semver": "^7.3.8",
8385
"tsdoc-markdown": "^0.0.1",
8486
"tsx": "^3.12.3",
8587
"turbo": "^1.7.4",

pnpm-lock.yaml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/publish-canary-version.ts

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* eslint-disable no-console */
2+
import { execSync } from 'child_process'
3+
import { readFileSync, writeFileSync } from 'fs'
4+
import path from 'path'
5+
import { parse } from 'semver'
6+
import { pathToFileURL } from 'url'
7+
8+
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
9+
// module was called directly
10+
const bumpType = process.argv[2] as 'major' | 'minor' | 'patch'
11+
if (!bumpType) {
12+
throw new Error('No bump type provided')
13+
} else if (!['major', 'minor', 'patch'].includes(bumpType)) {
14+
throw new Error('Invalid bump type provided')
15+
}
16+
17+
const packageJson = JSON.parse(readFileSync(path.join(process.cwd(), 'package.json'), 'utf8'))
18+
19+
if (packageJson.private) {
20+
console.log('Skipping canary publish for private package', packageJson.name)
21+
process.exit(0)
22+
}
23+
24+
const currentVerison = parse(packageJson.version)
25+
26+
const nextVersion = currentVerison?.inc(bumpType)
27+
28+
if (!nextVersion) {
29+
throw new Error('Could not parse current version')
30+
}
31+
32+
const sha = execSync('git rev-parse --short HEAD').toString().trim()
33+
34+
const versionString = `${nextVersion.major}.${nextVersion.minor}.${nextVersion.patch}-canary.${sha}`
35+
36+
console.log(`Setting ${packageJson.name} version to ${versionString}`)
37+
38+
writeFileSync('package.json', JSON.stringify({ ...packageJson, version: versionString }, null, 2))
39+
40+
execSync('npm publish --tag canary')
41+
}

scripts/publish-canary.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
set -eax
3+
4+
type=$(pnpm auto version)
5+
6+
# type will be 'patch', 'minor', 'major', or the empty string
7+
8+
# if it's the empty string, there's nothing to do, so exit
9+
if [ -z "$type" ]; then
10+
echo "No changes to publish"
11+
exit 0
12+
fi
13+
14+
project_root=$(git rev-parse --show-toplevel)
15+
16+
echo "publishing canary version"
17+
pnpm -r exec -- tsx $project_root/scripts/publish-canary-version.ts $type

0 commit comments

Comments
 (0)