Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to auto release workflow #832

Merged
merged 15 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion auto.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { AutoRc } from 'auto';
export default function rc(): AutoRc {
return {
baseBranch: 'main',
plugins: ['npm', 'released', ['exec', { afterShipIt: 'yarn publish-action' }]],
plugins: ['npm', 'released'],
shipit: {
onlyGraduateWithReleaseLabel: true,
prerelease: true,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/chromatic-main-and-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
chromatic:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/chromatic-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
chromatic:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/chromatic-staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
chromatic:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
23 changes: 21 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
name: Release

on: [push]
on:
# PRs have their own canary releases which always run but don't publish the GitHub Action.
# To allow debugging the release process, this does not have a paths filter.
pull_request:
branches:
- main

# Pushes to main trigger a release and publish the GitHub Action. To allow the occasional update
# to markdown and config files, only changes to actual source code trigger a release.
push:
branches:
- main
paths:
- action-src/**
- bin-src/**
- node-src/**
- action.yml
- isChromatic.*
- package.json
- storybook-addon.*

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
token: ${{ secrets.GH_TOKEN }}

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
self-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-node-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
self-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-node14.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
self-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-node16.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
self-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-node18.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
self-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-node20.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
self-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-npx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
chromatic:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
chromatic:
runs-on: windows-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-yarn-canary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
chromatic:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-yarn-classic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
chromatic:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/smoke-test-yarn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
self-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
Expand Down
78 changes: 17 additions & 61 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,6 @@ Publishes your Storybook to Chromatic and kicks off tests if they're enabled.

📝 View the [Changelog](https://github.com/chromaui/chromatic-cli/blob/main/CHANGELOG.md#readme)

## Using a `next` version

From time to time we pre-publish a `next` version of the package to test new features. To use the
next branch you can either:

### Using `npx`

Change your script to use the `next` dist-tag:

```bash
npx chromatic@next --project-token ...
```

### Using a dependency in `package.json`

Update to the latest `next` version with:

```bash
yarn add --dev chromatic@next

# or
npm i --save-dev chromatic@next
```

### Using the github action

Use our `chromatic-next` action:

```yaml
- uses: chromaui/action-next@v1
```

## Contributing

Contributions of any kind are welcome! We're available to chat via the Intercom widget on the documentation site.
Expand All @@ -58,7 +26,7 @@ Contributions of any kind are welcome! We're available to chat via the Intercom
Compatibility is guaranteed between this package and Chromatic like so:

- Production Chromatic ensures it’s compatible with what’s on npm
- What's on the main branch is equal to what's published on npm
- What's on the Git tag is equal to what's published on npm for that version
- This package ensures it’s compatible with production Chromatic

To facilitate upgrading in the future, removing and adding features, this is the process:
Expand All @@ -76,7 +44,7 @@ To facilitate upgrading in the future, removing and adding features, this is the
#### Running against staging

```bash
CHROMATIC_INDEX_URL=https://index.staging-chromatic.com yarn chromatic -t 253df72b53d2
CHROMATIC_INDEX_URL=https://index.staging-chromatic.com yarn chromatic -t <token>
```

#### Running against development
Expand All @@ -93,41 +61,29 @@ To only test a small number of test stories as a smoke test, use:
SMOKE_TEST=1 CHROMATIC_INDEX_URL=https://index.dev-chromatic.com yarn chromatic -t <token>
```

### Publishing a new version to npm
### Publishing a new version

Before publishing, make sure you've done the following:
We use `auto` to automate the release process. Versions are bumped, tags are created and the changelog is updated automatically. A new release goes out whenever a PR is merged to `main`. A PR **must** have **exactly one** of the following labels before merging:

- Updated CHANGELOG.md
- Committed and pushed everything (clean working directory)
- Decide on the proper semver bump (major/minor/patch)
- Decide on the proper tag (canary/next/latest)
- `major` triggers a major version bump
- `minor` triggers a minor version bump
- `patch` triggers a patch version bump

We have three types of releases:
Additionally, a PR **may** have exactly one of these labels:

- `canary` releases are intended for testing purposes and should not be used in production, as they may only work against a staging or dev environment.
- `next` releases should be valid, working releases that can potentially be used by early adopters of new features, for example to handle a support request.
- `latest` releases are the general audience production releases, used by most people.
- `release` creates a `latest` rather than a `next` release (see below)
- `skip-release` does not create a release at all

> For GitHub Actions, we publish `chromaui/action-canary` and `chromaui/action-next`, which contain the latest `canary` or `next` release, respectively. A `latest` release will also automatically update `chromaui/action-next` (besides `chromaui/action`), in order to keep users who happen to depend on `chromaui/action-next` up to date with the `latest` release.

A script is provided to create new releases:

```sh
yarn release <major|minor|patch> <canary|next|latest> [--dry-run]
```

This script ensures the version is bumped properly, the tag is set correctly and the corresponding GitHub Action is updated.
We have three types of releases:

#### Examples:
- `latest` releases are the general audience production releases, used by most people. Automatically created when merging a PR with the `release` label.
- `next` releases should be valid, working releases that can potentially be used by early adopters of new features, for example to handle a support request. Automatically created when merging a PR without the `release` and `skip-release` labels.
- `canary` releases are intended for testing purposes and should not be used in production, as they may only work against a staging or dev environment. Automatically created on every PR, but does not auto-publush the GitHub Action.

```sh
yarn release patch canary
```
> For GitHub Actions, we publish `chromaui/action-next` and `chromaui/action-canary`. The latter is only published manually, rather than for every PR.

Releases e.g. `6.6.1-canary.0`.
A script is provided to manually publish the GitHub Action, though it's typically only necessary for `action-canary` releases:

```sh
yarn release major latest
yarn publish-action <canary|next|latest>
```

Releases e.g. `7.0.0`.
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ inputs:
description: 'Deprecated, use onlyChanged, onlyStoryNames or onlyStoryFiles instead'
required: false
projectToken:
description: 'Your chromatic project token'
required: true
description: 'Your chromatic project token (best provided via env.CHROMATIC_PROJECT_TOKEN)'
required: false
repositorySlug:
description: 'Override the repository slug (e.g. ownerName/repositoryName)'
required: false
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"lint": "yarn lint:js .storybook bin-src node-src test-stories ./isChromatic.js ./isChromatic.mjs",
"lint:js": "cross-env NODE_ENV=production eslint --fix --cache --cache-location=.cache/eslint --ext .js,.json,.mjs,.ts,.cjs --report-unused-disable-directives",
"lint:package": "sort-package-json",
"release": "yarn run build && auto shipit",
"release": "yarn run build && auto shipit && yarn run publish-action",
"publish-action": "./scripts/publish-action.mjs",
"trace": "./dist/bin.js trace",
"trim-stats": "./dist/bin.js trim-stats-file",
Expand Down Expand Up @@ -108,7 +108,6 @@
"@actions/core": "^1.10.0",
"@actions/github": "^5.0.0",
"@antfu/ni": "^0.21.5",
"@auto-it/exec": "^11.0.4",
"@babel/cli": "^7.14.8",
"@babel/core": "^7.15.0",
"@babel/node": "^7.15.4",
Expand Down
50 changes: 31 additions & 19 deletions scripts/publish-action.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import tmp from 'tmp-promise';

const command = (cmd, opts) => execaCommand(cmd, { stdio: 'inherit', ...opts });

const publishAction = async ({ context, newVersion, repo }) => {
console.info(`✅ Publishing ${newVersion} as ${context} action to https://github.com/${repo}`);
const publishAction = async ({ version, repo }) => {
const dryRun = process.argv.includes('--dry-run');

const [major, minor, patch] = newVersion.replace(/^(\d+\.\d+\.\d+).*/, '$1').split('.');
if (!major || !minor || !patch) throw new Error(`Invalid version: ${newVersion}`);
console.info(`\n✅ Publishing ${version} to ${repo} ${dryRun ? '(dry run)' : ''}\n`);

const { path, cleanup } = await tmp.dir({ unsafeCleanup: true, prefix: `chromatic-action-` });
const run = (cmd) => command(cmd, { cwd: path });
Expand All @@ -21,14 +20,18 @@ const publishAction = async ({ context, newVersion, repo }) => {
await cpy(['action-src/CHANGELOG.md', 'action-src/LICENSE', 'action-src/README.md'], path);

await run('git init -b main');
await run('git config --global user.name "Chromatic"');
await run('git config --global user.email "[email protected]"');
await run(`git remote add origin https://${process.env.GH_TOKEN}@github.com/${repo}.git`);
await run('git add .');
await run(`git commit -m "${newVersion}"`);
await run('git tag -f v1');
await run('git push origin HEAD:main --force');
await run('git push --tags --force');
await run(`git commit -m ${version}`);
await run('git tag -f v1'); // For backwards compatibility
await run('git tag -f latest');

if (dryRun) {
console.info('\n✅ Skipping git push due to --dry-run\n');
} else {
await run('git push origin HEAD:main --force');
await run('git push --tags --force');
}

return cleanup();
};
Expand All @@ -37,28 +40,37 @@ const publishAction = async ({ context, newVersion, repo }) => {
* Generally, this script is invoked by auto's `afterShipIt` hook.
*
* For manual (local) use:
* yarn publish-action <context>
* yarn publish-action [context] [--dry-run]
* e.g. yarn publish-action canary
* or yarn publish-action --dry-run
*
* Make sure to build the action before publishing manually.
*/
(async () => {
const { default: pkg } = await import('../package.json', { assert: { type: 'json' } });

const { context, newVersion } = process.env.ARG_0
? JSON.parse(process.env.ARG_0)
: { newVersion: pkg.version, context: process.argv[2] };
const [, major, minor, patch, tag = 'latest'] =
pkg.version.match(/(\d+)\.(\d+)\.(\d+)-?(\w+)?/) || [];
if (!major || !minor || !patch) throw new Error(`Invalid version: ${pkg.version}`);

const context = ['canary', 'next', 'latest'].includes(process.argv[2]) ? process.argv[2] : tag;

switch (context) {
case 'canary':
await publishAction({ context, newVersion, repo: 'chromaui/action-canary' });
if (process.argv[2] !== 'canary') {
console.info('Skipping automatic publish of action-canary.');
console.info('Run `yarn publish-action canary` to publish a canary action.');
return;
}
await publishAction({ version: pkg.version, repo: 'chromaui/action-canary' });
break;
case 'next':
await publishAction({ context, newVersion, repo: 'chromaui/action-next' });
await publishAction({ version: pkg.version, repo: 'chromaui/action-next' });
break;
case 'latest':
await publishAction({ context, newVersion, repo: 'chromaui/action-next' });
await publishAction({ context, newVersion, repo: 'chromaui/action' });
await publishAction({ version: pkg.version, repo: 'chromaui/action' });
break;
default:
console.warn(`Unknown context: ${context}`);
throw new Error(`Unknown tag: ${tag}`);
}
})();
Loading