|
| 1 | +# This workflow is used to augment the capabilities of the renovate GitHub app by running a full |
| 2 | +# `nx migrate` when renovate opens a PR to change the version of @nx/workspace in package.json. |
| 3 | +# |
| 4 | +# You will therefore also notice that in the renovate configuration, we ignore any packages which |
| 5 | +# Nx will manage for us as part of `nx migrate` such as the remaining @nx/* packages. |
| 6 | + |
| 7 | +name: Nx Migrate |
| 8 | + |
| 9 | +on: |
| 10 | + # NOTE: Never use pull_request_target here because that would populate secrets for forks |
| 11 | + # Renovate creates branches directly on the main repo and acts like a trusted contributor |
| 12 | + pull_request: |
| 13 | + branches: [main] |
| 14 | + paths: |
| 15 | + - 'package.json' |
| 16 | + |
| 17 | +concurrency: |
| 18 | + group: ${{ github.workflow }}-${{ github.event.number || github.ref }} |
| 19 | + cancel-in-progress: true |
| 20 | + |
| 21 | +# Minimal permissions by default |
| 22 | +permissions: |
| 23 | + contents: read |
| 24 | + |
| 25 | +jobs: |
| 26 | + maybe_nx_migrate: |
| 27 | + # Only run if it was the renovate bot that triggered the workflow (otherwise we'll create a loop) |
| 28 | + if: contains('["renovate[bot]"]', github.actor) == true |
| 29 | + name: Run nx migrate if required |
| 30 | + runs-on: ubuntu-latest |
| 31 | + steps: |
| 32 | + - uses: actions/checkout@v5 |
| 33 | + with: |
| 34 | + # To allow us to perform the git diff we need the git history |
| 35 | + fetch-depth: 0 |
| 36 | + # To ensure we can push from a different user (and therefore cause actions to rerun) |
| 37 | + persist-credentials: false |
| 38 | + |
| 39 | + - name: Check if @nx/workspace was changed as part of the latest commit on the PR |
| 40 | + id: nx-workspace-package-check |
| 41 | + run: | |
| 42 | + git diff HEAD~1 -G"@nx/workspace" --exit-code package.json && echo "@nx/workspace unchanged" || echo "::set-output name=was-changed::true" |
| 43 | +
|
| 44 | + - uses: pnpm/action-setup@v4 |
| 45 | + if: ${{ steps.nx-workspace-package-check.outputs.was-changed == 'true' }} |
| 46 | + name: Install pnpm |
| 47 | + with: |
| 48 | + run_install: false |
| 49 | + |
| 50 | + - name: Install Node.js per package.json |
| 51 | + if: ${{ steps.nx-workspace-package-check.outputs.was-changed == 'true' }} |
| 52 | + uses: actions/setup-node@v5 |
| 53 | + with: |
| 54 | + # Use the volta.node property as the source of truth |
| 55 | + node-version-file: 'package.json' |
| 56 | + cache: 'pnpm' |
| 57 | + |
| 58 | + - name: Run nx migrate if @nx/workspace changed and commit the results |
| 59 | + if: ${{ steps.nx-workspace-package-check.outputs.was-changed == 'true' }} |
| 60 | + env: |
| 61 | + # We cannot use secrets.GITHUB_TOKEN for this because it is not permitted to kick off subsequent actions worfklow runs, so we use a fine grained PAT instead |
| 62 | + GITHUB_TOKEN: ${{ secrets.GH_FINE_GRAINED_PAT }} |
| 63 | + # We don't want to run any of our postinstall logic when Nx is invoking install behind the scenes |
| 64 | + SKIP_POSTINSTALL: 'true' |
| 65 | + run: | |
| 66 | + # Checkout the PR branch using the github CLI |
| 67 | + gh pr checkout ${{ github.event.pull_request.number }} |
| 68 | +
|
| 69 | + # Get the version of Nx we are migrating to |
| 70 | + NX_VERSION=$(node -e "console.log(require('./package.json').devDependencies['@nx/workspace'])") |
| 71 | +
|
| 72 | + # Revert renovate's changes to package.json and pnpm-lock.yaml so that it is a clean migrate from the status quo |
| 73 | + git checkout HEAD~1 -- package.json pnpm-lock.yaml |
| 74 | +
|
| 75 | + # We need to expect lock file changes to be applicable |
| 76 | + pnpm install --ignore-scripts --frozen-lockfile=false |
| 77 | +
|
| 78 | + pnpm nx migrate $NX_VERSION |
| 79 | +
|
| 80 | + # Sometimes Nx can require config formatting changes after a migrate command |
| 81 | + # We need to expect lock file changes to be applicable |
| 82 | + pnpm install --ignore-scripts --frozen-lockfile=false |
| 83 | + pnpm nx format |
| 84 | +
|
| 85 | + # migrations.json may or may not exist after running nx migrate |
| 86 | + if [ -f migrations.json ]; then |
| 87 | + pnpm nx migrate --run-migrations=migrations.json |
| 88 | +
|
| 89 | + # After we have run its migrations, we no longer need the migrations.json file |
| 90 | + rm migrations.json |
| 91 | + fi |
| 92 | +
|
| 93 | + # Ensure all the changed files are formatted appropriately |
| 94 | + pnpm format |
| 95 | +
|
| 96 | + # Commit all the changes to the PR (see note on not being able to use secrets.GITHUB_TOKEN for this) |
| 97 | + git config --global user.email "[email protected]" |
| 98 | + git config --global user.name "Coly010" |
| 99 | + git remote set-url origin https://x-access-token:[email protected]/$GITHUB_REPOSITORY.git |
| 100 | +
|
| 101 | + git add --all |
| 102 | + git commit -m "chore: run nx migrate for nx v$NX_VERSION" |
| 103 | + git push |
0 commit comments