Skip to content

Commit

Permalink
Adds canary.yml (#35)
Browse files Browse the repository at this point in the history
* Adds canary.yml

* release:canary

* dependencies

* pnpm-lock.yaml

* pnpm install --no-frozen-lockfile

* shared-workspace-lockfile

* pnpm i

* pnpm up

* Remove duplicat step

* changeset config

* GITHUB_TOKEN

* pnpm up not working

* don't install pnpm twice

* setup npm credentials

* Delete cuddly-beans-approve.md

Co-authored-by: Pablo Sáez <[email protected]>
  • Loading branch information
gigamesh and PabloSzx authored Sep 10, 2022
1 parent db003a0 commit 055f781
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 18 deletions.
6 changes: 1 addition & 5 deletions .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,5 @@
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"snapshot": {
"useCalculatedVersion": true,
"prereleaseTemplate": "{tag}-{datetime}-{commit}"
}
"updateInternalDependencies": "patch"
}
46 changes: 46 additions & 0 deletions .github/workflows/canary.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
name: Canary Release

on:
push:
workflow_dispatch:

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
publish-canary:
name: Publish Canary
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@master
with:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0

- name: Setup PNPM
uses: pnpm/[email protected]
with:
version: ^7.9.5

- name: Use Node
uses: actions/setup-node@v3
with:
node-version: 18.x
cache: 'pnpm'

- name: Setup NPM credentials
run: echo "//registry.npmjs.org/:_authToken=$NPM_TOKEN" >> ~/.npmrc
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Install Dependencies
run: pnpm i

- name: Release Canary
id: canary
uses: 'kamilkisiela/release-canary@master'
with:
npm-token: ${{ secrets.NPM_TOKEN }}
npm-script: 'pnpm release:canary'
changesets: true
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
strict-peer-dependencies=false
shared-workspace-lockfile=true
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"postinstall": "husky install",
"pretty-quick": "pretty-quick --staged",
"release": "pnpm -r --access=public publish --no-git-checks",
"release:canary": "(node scripts/canary-release.js && pnpm -r publish --access public --no-git-checks --tag alpha) || echo Skipping Canary...",
"test": "pnpm -r --filter=sdk test",
"tsc": "tsc -p tsconfig.check.json",
"changeset": "changeset"
Expand All @@ -31,8 +32,13 @@
],
"packageManager": "[email protected]",
"devDependencies": {
"@changesets/apply-release-plan": "^6.1.0",
"@changesets/assemble-release-plan": "^5.2.1",
"@changesets/changelog-github": "0.4.6",
"@changesets/cli": "2.24.3",
"@changesets/config": "^2.1.1",
"@changesets/read": "^0.5.7",
"@manypkg/get-packages": "^1.1.3",
"@types/node": "18.7.5",
"@typescript-eslint/eslint-plugin": "^5.36.1",
"@typescript-eslint/parser": "^5.36.1",
Expand All @@ -44,6 +50,7 @@
"prettier": "^2.7.1",
"pretty-quick": "^3.1.3",
"rimraf": "^3.0.2",
"semver": "^7.3.7",
"typescript": "4.7.4"
},
"pnpm": {
Expand Down
26 changes: 13 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions scripts/canary-release.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const semver = require('semver')
const cp = require('child_process')
const { basename } = require('path')

const { read: readConfig } = require('@changesets/config')
const readChangesets = require('@changesets/read').default
const assembleReleasePlan = require('@changesets/assemble-release-plan').default
const applyReleasePlan = require('@changesets/apply-release-plan').default
const { getPackages } = require('@manypkg/get-packages')

function getNewVersion(version, type) {
const gitHash = cp.spawnSync('git', ['rev-parse', '--short', 'HEAD']).stdout.toString().trim()

return semver.inc(version, `pre${type}`, true, 'alpha-' + gitHash)
}

function getRelevantChangesets(baseBranch) {
const comparePoint = cp
.spawnSync('git', ['merge-base', `origin/${baseBranch}`, 'HEAD'])
.stdout.toString()
.trim()
console.log('compare point', comparePoint)
const listModifiedFiles = cp
.spawnSync('git', ['diff', '--name-only', comparePoint])
.stdout.toString()
.trim()
.split('\n')
console.log('listModifiedFiles', listModifiedFiles)

const items = listModifiedFiles.filter((f) => f.startsWith('.changeset')).map((f) => basename(f, '.md'))
console.log('items', items)

return items
}

async function updateVersions() {
const cwd = process.cwd()
const packages = await getPackages(cwd)
const config = await readConfig(cwd, packages)
const modifiedChangesets = getRelevantChangesets(config.baseBranch)
const changesets = (await readChangesets(cwd)).filter((change) => modifiedChangesets.includes(change.id))

if (changesets.length === 0) {
console.warn(`Unable to find any relevant package for canary publishing. Please make sure changesets exists!`)
process.exit(1)
} else {
const releasePlan = assembleReleasePlan(changesets, packages, config, undefined, false)

if (releasePlan.releases.length === 0) {
console.warn(`Unable to find any relevant package for canary releasing. Please make sure changesets exists!`)
process.exit(1)
} else {
for (const release of releasePlan.releases) {
if (release.type !== 'none') {
release.newVersion = getNewVersion(release.oldVersion, release.type)
}
}

await applyReleasePlan(
releasePlan,
packages,
{
...config,
commit: false,
},
false,
true,
)
}
}
}

updateVersions()
.then(() => {
console.info(`Done!`)
})
.catch((err) => {
console.error(err)
process.exit(1)
})

0 comments on commit 055f781

Please sign in to comment.