|
1 | | -name: update lockfile |
| 1 | +--- |
| 2 | +name: "lazy-lock: update lazy.nvim dependencies (Verified)" |
2 | 3 | on: |
3 | | - # Scheduled update (each day) |
4 | | - schedule: [{ cron: "30 01 * * *" }] |
| 4 | + workflow_dispatch: # allows manual triggering |
| 5 | + schedule: |
| 6 | + # Scheduled update (each day) |
| 7 | + - cron: 30 01 * * * |
| 8 | + |
| 9 | +env: |
| 10 | + BRANCH: "main" |
| 11 | + COMMIT_MESSAGE: "chore(lockfile): auto update lazy-lock.json" |
5 | 12 |
|
6 | 13 | jobs: |
7 | 14 | update-lockfile: |
8 | 15 | if: github.repository_owner == 'ayamir' |
9 | | - runs-on: ubuntu-latest |
10 | 16 | permissions: |
11 | 17 | contents: write |
| 18 | + id-token: write |
| 19 | + runs-on: ubuntu-latest |
| 20 | + |
12 | 21 | steps: |
13 | | - - uses: actions/checkout@v5 |
| 22 | + - name: Checkout repository |
| 23 | + uses: actions/checkout@v5 |
14 | 24 | with: |
15 | 25 | fetch-depth: 0 # Required to count the commits |
16 | | - - uses: andstor/file-existence-action@v3 |
| 26 | + |
| 27 | + - name: Check if lockfile existed |
| 28 | + uses: andstor/file-existence-action@v3 |
17 | 29 | id: check_lockfile |
18 | 30 | with: |
19 | 31 | files: "lazy-lock.json" |
| 32 | + |
20 | 33 | - name: Run count-new-commits |
| 34 | + id: new-commits |
21 | 35 | run: | |
22 | | - echo "NEW_COMMIT_COUNT=$(git log --oneline --since '24 hours ago' --perl-regexp --author='^((?!github-actions).*)$' | wc -l)" >> "$GITHUB_ENV" |
23 | | - - uses: rhysd/action-setup-vim@v1 |
24 | | - if: ${{ steps.check_lockfile.outputs.files_exists == 'true' && env.NEW_COMMIT_COUNT > 0 }} |
| 36 | + echo "new_commit_count=$(git log --oneline --since '24 hours ago' --perl-regexp --author='^((?!github-actions).*)$' | wc -l)" >> "$GITHUB_OUTPUT" |
| 37 | +
|
| 38 | + - name: Setup neovim |
| 39 | + uses: rhysd/action-setup-vim@v1 |
| 40 | + if: ${{ steps.check_lockfile.outputs.files_exists == 'true' && steps.new-commits.outputs.new_commit_count > 0 }} |
25 | 41 | with: |
26 | 42 | neovim: true |
27 | | - - name: Run lockfile-autoupdate |
28 | | - if: ${{ steps.check_lockfile.outputs.files_exists == 'true' && env.NEW_COMMIT_COUNT > 0 }} |
| 43 | + |
| 44 | + - name: Run lazy update |
| 45 | + if: ${{ steps.check_lockfile.outputs.files_exists == 'true' && steps.new-commits.outputs.new_commit_count > 0 }} |
29 | 46 | timeout-minutes: 5 |
30 | 47 | run: | |
31 | 48 | ./scripts/install.sh |
32 | 49 | nvim --headless "+Lazy! update" +qa |
33 | 50 | cp -pv "${HOME}/.config/nvim/lazy-lock.json" . |
34 | | - - uses: stefanzweifel/git-auto-commit-action@v6 |
35 | | - if: ${{ steps.check_lockfile.outputs.files_exists == 'true' && env.NEW_COMMIT_COUNT > 0 }} |
| 51 | +
|
| 52 | + - name: Detect modified files |
| 53 | + if: ${{ steps.check_lockfile.outputs.files_exists == 'true' && steps.new-commits.outputs.new_commit_count > 0 }} |
| 54 | + id: diff |
| 55 | + shell: bash |
| 56 | + run: | |
| 57 | + set -euo pipefail |
| 58 | + # List modified (tracked) files relative to HEAD. |
| 59 | + # If you only want specific patterns, add a grep here (e.g., grep -E '(^|/)lazy-lock\.json$'). |
| 60 | + mapfile -t changed < <(git ls-files -m --full-name) |
| 61 | +
|
| 62 | + if [ "${#changed[@]}" -eq 0 ]; then |
| 63 | + echo "changed=false" >> "$GITHUB_OUTPUT" |
| 64 | + echo "changed_files=" >> "$GITHUB_OUTPUT" |
| 65 | + exit 0 |
| 66 | + fi |
| 67 | +
|
| 68 | + # Join into a comma-separated string for downstream steps. |
| 69 | + IFS=',' read -r -a _ <<< "" |
| 70 | + changed_csv="$(printf "%s," "${changed[@]}")" |
| 71 | + changed_csv="${changed_csv%,}" |
| 72 | +
|
| 73 | + echo "Changed files:" |
| 74 | + printf ' - %s\n' "${changed[@]}" |
| 75 | +
|
| 76 | + echo "changed=true" >> "$GITHUB_OUTPUT" |
| 77 | + echo "changed_files=${changed_csv}" >> "$GITHUB_OUTPUT" |
| 78 | +
|
| 79 | + - name: Commit via REST Contents API (server-signed ??Verified) |
| 80 | + if: ${{ steps.diff.outputs.changed == 'true' && steps.new-commits.outputs.new_commit_count > 0 }} |
| 81 | + uses: actions/github-script@v8 |
| 82 | + env: |
| 83 | + CHANGED_FILES: ${{ steps.diff.outputs.changed_files }} |
36 | 84 | with: |
37 | | - commit_message: "chore(lockfile): auto update lazy-lock.json" |
38 | | - commit_user_name: "github-actions[bot]" |
39 | | - commit_user_email: "41898282+github-actions[bot]@users.noreply.github.com" |
40 | | - commit_author: "github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>" |
41 | | - file_pattern: "lazy-lock.json" |
| 85 | + script: | |
| 86 | + const fs = require('fs'); |
| 87 | + const owner = context.repo.owner; |
| 88 | + const repo = context.repo.repo; |
| 89 | + const branch = process.env.BRANCH; |
| 90 | + const message = process.env.COMMIT_MESSAGE; |
| 91 | +
|
| 92 | + const files = (process.env.CHANGED_FILES || '') |
| 93 | + .split(',') |
| 94 | + .map(s => s.trim()) |
| 95 | + .filter(Boolean); |
| 96 | +
|
| 97 | + for (const path of files) { |
| 98 | + const content = fs.readFileSync(path, { encoding: 'base64' }); |
| 99 | +
|
| 100 | + // Get existing sha if the file already exists |
| 101 | + let sha; |
| 102 | + try { |
| 103 | + const res = await github.rest.repos.getContent({ owner, repo, path, ref: branch }); |
| 104 | + if (!Array.isArray(res.data)) sha = res.data.sha; |
| 105 | + } catch (e) { |
| 106 | + if (e.status !== 404) throw e; |
| 107 | + } |
| 108 | +
|
| 109 | + // NOTE: author/committer intentionally omitted to allow platform signing |
| 110 | + const r = await github.rest.repos.createOrUpdateFileContents({ |
| 111 | + owner, repo, path, branch, |
| 112 | + message, |
| 113 | + content, |
| 114 | + sha |
| 115 | + }); |
| 116 | + core.info(`Committed ${path}: ${r.data.commit.sha}`); |
| 117 | + } |
0 commit comments