Nx migrate #289
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
name: Nx migrate | |
on: | |
workflow_dispatch: | |
schedule: | |
# Every day at 6am UTC | |
- cron: '0 6 * * *' | |
jobs: | |
nx-migrate: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v4 | |
with: | |
token: ${{ secrets.GH_BOT_TOKEN }} | |
fetch-depth: 0 | |
- name: Set git metadata | |
run: | | |
git config user.name "${{ vars.SEMVER_COMMIT_NAME }}" | |
git config user.email "${{ vars.SEMVER_COMMIT_EMAIL }}" | |
- uses: actions/setup-node@v4 | |
with: | |
node-version: 20 | |
cache: 'yarn' | |
- run: yarn install --immutable | |
shell: bash | |
- name: Check versions whether @nx/workspace is outdated | |
id: workspace | |
run: | | |
CURRENT_VERSION=$(npm list @nx/workspace --depth=0 --json | jq -r '.dependencies[].version') | |
echo $CURRENT_VERSION | |
CURRENT_MAJOR=$(echo $CURRENT_VERSION | cut -d '.' -f 1) | |
echo $CURRENT_MAJOR | |
LATEST_VERSION=$(npm view @nx/workspace version) | |
echo $LATEST_VERSION | |
LATEST_MAJOR=$(echo $LATEST_VERSION | cut -d '.' -f 1) | |
echo $LATEST_MAJOR | |
MAJOR_UPDATE=$(test $LATEST_MAJOR -gt $CURRENT_MAJOR && echo true || echo false) | |
echo $MAJOR_UPDATE | |
IS_OUTDATED=$(test ! -z "$(npm outdated @nx/workspace)" && echo true || echo false) | |
echo $IS_OUTDATED | |
echo "currentVersion=$CURRENT_VERSION" >> $GITHUB_OUTPUT | |
echo "currentMajor=$CURRENT_MAJOR" >> $GITHUB_OUTPUT | |
echo "latestVersion=$LATEST_VERSION" >> $GITHUB_OUTPUT | |
echo "latestMajor=$LATEST_MAJOR" >> $GITHUB_OUTPUT | |
echo "majorUpdate=$MAJOR_UPDATE" >> $GITHUB_OUTPUT | |
echo "outdated=$IS_OUTDATED" >> $GITHUB_OUTPUT | |
- name: Update @nx/workspace | |
if: steps.workspace.outputs.outdated == 'true' | |
run: yarn nx migrate ${{ steps.workspace.outputs.latestVersion }} | |
- name: Update create-nx-workspace | |
if: steps.workspace.outputs.outdated == 'true' | |
run: yarn add create-nx-workspace@${{ steps.workspace.outputs.latestVersion }} | |
- name: Install dependencies | |
if: steps.workspace.outputs.outdated == 'true' | |
run: yarn install --no-immutable | |
- name: Check for migrations | |
id: migrations | |
if: steps.workspace.outputs.outdated == 'true' | |
run: | | |
MIGRATIONS_EXIST=$(test -f migrations.json && echo true || echo false) | |
echo $MIGRATIONS_EXIST | |
echo "exist=$MIGRATIONS_EXIST" >> $GITHUB_OUTPUT | |
- name: Run @nx/workspace migrations | |
if: steps.migrations.outputs.exist == 'true' | |
run: yarn nx migrate --run-migrations | |
- name: Update nx semantic version in packages | |
if: steps.workspace.outputs.outdated == 'true' | |
uses: jacobtomlinson/gha-find-replace@v3 | |
with: | |
include: packages/**/package.json | |
# Match lines with "create-nx-workspace", "nx" or "@nx/*" with semantic versions (x.y.z). | |
# Package, version and separator are divided into match groups: | |
# Eg. "@nx/express": "18.0.0" | |
# => "<group 1><group 2><group 3>" | |
# "<@nx/express><": "><18.0.0>" | |
# Match group 3 is replaced with the latest version, | |
# hence the value for current version is not considered. | |
find: (\"(?:create\-nx\-workspace|nx|@nx\/[^\"]*))(\":\s\")(\d+\.\d+\.\d+) | |
replace: ${1}${2}${{ steps.workspace.outputs.latestVersion }} | |
- name: Update nx dynamic major version in packages | |
if: steps.workspace.outputs.outdated == 'true' | |
uses: jacobtomlinson/gha-find-replace@v3 | |
with: | |
include: packages/**/package.json | |
# Logic is the same as the previous step, | |
# except dynamic versions are matched (<major>.x) | |
find: (\"(?:create\-nx\-workspace|nx|@nx\/[^\"]*))(\":\s\")(\d+)(\.x) | |
replace: ${1}${2}${{ steps.workspace.outputs.latestMajor }}${4} | |
- name: Save file changes state before the tests | |
if: steps.workspace.outputs.outdated == 'true' | |
run: | | |
git add . | |
git commit -m "temporary save state" --no-verify | |
- name: Run tests | |
id: test | |
if: steps.workspace.outputs.outdated == 'true' | |
run: | | |
yarn nx run-many -t lint,test,build -c ci --no-cloud | |
yarn nx release -d | |
continue-on-error: true | |
- name: Run E2E | |
id: e2e | |
if: steps.workspace.outputs.outdated == 'true' && steps.test.outcome == 'success' | |
run: yarn nx e2e nx-payload-e2e --no-cloud | |
timeout-minutes: 30 | |
continue-on-error: true | |
- name: Restore file changes state to before the tests | |
if: steps.workspace.outputs.outdated == 'true' | |
run: | | |
git checkout -- . | |
git reset HEAD^ | |
- name: Create Pull Request | |
id: cpr | |
if: steps.workspace.outputs.outdated == 'true' | |
uses: peter-evans/create-pull-request@v7 | |
env: | |
currentVersion: ${{ steps.workspace.outputs.currentVersion }} | |
latestVersion: ${{ steps.workspace.outputs.latestVersion }} | |
with: | |
token: ${{ secrets.GH_BOT_TOKEN }} | |
commit-message: 'build: 📦 update nx workspace to ${{ env.latestVersion }}' | |
branch: update-nx-workspace-${{ env.latestVersion }} | |
delete-branch: true | |
title: Update @nx/workspace to ${{ env.latestVersion }} | |
body: | | |
Update Nx from ${{ env.currentVersion }} to ${{ env.latestVersion }} | |
> Auto-generated by [create-pull-request][1] | |
[1]: https://github.com/peter-evans/create-pull-request | |
labels: automated pr | |
assignees: hakalb | |
- name: Enable Pull Request Automerge | |
if: vars.NX_AUTO_MIGRATE == 'true' && steps.workspace.outputs.majorUpdate != 'true' && steps.cpr.outputs.pull-request-operation == 'created' | |
run: gh pr merge --rebase --auto ${{ steps.cpr.outputs.pull-request-number }} | |
env: | |
GH_TOKEN: ${{ secrets.GH_BOT_TOKEN }} | |
- name: Add Pull Request comment for disabled Automerge | |
if: vars.NX_AUTO_MIGRATE == 'true' && steps.workspace.outputs.majorUpdate == 'true' && steps.cpr.outputs.pull-request-operation == 'created' | |
run: gh pr comment ${{ steps.cpr.outputs.pull-request-number }} -b "Automerge is disabled for major version migrations" | |
env: | |
GH_TOKEN: ${{ secrets.GH_BOT_TOKEN }} | |
- name: Autoclose deprecated Pull Requests | |
if: steps.cpr.outputs.pull-request-operation == 'created' | |
run: gh pr list --label "${{env.label}}" --json number | jq -r '.[].number|select(. != ${{env.last-pr}})' | xargs -I % gh pr close % -d -c "${{ env.comment }}" | |
env: | |
last-pr: ${{ steps.cpr.outputs.pull-request-number }} | |
label: 'automated pr' | |
comment: 'Autoclosed - Replaced by a new PR' | |
GH_TOKEN: ${{ github.token }} |