|
| 1 | +name: upgrade-dependencies |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: {} |
| 5 | + schedule: |
| 6 | + - cron: "0 0 * * *" |
| 7 | + |
| 8 | +jobs: |
| 9 | + upgrade-deps: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + steps: |
| 12 | + - uses: actions/checkout@main |
| 13 | + - uses: actions/setup-node@main |
| 14 | + with: |
| 15 | + node-version: 18 |
| 16 | + - name: Cache node modules |
| 17 | + uses: actions/cache@main |
| 18 | + env: |
| 19 | + cache-name: cache-node-${{ matrix.node }}-modules |
| 20 | + with: |
| 21 | + path: ~/.npm |
| 22 | + key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }} |
| 23 | + restore-keys: | |
| 24 | + ${{ runner.os }}-build-${{ env.cache-name }}- |
| 25 | + ${{ runner.os }}-build- |
| 26 | + ${{ runner.os }}- |
| 27 | + - name: Setup Git |
| 28 | + run: | |
| 29 | + git config --local user.name "github-actions[bot]" |
| 30 | + git config --local user.email "github-actions[bot]@users.noreply.github.com" |
| 31 | + git config --local pull.rebase true |
| 32 | +
|
| 33 | + # work around "insufficient permission for adding an object to repository database .git/object" issue |
| 34 | + sudo chmod -R ugo+rwX .git |
| 35 | + - name: Check for updates |
| 36 | + id: check-updates |
| 37 | + run: | |
| 38 | + set -ex |
| 39 | + npm ci &> /dev/null |
| 40 | + npx ncu |
| 41 | + npm i &> /dev/null |
| 42 | + npm audit fix --quiet --no-progress --no-fund || true |
| 43 | + npm run fix &> /dev/null || true |
| 44 | +
|
| 45 | + git add -u |
| 46 | + git update-index --refresh |
| 47 | + if ! git diff-index --quiet HEAD --; then |
| 48 | + echo "is-changed=1" >> $GITHUB_OUTPUT |
| 49 | + fi |
| 50 | + - name: Create a PR |
| 51 | + if: steps.check-updates.outputs.is-changed |
| 52 | + id: create-pr |
| 53 | + run: | |
| 54 | + npm version patch |
| 55 | + PKG_VERSION="$(node -e 'process.stdout.write(require("./package.json").version)')" |
| 56 | +
|
| 57 | + REMOTE_REPO="https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git" |
| 58 | + CURRENT_BRANCH="$(git branch --show-current)" |
| 59 | + NEW_BRANCH="v${PKG_VERSION}" |
| 60 | +
|
| 61 | + if git ls-remote --exit-code --heads "${REMOTE_REPO}" "${NEW_BRANCH}" > /dev/null; then |
| 62 | + # PR already exists |
| 63 | + exit 0 |
| 64 | + fi |
| 65 | +
|
| 66 | + git commit -a -m "${PKG_VERSION}" --no-verify |
| 67 | + git pull "${REMOTE_REPO}" "${CURRENT_BRANCH}" |
| 68 | + git checkout -b "${NEW_BRANCH}" |
| 69 | + git push "${REMOTE_REPO}" "HEAD:${NEW_BRANCH}" |
| 70 | +
|
| 71 | + PR_URL=$(gh pr create -B "${CURRENT_BRANCH}" -H "${NEW_BRANCH}" -f) |
| 72 | + echo "pr-url=${PR_URL}" >> $GITHUB_OUTPUT |
| 73 | + env: |
| 74 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 75 | + - name: Approve and merge the PR |
| 76 | + if: steps.create-pr.outputs.pr-url |
| 77 | + run: | |
| 78 | + gh pr review --approve "${PR_URL}" |
| 79 | + gh pr merge --auto --delete-branch --rebase "${PR_URL}" |
| 80 | + env: |
| 81 | + GITHUB_TOKEN: ${{ secrets.GH_PAT }} |
| 82 | + PR_URL: ${{ steps.create-pr.outputs.pr-url }} |
0 commit comments