|
| 1 | +name: Update Licenses on Dependency Changes |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - "**" |
| 7 | + pull_request: |
| 8 | + branches: |
| 9 | + - "**" |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: write |
| 13 | + pull-requests: write |
| 14 | + actions: write |
| 15 | + |
| 16 | +jobs: |
| 17 | + update-licenses: |
| 18 | + runs-on: ubuntu-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout code |
| 22 | + uses: actions/checkout@v4 |
| 23 | + with: |
| 24 | + fetch-depth: 0 # Fetch all history for proper branch tracking |
| 25 | + |
| 26 | + - name: Set up pnpm |
| 27 | + uses: pnpm/action-setup@v4 |
| 28 | + |
| 29 | + - name: Set up Node.js |
| 30 | + uses: actions/setup-node@v4 |
| 31 | + with: |
| 32 | + node-version: "lts/*" |
| 33 | + cache: pnpm |
| 34 | + |
| 35 | + - name: Install dependencies |
| 36 | + run: pnpm install --frozen-lockfile |
| 37 | + |
| 38 | + - name: Generate licenses.json |
| 39 | + run: | |
| 40 | + pnpm add -g license-checker |
| 41 | + license-checker --json > licenses.json |
| 42 | +
|
| 43 | + - name: Display licenses.json content |
| 44 | + run: cat licenses.json |
| 45 | + |
| 46 | + - name: Commit and Push Updated Licenses |
| 47 | + env: |
| 48 | + PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }} |
| 49 | + run: | |
| 50 | + git config --global user.name "github-actions[bot]" |
| 51 | + git config --global user.email "github-actions[bot]@users.noreply.github.com" |
| 52 | +
|
| 53 | + BRANCH_NAME="${GITHUB_HEAD_REF:-${GITHUB_REF_NAME:-$(git rev-parse --abbrev-ref HEAD)}}" |
| 54 | + echo "Working on branch: $BRANCH_NAME" |
| 55 | +
|
| 56 | + # Fetch latest changes from the remote repository |
| 57 | + git fetch origin $BRANCH_NAME |
| 58 | +
|
| 59 | + # Ensure we are on the correct branch |
| 60 | + git checkout -B $BRANCH_NAME origin/$BRANCH_NAME |
| 61 | +
|
| 62 | + # Reset any local changes to match remote branch |
| 63 | + git reset --hard origin/$BRANCH_NAME |
| 64 | +
|
| 65 | + # Stage the updated licenses.json |
| 66 | + git add licenses.json |
| 67 | +
|
| 68 | + if ! git diff --cached --quiet; then |
| 69 | + git commit -m "chore: Update licenses.json" |
| 70 | +
|
| 71 | + # Explicitly set upstream branch to avoid stale refs |
| 72 | + git branch --set-upstream-to=origin/$BRANCH_NAME |
| 73 | +
|
| 74 | + # Force push with lease (safe force push) |
| 75 | + git push --force-with-lease https://x-access-token:${PERSONAL_ACCESS_TOKEN}@github.com/${{ github.repository }} HEAD:$BRANCH_NAME |
| 76 | + else |
| 77 | + echo "No changes to commit." |
| 78 | + fi |
| 79 | +
|
| 80 | + - name: Verify Commit |
| 81 | + run: git log -n 3 --oneline |
| 82 | + |
| 83 | + - name: Trigger test-and-build Workflow |
| 84 | + env: |
| 85 | + PERSONAL_ACCESS_TOKEN: ${{ secrets.PERSONAL_ACCESS_TOKEN }} |
| 86 | + run: | |
| 87 | + curl -X POST \ |
| 88 | + -H "Authorization: token ${PERSONAL_ACCESS_TOKEN}" \ |
| 89 | + -H "Accept: application/vnd.github.v3+json" \ |
| 90 | + https://api.github.com/repos/${{ github.repository }}/dispatches \ |
| 91 | + -d '{"event_type": "trigger-test-and-build"}' |
0 commit comments