Automate release process via Changesets (#3915)
Co-authored-by: Francisco <fg@frang.io>
This commit is contained in:
8
scripts/release/workflow/exit-prerelease.sh
Normal file
8
scripts/release/workflow/exit-prerelease.sh
Normal file
@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
npx changeset pre exit rc
|
||||
git add .
|
||||
git commit -m "Exit release candidate"
|
||||
git push origin
|
||||
47
scripts/release/workflow/github-release.js
Normal file
47
scripts/release/workflow/github-release.js
Normal file
@ -0,0 +1,47 @@
|
||||
const { readFileSync } = require('fs');
|
||||
const { join } = require('path');
|
||||
const { version } = require(join(__dirname, '../../../package.json'));
|
||||
|
||||
module.exports = async ({ github, context }) => {
|
||||
const changelog = readFileSync('CHANGELOG.md', 'utf8');
|
||||
|
||||
await github.rest.repos.createRelease({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
tag_name: `v${version}`,
|
||||
body: extractSection(changelog, version),
|
||||
prerelease: process.env.PRERELEASE === 'true',
|
||||
});
|
||||
};
|
||||
|
||||
// From https://github.com/frangio/extract-changelog/blob/master/src/utils/word-regexp.ts
|
||||
function makeWordRegExp(word) {
|
||||
const start = word.length > 0 && /\b/.test(word[0]) ? '\\b' : '';
|
||||
const end = word.length > 0 && /\b/.test(word[word.length - 1]) ? '\\b' : '';
|
||||
return new RegExp(start + [...word].map(c => (/[a-z0-9]/i.test(c) ? c : '\\' + c)).join('') + end);
|
||||
}
|
||||
|
||||
// From https://github.com/frangio/extract-changelog/blob/master/src/core.ts
|
||||
function extractSection(document, wantedHeading) {
|
||||
// ATX Headings as defined in GitHub Flavored Markdown (https://github.github.com/gfm/#atx-headings)
|
||||
const heading = /^ {0,3}(?<lead>#{1,6})(?: [ \t\v\f]*(?<text>.*?)[ \t\v\f]*)?(?:[\n\r]+|$)/gm;
|
||||
|
||||
const wantedHeadingRe = makeWordRegExp(wantedHeading);
|
||||
|
||||
let start, end;
|
||||
|
||||
for (const m of document.matchAll(heading)) {
|
||||
if (!start) {
|
||||
if (m.groups.text.search(wantedHeadingRe) === 0) {
|
||||
start = m;
|
||||
}
|
||||
} else if (m.groups.lead.length <= start.groups.lead.length) {
|
||||
end = m;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (start) {
|
||||
return document.slice(start.index + start[0].length, end?.index);
|
||||
}
|
||||
}
|
||||
25
scripts/release/workflow/pack.sh
Normal file
25
scripts/release/workflow/pack.sh
Normal file
@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
dist_tag() {
|
||||
PACKAGE_JSON_NAME="$(jq -r .name ./package.json)"
|
||||
LATEST_NPM_VERSION="$(npm info "$PACKAGE_JSON_NAME" version)"
|
||||
PACKAGE_JSON_VERSION="$(jq -r .version ./package.json)"
|
||||
|
||||
if [ "$PRERELEASE" = "true" ]; then
|
||||
echo "next"
|
||||
elif npx semver -r ">$LATEST_NPM_VERSION" "$PACKAGE_JSON_VERSION" > /dev/null; then
|
||||
echo "latest"
|
||||
else
|
||||
# This is a patch for an older version
|
||||
# npm can't publish without a tag
|
||||
echo "tmp"
|
||||
fi
|
||||
}
|
||||
|
||||
cd contracts
|
||||
TARBALL="$(npm pack | tee /dev/stderr | tail -1)"
|
||||
echo "tarball=$(pwd)/$TARBALL" >> $GITHUB_OUTPUT
|
||||
echo "tag=$(dist_tag)" >> $GITHUB_OUTPUT
|
||||
cd ..
|
||||
24
scripts/release/workflow/prepare-release-merge.sh
Normal file
24
scripts/release/workflow/prepare-release-merge.sh
Normal file
@ -0,0 +1,24 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Define merge branch name
|
||||
MERGE_BRANCH=merge/$GITHUB_REF_NAME
|
||||
|
||||
# Create the branch and force to start from ref
|
||||
git checkout -B "$MERGE_BRANCH" "$GITHUB_REF_NAME"
|
||||
|
||||
# Get deleted changesets in this branch that might conflict with master
|
||||
readarray -t DELETED_CHANGESETS < <(git diff origin/master --name-only -- '.changeset/*.md')
|
||||
|
||||
# Merge master, which will take those files cherry-picked. Auto-resolve conflicts favoring master.
|
||||
git merge origin/master -m "Merge master to $GITHUB_REF_NAME" -X theirs
|
||||
|
||||
# Remove the originally deleted changesets to correctly sync with master
|
||||
rm -f "${DELETED_CHANGESETS[@]}"
|
||||
|
||||
git add .changeset/
|
||||
|
||||
# Allow empty here since there may be no changes if `rm -f` failed for all changesets
|
||||
git commit --allow-empty -m "Sync changesets with master"
|
||||
git push -f origin "$MERGE_BRANCH"
|
||||
15
scripts/release/workflow/publish.sh
Normal file
15
scripts/release/workflow/publish.sh
Normal file
@ -0,0 +1,15 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Intentionally escape $ to avoid interpolation and writing the token to disk
|
||||
echo "//registry.npmjs.org/:_authToken=\${NPM_TOKEN}" > .npmrc
|
||||
|
||||
# Actual publish
|
||||
npm publish "$TARBALL" --tag "$TAG"
|
||||
|
||||
if [ "$TAG" = "tmp" ]; then
|
||||
# Remove tmp tag
|
||||
PACKAGE_JSON_NAME="$(tar xfO "$TARBALL" package/package.json | jq -r .name)"
|
||||
npm dist-tag rm "$PACKAGE_JSON_NAME" "$TAG"
|
||||
fi
|
||||
7
scripts/release/workflow/rerun.js
Normal file
7
scripts/release/workflow/rerun.js
Normal file
@ -0,0 +1,7 @@
|
||||
module.exports = ({ github, context }) =>
|
||||
github.rest.actions.createWorkflowDispatch({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
workflow_id: 'release-cycle.yml',
|
||||
ref: process.env.REF || process.env.GITHUB_REF_NAME,
|
||||
});
|
||||
17
scripts/release/workflow/set-changesets-pr-title.js
Normal file
17
scripts/release/workflow/set-changesets-pr-title.js
Normal file
@ -0,0 +1,17 @@
|
||||
const { coerce, inc, rsort } = require('semver');
|
||||
const { join } = require('path');
|
||||
const { version } = require(join(__dirname, '../../../package.json'));
|
||||
|
||||
module.exports = async ({ core }) => {
|
||||
// Variables not in the context
|
||||
const refName = process.env.GITHUB_REF_NAME;
|
||||
|
||||
// Compare package.json version's next patch vs. first version patch
|
||||
// A recently opened branch will give the next patch for the previous minor
|
||||
// So, we get the max against the patch 0 of the release branch's version
|
||||
const branchPatch0 = coerce(refName.replace('release-v', '')).version;
|
||||
const packageJsonNextPatch = inc(version, 'patch');
|
||||
const [nextVersion] = rsort([branchPatch0, packageJsonNextPatch], false);
|
||||
|
||||
core.exportVariable('TITLE', `Release v${nextVersion}`);
|
||||
};
|
||||
32
scripts/release/workflow/start.sh
Normal file
32
scripts/release/workflow/start.sh
Normal file
@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -euo pipefail
|
||||
|
||||
# Set changeset status location
|
||||
# This is needed because `changeset status --output` only works with relative routes
|
||||
CHANGESETS_STATUS_JSON="$(realpath --relative-to=. "$RUNNER_TEMP/status.json")"
|
||||
|
||||
# Save changeset status to temp file
|
||||
npx changeset status --output="$CHANGESETS_STATUS_JSON"
|
||||
|
||||
# Defensive assertion. SHOULD NOT BE REACHED
|
||||
if [ "$(jq '.releases | length' "$CHANGESETS_STATUS_JSON")" != 1 ]; then
|
||||
echo "::error file=$CHANGESETS_STATUS_JSON::The status doesn't contain only 1 release"
|
||||
exit 1;
|
||||
fi;
|
||||
|
||||
# Create branch
|
||||
BRANCH_SUFFIX="$(jq -r '.releases[0].newVersion | gsub("\\.\\d+$"; "")' $CHANGESETS_STATUS_JSON)"
|
||||
RELEASE_BRANCH="release-v$BRANCH_SUFFIX"
|
||||
git checkout -b "$RELEASE_BRANCH"
|
||||
|
||||
# Output branch
|
||||
echo "branch=$RELEASE_BRANCH" >> $GITHUB_OUTPUT
|
||||
|
||||
# Enter in prerelease state
|
||||
npx changeset pre enter rc
|
||||
git add .
|
||||
git commit -m "Start release candidate"
|
||||
|
||||
# Push branch
|
||||
git push origin "$RELEASE_BRANCH"
|
||||
104
scripts/release/workflow/state.js
Normal file
104
scripts/release/workflow/state.js
Normal file
@ -0,0 +1,104 @@
|
||||
const { readPreState } = require('@changesets/pre');
|
||||
const { default: readChangesets } = require('@changesets/read');
|
||||
const { join } = require('path');
|
||||
const { version } = require(join(__dirname, '../../../package.json'));
|
||||
|
||||
module.exports = async ({ github, context, core }) => {
|
||||
const state = await getState({ github, context, core });
|
||||
|
||||
function setOutput(key, value) {
|
||||
core.info(`State ${key} = ${value}`);
|
||||
core.setOutput(key, value);
|
||||
}
|
||||
|
||||
// Jobs to trigger
|
||||
setOutput('start', shouldRunStart(state));
|
||||
setOutput('promote', shouldRunPromote(state));
|
||||
setOutput('changesets', shouldRunChangesets(state));
|
||||
setOutput('publish', shouldRunPublish(state));
|
||||
setOutput('merge', shouldRunMerge(state));
|
||||
|
||||
// Global Variables
|
||||
setOutput('is_prerelease', state.prerelease);
|
||||
};
|
||||
|
||||
function shouldRunStart({ isMaster, isWorkflowDispatch, botRun }) {
|
||||
return isMaster && isWorkflowDispatch && !botRun;
|
||||
}
|
||||
|
||||
function shouldRunPromote({ isReleaseBranch, isWorkflowDispatch, botRun }) {
|
||||
return isReleaseBranch && isWorkflowDispatch && !botRun;
|
||||
}
|
||||
|
||||
function shouldRunChangesets({ isReleaseBranch, isPush, isWorkflowDispatch, botRun }) {
|
||||
return (isReleaseBranch && isPush) || (isReleaseBranch && isWorkflowDispatch && botRun);
|
||||
}
|
||||
|
||||
function shouldRunPublish({ isReleaseBranch, isPush, hasPendingChangesets }) {
|
||||
return isReleaseBranch && isPush && !hasPendingChangesets;
|
||||
}
|
||||
|
||||
function shouldRunMerge({
|
||||
isReleaseBranch,
|
||||
isPush,
|
||||
prerelease,
|
||||
isCurrentFinalVersion,
|
||||
hasPendingChangesets,
|
||||
prBackExists,
|
||||
}) {
|
||||
return isReleaseBranch && isPush && !prerelease && isCurrentFinalVersion && !hasPendingChangesets && prBackExists;
|
||||
}
|
||||
|
||||
async function getState({ github, context, core }) {
|
||||
// Variables not in the context
|
||||
const refName = process.env.GITHUB_REF_NAME;
|
||||
const botRun = process.env.TRIGGERING_ACTOR === 'github-actions[bot]';
|
||||
|
||||
const { changesets, preState } = await readChangesetState();
|
||||
|
||||
// Static vars
|
||||
const state = {
|
||||
refName,
|
||||
hasPendingChangesets: changesets.length > 0,
|
||||
prerelease: preState?.mode === 'pre',
|
||||
isMaster: refName === 'master',
|
||||
isReleaseBranch: refName.startsWith('release-v'),
|
||||
isWorkflowDispatch: context.eventName === 'workflow_dispatch',
|
||||
isPush: context.eventName === 'push',
|
||||
isCurrentFinalVersion: !version.includes('-rc.'),
|
||||
botRun,
|
||||
};
|
||||
|
||||
// Async vars
|
||||
const { data: prs } = await github.rest.pulls.list({
|
||||
owner: context.repo.owner,
|
||||
repo: context.repo.repo,
|
||||
head: `${context.repo.owner}:merge/${state.refName}`,
|
||||
base: 'master',
|
||||
state: 'open',
|
||||
});
|
||||
|
||||
state.prBackExists = prs.length === 0;
|
||||
|
||||
// Log every state value in debug mode
|
||||
if (core.isDebug()) for (const [key, value] of Object.entries(state)) core.debug(`${key}: ${value}`);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
// From https://github.com/changesets/action/blob/v1.4.1/src/readChangesetState.ts
|
||||
async function readChangesetState(cwd = process.cwd()) {
|
||||
const preState = await readPreState(cwd);
|
||||
const isInPreMode = preState !== undefined && preState.mode === 'pre';
|
||||
|
||||
let changesets = await readChangesets(cwd);
|
||||
|
||||
if (isInPreMode) {
|
||||
changesets = changesets.filter(x => !preState.changesets.includes(x.id));
|
||||
}
|
||||
|
||||
return {
|
||||
preState: isInPreMode ? preState : undefined,
|
||||
changesets,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user