|
| 1 | +name: 04 - Update pnpm Lock |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | + inputs: |
| 6 | + target_branch: |
| 7 | + description: 'Branch that should receive a refreshed pnpm-lock.yaml' |
| 8 | + required: true |
| 9 | + type: string |
| 10 | + |
| 11 | +concurrency: |
| 12 | + group: 'workflow-${{ github.workflow }}-${{ inputs.target_branch }}' |
| 13 | + cancel-in-progress: true |
| 14 | + |
| 15 | +jobs: |
| 16 | + validate-branch: |
| 17 | + runs-on: ubuntu-latest |
| 18 | + outputs: |
| 19 | + target_branch: ${{ steps.validate.outputs.target_branch }} |
| 20 | + steps: |
| 21 | + - name: Validate target branch |
| 22 | + id: validate |
| 23 | + run: | |
| 24 | + RAW_TARGET_BRANCH='${{ inputs.target_branch }}' |
| 25 | + TARGET_BRANCH="${RAW_TARGET_BRANCH#refs/heads/}" |
| 26 | + TARGET_BRANCH="${TARGET_BRANCH#origin/}" |
| 27 | +
|
| 28 | + if [ -z "$TARGET_BRANCH" ]; then |
| 29 | + echo 'The workflow requires a target branch.' >&2 |
| 30 | + exit 1 |
| 31 | + fi |
| 32 | +
|
| 33 | + if [ "${TARGET_BRANCH#refs/}" != "$TARGET_BRANCH" ]; then |
| 34 | + echo "Use a branch name instead of a ref: '$RAW_TARGET_BRANCH'." >&2 |
| 35 | + exit 1 |
| 36 | + fi |
| 37 | +
|
| 38 | + if ! LC_ALL=C printf '%s' "$TARGET_BRANCH" | grep -Eq '^[ -~]+$'; then |
| 39 | + echo "Branch '$TARGET_BRANCH' must only contain printable ASCII characters." >&2 |
| 40 | + exit 1 |
| 41 | + fi |
| 42 | +
|
| 43 | + if ! git check-ref-format --branch "$TARGET_BRANCH" >/dev/null 2>&1; then |
| 44 | + echo "Branch '$TARGET_BRANCH' is not a valid branch name." >&2 |
| 45 | + exit 1 |
| 46 | + fi |
| 47 | +
|
| 48 | + case "$TARGET_BRANCH" in |
| 49 | + develop|release/*) |
| 50 | + echo "The workflow must not run for branch '$TARGET_BRANCH'." >&2 |
| 51 | + exit 1 |
| 52 | + ;; |
| 53 | + esac |
| 54 | +
|
| 55 | + echo "target_branch=$TARGET_BRANCH" >> "$GITHUB_OUTPUT" |
| 56 | +
|
| 57 | + update: |
| 58 | + needs: validate-branch |
| 59 | + runs-on: ubuntu-latest |
| 60 | + outputs: |
| 61 | + changed: ${{ steps.detect.outputs.changed }} |
| 62 | + steps: |
| 63 | + - name: Checkout target branch |
| 64 | + uses: actions/checkout@v6 |
| 65 | + with: |
| 66 | + ref: ${{ needs.validate-branch.outputs.target_branch }} |
| 67 | + persist-credentials: false |
| 68 | + |
| 69 | + - name: Install pnpm |
| 70 | + uses: pnpm/action-setup@v5 |
| 71 | + with: |
| 72 | + version: 10 |
| 73 | + run_install: false |
| 74 | + |
| 75 | + - name: Use Node.js |
| 76 | + uses: actions/setup-node@v6 |
| 77 | + with: |
| 78 | + node-version: 24 |
| 79 | + cache: pnpm |
| 80 | + |
| 81 | + - name: Refresh pnpm lock file |
| 82 | + run: pnpm install --ignore-scripts --lockfile-only --no-frozen-lockfile |
| 83 | + |
| 84 | + - name: Git status after lock file refresh |
| 85 | + run: git status |
| 86 | + |
| 87 | + - name: Check for lock file changes |
| 88 | + id: detect |
| 89 | + run: | |
| 90 | + if git diff --quiet -- pnpm-lock.yaml; then |
| 91 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 92 | + else |
| 93 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 94 | + fi |
| 95 | +
|
| 96 | + - name: Upload lock file artifact |
| 97 | + if: steps.detect.outputs.changed == 'true' |
| 98 | + uses: actions/upload-artifact@v7 |
| 99 | + with: |
| 100 | + name: pnpm-lock-${{ github.run_id }} |
| 101 | + path: pnpm-lock.yaml |
| 102 | + if-no-files-found: error |
| 103 | + retention-days: 1 |
| 104 | + |
| 105 | + push: |
| 106 | + needs: [validate-branch, update] |
| 107 | + if: needs.update.outputs.changed == 'true' |
| 108 | + runs-on: ubuntu-latest |
| 109 | + steps: |
| 110 | + - uses: actions/create-github-app-token@v3 |
| 111 | + id: app-token |
| 112 | + with: |
| 113 | + app-id: ${{ secrets.APP_ID }} |
| 114 | + private-key: ${{ secrets.PRIVATE_KEY }} |
| 115 | + |
| 116 | + - name: Get GitHub App User ID |
| 117 | + id: get-user-id |
| 118 | + run: echo "user-id=$(gh api "/users/${{ steps.app-token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT" |
| 119 | + env: |
| 120 | + GH_TOKEN: ${{ steps.app-token.outputs.token }} |
| 121 | + |
| 122 | + - name: Checkout target branch |
| 123 | + uses: actions/checkout@v6 |
| 124 | + with: |
| 125 | + ref: ${{ needs.validate-branch.outputs.target_branch }} |
| 126 | + token: ${{ steps.app-token.outputs.token }} |
| 127 | + |
| 128 | + - name: Download lock file artifact |
| 129 | + uses: actions/download-artifact@v8 |
| 130 | + with: |
| 131 | + name: pnpm-lock-${{ github.run_id }} |
| 132 | + path: lockfile-artifact |
| 133 | + |
| 134 | + - name: Apply lock file update |
| 135 | + run: cp lockfile-artifact/pnpm-lock.yaml pnpm-lock.yaml |
| 136 | + |
| 137 | + - name: Stage lock file |
| 138 | + run: git add pnpm-lock.yaml |
| 139 | + |
| 140 | + - name: Git status after staging |
| 141 | + run: git status |
| 142 | + |
| 143 | + - name: Commit and push changes |
| 144 | + uses: stefanzweifel/git-auto-commit-action@v7 |
| 145 | + with: |
| 146 | + commit_message: 'chore: update pnpm-lock.yaml' |
| 147 | + # Disable internal git add so that only the file staged above is committed. |
| 148 | + file_pattern: '' |
| 149 | + commit_user_name: '${{ steps.app-token.outputs.app-slug }}[bot]' |
| 150 | + commit_user_email: '${{ steps.get-user-id.outputs.user-id }}+${{ steps.app-token.outputs.app-slug }}[bot]@users.noreply.github.com' |
0 commit comments