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

Fix GitHub Action release script using data from Auto shipIt hook rather than from Git #956

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions node-src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,8 @@ export interface Context {
committerEmail?: string;
committedAt: number;
slug?: string;
fromCI: boolean;
ciService?: string;
mergeCommit?: string;
uncommittedHash?: string;
parentCommits?: string[];
Expand Down
9 changes: 8 additions & 1 deletion node-src/ui/messages/errors/fatalError.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ export default function fatalError(
{
timestamp,
sessionId,
gitVersion: git && git.version,
gitVersion: git?.version,
gitBranch: git?.branch,
gitSlug: git?.slug,
fromCI: git?.fromCI,
ciService: git?.ciService,
nodePlatform: process.platform,
nodeVersion: process.versions.node,
...runtimeMetadata,
Expand All @@ -50,6 +54,9 @@ export default function fatalError(
flags,
...(extraOptions && { extraOptions }),
...(configuration && { configuration }),
...('options' in ctx && ctx.options?.isLocalBuild
? { isLocalBuild: ctx.options.isLocalBuild }
: {}),
...('options' in ctx && ctx.options?.buildScriptName
? { buildScript: scripts[ctx.options.buildScriptName] }
: {}),
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,13 @@
},
"plugins": [
"npm",
"released"
"released",
[
"exec",
{
"afterShipIt": "yarn run publish-action"
}
]
]
},
"docs": "https://www.chromatic.com/docs/cli",
Expand Down
40 changes: 25 additions & 15 deletions scripts/publish-action.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import cpy from 'cpy';
import { $ } from 'execa';
import { readFileSync } from 'fs';
import tmp from 'tmp-promise';

const copy = (globs, ...args) => {
Expand Down Expand Up @@ -52,9 +53,9 @@ const publishAction = async ({ major, version, repo }) => {
* Generally, this script is invoked by auto's `afterShipIt` hook.
*
* For manual (local) use:
* yarn publish-action [context] [--dry-run]
* yarn publish-action {context} [--dry-run]
* e.g. yarn publish-action canary
* or yarn publish-action --dry-run
* or yarn publish-action latest --dry-run
*
* Make sure to build the action before publishing manually.
*/
Expand All @@ -65,36 +66,45 @@ const publishAction = async ({ major, version, repo }) => {
return;
}

const { default: pkg } = await import('../package.json', { assert: { type: 'json' } });
let context, version;
if (['canary', 'next', 'latest'].includes(process.argv[2])) {
const pkg = JSON.parse(readFileSync('package.json', 'utf8'));
context = process.argv[2];
version = pkg.version;
console.info(`📌 Using context arg: ${context}`);
console.info(`📌 Using package.json version: ${version}`);
} else {
const data = JSON.parse(process.env.ARG_0);
context = data.context;
version = data.newVersion;
console.info(`📌 Using auto shipIt context: ${context}`);
console.info(`📌 Using auto shipIt version: ${version}`);
console.info(`Commits in release:`);
data.commitsInRelease.forEach((c) => console.info(`- ${c.hash.slice(0, 8)} ${c.subject}`));
}

const [, major, minor, patch, tag] = pkg.version.match(/(\d+)\.(\d+)\.(\d+)-*(\w+)?/) || [];
const [, major, minor, patch] = version.match(/(\d+)\.(\d+)\.(\d+)-*(\w+)?/) || [];
if (!major || !minor || !patch) {
console.error(`❗️ Invalid version: ${pkg.version}`);
console.error(`❗️ Invalid version: ${version}`);
return;
}

const { stdout: branch } = await $`git rev-parse --abbrev-ref HEAD`;
const defaultTag = branch === 'main' ? 'latest' : 'canary';
const context = ['canary', 'next', 'latest'].includes(process.argv[2])
? process.argv[2]
: tag || defaultTag;

switch (context) {
case '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({ major, version: pkg.version, repo: 'chromaui/action-canary' });
await publishAction({ major, version, repo: 'chromaui/action-canary' });
break;
case 'next':
await publishAction({ major, version: pkg.version, repo: 'chromaui/action-next' });
await publishAction({ major, version, repo: 'chromaui/action-next' });
break;
case 'latest':
await publishAction({ major, version: pkg.version, repo: 'chromaui/action' });
await publishAction({ major, version, repo: 'chromaui/action' });
break;
default:
console.error(`❗️ Unknown tag: ${tag}`);
console.error(`❗️ Unknown context: ${context}`);
}
})();