bump_version #112
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Workflow which bumps the version of all packages in the repo | |
name: bump_version | |
on: | |
# run manually | |
workflow_dispatch: | |
inputs: | |
version: | |
type: string | |
description: Version number (e.g. 1.2.3) | |
required: true | |
src: | |
type: string | |
description: Source branch name (e.g. staging) | |
required: true | |
default: staging | |
interim: | |
type: string | |
description: Interim branch name (e.g. release/1.2.3). Leave empty to use 'release/<version_number>'. | |
required: false | |
dest: | |
type: string | |
description: Destination branch name (e.g. main) | |
required: true | |
default: main | |
concurrency: | |
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} | |
cancel-in-progress: true | |
env: | |
GITHUB_TOKEN: ${{ secrets.PROSOPONATOR_PAT }} | |
GH_TOKEN: ${{ secrets.PROSOPONATOR_PAT }} | |
CARGO_TERM_COLOR: always | |
NODE_OPTIONS: "--max-old-space-size=4096" | |
defaults: | |
run: | |
shell: bash | |
jobs: | |
bump_version: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Print contexts | |
env: | |
GITHUB_CONTEXT: ${{ toJson(github) }} | |
ENV_CONTEXT: ${{ toJson(env) }} | |
VARS_CONTEXT: ${{ toJson(vars) }} | |
JOB_CONTEXT: ${{ toJson(job) }} | |
STEPS_CONTEXT: ${{ toJson(steps) }} | |
RUNNER_CONTEXT: ${{ toJson(runner) }} | |
SECRETS_CONTEXT: ${{ toJson(secrets) }} | |
STRATEGY_CONTEXT: ${{ toJson(strategy) }} | |
MATRIX_CONTEXT: ${{ toJson(matrix) }} | |
NEEDS_CONTEXT: ${{ toJson(needs) }} | |
INPUTS_CONTEXT: ${{ toJson(inputs) }} | |
run: | | |
echo "******************************" | |
echo "github:" "$GITHUB_CONTEXT" | |
echo "******************************" | |
echo "env:" "$ENV_CONTEXT" | |
echo "******************************" | |
echo "vars:" "$VARS_CONTEXT" | |
echo "******************************" | |
echo "job:" "$JOB_CONTEXT" | |
echo "******************************" | |
echo "steps:" "$STEPS_CONTEXT" | |
echo "******************************" | |
echo "runner:" "$RUNNER_CONTEXT" | |
echo "******************************" | |
echo "secrets:" "$SECRETS_CONTEXT" | |
echo "******************************" | |
echo "strategy:" "$STRATEGY_CONTEXT" | |
echo "******************************" | |
echo "matrix:" "$MATRIX_CONTEXT" | |
echo "******************************" | |
echo "needs:" "$NEEDS_CONTEXT" | |
echo "******************************" | |
echo "inputs:" "$INPUTS_CONTEXT" | |
echo "******************************" | |
- uses: actions/checkout@v4 | |
with: | |
submodules: "recursive" | |
fetch-depth: 0 # Fetch all history for all branches | |
- uses: actions/setup-node@v4 | |
with: | |
node-version-file: 'package.json' | |
- run: npm i -g "npm@$(jq -r .engines.npm < package.json)" | |
- run: mkdir -p ~/.npm | |
- run: mkdir -p ~/.cache/Cypress | |
- name: Restore npm cache | |
if: ${{ runner.environment != 'self-hosted' }} # don't restore cache on self-hosted runners, network speed not good enough | |
uses: actions/cache/restore@v4 | |
with: | |
# must restore all cache dirs, and they must exist ahead of this! | |
path: | | |
~/.npm | |
~/.cache/Cypress | |
# note that restoring a cache in github is a pain. The trailing '-' matches any string after the '-', therefore 'abc-' would match a cache named 'abc-1234' or 'abc-5678', etc. | |
# the problem is 'abc-' will not match a cache named 'abc'! So if you're using wildcard cache name selectors like this, you need a field that changes as the suffix to become the wildcard | |
# here we're setting the key to an unused cache key so it falls back to the wildcard selector in `restore-keys` | |
key: some-unused-cache-key | |
restore-keys: | | |
npm-${{ runner.os }}-${{ runner.arch }}- | |
- name: Bump version | |
run: | | |
set -euo pipefail # stop on errors, print commands, fail on pipe fails | |
# set the author in git | |
git config user.name "prosoponator[bot]" | |
git config user.email "[email protected]" | |
# check the source branch (e.g. staging) is up-to-date with the dest branch (e.g. main) | |
if [ "$(git rev-list --count origin/${{ github.event.inputs.src }}..origin/${{ github.event.inputs.dest }})" -ne 0 ]; then | |
echo "Source branch ${{ github.event.inputs.src }} is not up-to-date with destination branch ${{ github.event.inputs.dest }}. PR destination branch into the source branch and try again." | |
exit 1 | |
else | |
echo "Source branch ${{ github.event.inputs.src }} is up-to-date with destination branch ${{ github.event.inputs.dest }}" | |
fi | |
# checkout the src branch | |
git checkout ${{ github.event.inputs.src }} | |
INTERIM="${{ github.event.inputs.interim}}" | |
# set the interim branch name to release/<version_number> if not provided | |
INTERIM="${INTERIM:-release/${{ github.event.inputs.version }}}" | |
# make a new branch for the version changes | |
git switch -c $INTERIM | |
pkgJsons=$(find "${{ github.workspace }}" -type f -name "package.json" -not -path "**/node_modules/**" -not -path "**/.next/**" | xargs -I "{}" echo {} | sed s/\"//g) | |
echo $pkgJsons | |
# get the package "name" out of each pkgJson | |
pkgNames=$(xargs -I % sh -c "jq -r '.name' %" <<< "$pkgJsons") | |
echo $pkgNames | |
while IFS= read -r pkgJson; do | |
echo "Bumping version in $pkgJson" | |
# for each package in the workspace, set the version number | |
cat $pkgJson | jq ".version = \"${{ github.event.inputs.version }}\"" > tmp | |
mv tmp $pkgJson | |
while IFS= read -r pkgName; do | |
# for each package in the workspace, check whether it is in the dependencies of the current package | |
# if so, bump the version number in the dependencies | |
cat $pkgJson | jq "if .dependencies[\"$pkgName\"] then .dependencies[\"$pkgName\"] = \"${{ github.event.inputs.version }}\" else . end" > tmp | |
mv tmp $pkgJson | |
cat $pkgJson | jq "if .devDependencies[\"$pkgName\"] then .devDependencies[\"$pkgName\"] = \"${{ github.event.inputs.version }}\" else . end" > tmp | |
mv tmp $pkgJson | |
done <<< "$pkgNames" | |
done <<< "$pkgJsons" | |
# need to bump the PROSOPO_PACKAGE_VERSION variable in env files | |
envFiles=$(find . -iregex ".*\/\.?env\.\w+" -not -iname "*\.js" -not -iname "*\.ts" -not -path "./node_modules/**") | |
echo "Bumping version in env files: $envFiles" | |
for envFile in $envFiles; do | |
sed -i "s/PROSOPO_PACKAGE_VERSION=.*/PROSOPO_PACKAGE_VERSION=${{ github.event.inputs.version }}/g" $envFile | |
done | |
# the json may be poorly formatted now, so we need to fix it | |
npm i | |
npm run -w @prosopo/scripts build | |
npm run -w @prosopo/lint build | |
npm run lint-fix | |
# commit the version changes | |
git add . | |
git commit -m "Bump version to ${{ github.event.inputs.version }}" | |
# push version changes | |
git push --set-upstream origin $INTERIM | |
# fetch the dest branch | |
git fetch origin ${{ github.event.inputs.dest }} | |
# create a PR for the release | |
gh pr create --base ${{ github.event.inputs.dest }} --title "Release ${{ github.event.inputs.version }}" --fill | |