|
| 1 | +# Reusable workflow for updating vendored NPM packages. |
| 2 | +# Caller workflows only need to define schedule/dispatch triggers, |
| 3 | +# the three package identifiers, and a custom extract script. |
| 4 | +# |
| 5 | +# To add a new package, create a thin caller workflow — see any update-*.yaml for an example. |
| 6 | + |
| 7 | +name: Reusable NPM Update |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +on: |
| 14 | + workflow_call: |
| 15 | + inputs: |
| 16 | + npm_package: |
| 17 | + description: 'NPM package name (e.g. exceljs, @ruffle-rs/ruffle)' |
| 18 | + required: true |
| 19 | + type: string |
| 20 | + target_dir: |
| 21 | + description: 'Target directory in the repo to vendor files into' |
| 22 | + required: true |
| 23 | + type: string |
| 24 | + branch_name: |
| 25 | + description: 'Branch name for the auto-update PR (e.g. auto-update/exceljs)' |
| 26 | + required: true |
| 27 | + type: string |
| 28 | + extract_script: |
| 29 | + description: 'Shell script to extract/copy files from $PKG_DIR into $TARGET_DIR' |
| 30 | + required: true |
| 31 | + type: string |
| 32 | + |
| 33 | +jobs: |
| 34 | + update: |
| 35 | + runs-on: ubuntu-latest |
| 36 | + env: |
| 37 | + NPM_PACKAGE: ${{ inputs.npm_package }} |
| 38 | + TARGET_DIR: ${{ inputs.target_dir }} |
| 39 | + BRANCH_NAME: ${{ inputs.branch_name }} |
| 40 | + steps: |
| 41 | + - name: Checkout code |
| 42 | + uses: actions/checkout@v6 |
| 43 | + |
| 44 | + - name: Setup Node.js |
| 45 | + uses: actions/setup-node@v6 |
| 46 | + with: |
| 47 | + node-version: '24' |
| 48 | + |
| 49 | + - name: Fetch latest version |
| 50 | + id: fetch |
| 51 | + run: | |
| 52 | + LATEST=$(npm view "$NPM_PACKAGE" version 2>/dev/null) |
| 53 | + echo "version=$LATEST" >> "$GITHUB_OUTPUT" |
| 54 | + echo "Latest $NPM_PACKAGE version: $LATEST" |
| 55 | +
|
| 56 | + - name: Download and extract package |
| 57 | + run: | |
| 58 | + set -euo pipefail |
| 59 | + TMP=$(mktemp -d) |
| 60 | + cd "$TMP" |
| 61 | + npm pack "$NPM_PACKAGE" > /dev/null 2>&1 |
| 62 | + mkdir -p extracted |
| 63 | + tar -xzf *.tgz -C extracted |
| 64 | + cd - |
| 65 | + echo "PKG_DIR=$TMP/extracted/package" >> "$GITHUB_ENV" |
| 66 | +
|
| 67 | + - name: Extract files |
| 68 | + run: | |
| 69 | + set -euo pipefail |
| 70 | + ${{ inputs.extract_script }} |
| 71 | +
|
| 72 | + - name: Check for changes |
| 73 | + id: changes |
| 74 | + run: | |
| 75 | + if [[ -n $(git status --porcelain) ]]; then |
| 76 | + echo "has_changes=true" >> "$GITHUB_OUTPUT" |
| 77 | + else |
| 78 | + echo "has_changes=false" >> "$GITHUB_OUTPUT" |
| 79 | + echo "No changes detected." |
| 80 | + fi |
| 81 | +
|
| 82 | + - name: Remove index.html for GenerateDirectoryTree regeneration |
| 83 | + if: steps.changes.outputs.has_changes == 'true' |
| 84 | + run: | |
| 85 | + find "$TARGET_DIR" -name 'index.html' -exec rm -f {} + 2>/dev/null || true |
| 86 | +
|
| 87 | + - name: Create Pull Request |
| 88 | + if: steps.changes.outputs.has_changes == 'true' |
| 89 | + uses: peter-evans/create-pull-request@v7 |
| 90 | + with: |
| 91 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 92 | + commit-message: 'chore: update ${{ inputs.npm_package }} to ${{ steps.fetch.outputs.version }}' |
| 93 | + branch: ${{ inputs.branch_name }} |
| 94 | + delete-branch: true |
| 95 | + title: 'chore: update `${{ inputs.npm_package }}` to ${{ steps.fetch.outputs.version }}' |
| 96 | + body: | |
| 97 | + ## Automated Update |
| 98 | +
|
| 99 | + | Package | Version | Directory | |
| 100 | + |---------|---------|-----------| |
| 101 | + | `${{ inputs.npm_package }}` | `${{ steps.fetch.outputs.version }}` | `${{ inputs.target_dir }}/` | |
| 102 | +
|
| 103 | + --- |
| 104 | + _Auto-generated by updating **`${{ inputs.npm_package }}`**. Please review before merging._ |
| 105 | + labels: | |
| 106 | + automated |
| 107 | + dependencies |
0 commit comments