|
| 1 | +# Automatically run `cargo update` periodically |
| 2 | + |
| 3 | +--- |
| 4 | +name: Bump dependencies in Cargo.lock |
| 5 | +on: |
| 6 | + schedule: |
| 7 | + # Run weekly |
| 8 | + - cron: '0 0 * * Sun' |
| 9 | + workflow_dispatch: |
| 10 | + # Needed so we can run it manually |
| 11 | +permissions: |
| 12 | + contents: read |
| 13 | +defaults: |
| 14 | + run: |
| 15 | + shell: bash |
| 16 | +env: |
| 17 | + # So cargo doesn't complain about unstable features |
| 18 | + RUSTC_BOOTSTRAP: 1 |
| 19 | + PR_TITLE: Weekly `cargo update` |
| 20 | + PR_MESSAGE: | |
| 21 | + Automation to keep dependencies in `Cargo.lock` current. |
| 22 | +
|
| 23 | + The following is the output from `cargo update`: |
| 24 | + COMMIT_MESSAGE: "cargo update \n\n" |
| 25 | + |
| 26 | +jobs: |
| 27 | + not-waiting-on-bors: |
| 28 | + name: skip if S-waiting-on-bors |
| 29 | + runs-on: ubuntu-latest |
| 30 | + steps: |
| 31 | + - env: |
| 32 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + run: | |
| 34 | + # Fetch state and labels of PR |
| 35 | + # Or exit successfully if PR does not exist |
| 36 | + JSON=$(gh pr view cargo_update --repo $GITHUB_REPOSITORY --json labels,state || exit 0) |
| 37 | + STATE=$(echo "$JSON" | jq -r '.state') |
| 38 | + WAITING_ON_BORS=$(echo "$JSON" | jq '.labels[] | any(.name == "S-waiting-on-bors"; .)') |
| 39 | +
|
| 40 | + # Exit with error if open and S-waiting-on-bors |
| 41 | + if [[ "$STATE" == "OPEN" && "$WAITING_ON_BORS" == "true" ]]; then |
| 42 | + exit 1 |
| 43 | + fi |
| 44 | +
|
| 45 | + update: |
| 46 | + name: update dependencies |
| 47 | + needs: not-waiting-on-bors |
| 48 | + runs-on: ubuntu-latest |
| 49 | + steps: |
| 50 | + - name: checkout the source code |
| 51 | + uses: actions/checkout@v3 |
| 52 | + with: |
| 53 | + submodules: recursive |
| 54 | + - name: install the bootstrap toolchain |
| 55 | + run: | |
| 56 | + # Extract the stage0 version |
| 57 | + TOOLCHAIN=$(jq -r '.compiler | {version,date} | join("-")' -- src/stage0.json) |
| 58 | + # Install and set as default |
| 59 | + rustup toolchain install --no-self-update --profile minimal $TOOLCHAIN |
| 60 | + rustup default $TOOLCHAIN |
| 61 | +
|
| 62 | + - name: cargo update |
| 63 | + # Remove first line that always just says "Updating crates.io index" |
| 64 | + run: cargo update 2>&1 | sed '/crates.io index/d' | tee -a cargo_update.log |
| 65 | + - name: upload Cargo.lock artifact for use in PR |
| 66 | + uses: actions/upload-artifact@v3 |
| 67 | + with: |
| 68 | + name: Cargo-lock |
| 69 | + path: Cargo.lock |
| 70 | + retention-days: 1 |
| 71 | + - name: upload cargo-update log artifact for use in PR |
| 72 | + uses: actions/upload-artifact@v3 |
| 73 | + with: |
| 74 | + name: cargo-updates |
| 75 | + path: cargo_update.log |
| 76 | + retention-days: 1 |
| 77 | + |
| 78 | + pr: |
| 79 | + name: amend PR |
| 80 | + needs: update |
| 81 | + runs-on: ubuntu-latest |
| 82 | + permissions: |
| 83 | + contents: write |
| 84 | + pull-requests: write |
| 85 | + steps: |
| 86 | + - name: checkout the source code |
| 87 | + uses: actions/checkout@v3 |
| 88 | + |
| 89 | + - name: download Cargo.lock from update job |
| 90 | + uses: actions/download-artifact@v3 |
| 91 | + with: |
| 92 | + name: Cargo-lock |
| 93 | + - name: download cargo-update log from update job |
| 94 | + uses: actions/download-artifact@v3 |
| 95 | + with: |
| 96 | + name: cargo-updates |
| 97 | + |
| 98 | + - name: craft PR body and commit message |
| 99 | + run: | |
| 100 | + echo "${COMMIT_MESSAGE}" > commit.txt |
| 101 | + cat cargo_update.log >> commit.txt |
| 102 | +
|
| 103 | + echo "${PR_MESSAGE}" > body.md |
| 104 | + echo '```txt' >> body.md |
| 105 | + cat cargo_update.log >> body.md |
| 106 | + echo '```' >> body.md |
| 107 | +
|
| 108 | + - name: commit |
| 109 | + run: | |
| 110 | + git config user.name github-actions |
| 111 | + git config user.email [email protected] |
| 112 | + git switch --force-create cargo_update |
| 113 | + git add ./Cargo.lock |
| 114 | + git commit --no-verify --file=commit.txt |
| 115 | +
|
| 116 | + - name: push |
| 117 | + run: git push --no-verify --force --set-upstream origin cargo_update |
| 118 | + |
| 119 | + - name: edit existing open pull request |
| 120 | + id: edit |
| 121 | + # Don't fail job if we need to open new PR |
| 122 | + continue-on-error: true |
| 123 | + env: |
| 124 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 125 | + run: | |
| 126 | + # Exit with error if PR is closed |
| 127 | + STATE=$(gh pr view cargo_update --repo $GITHUB_REPOSITORY --json state --jq '.state') |
| 128 | + if [[ "$STATE" != "OPEN" ]]; then |
| 129 | + exit 1 |
| 130 | + fi |
| 131 | +
|
| 132 | + gh pr edit cargo_update --title "${PR_TITLE}" --body-file body.md --repo $GITHUB_REPOSITORY |
| 133 | +
|
| 134 | + - name: open new pull request |
| 135 | + # Only run if there wasn't an existing PR |
| 136 | + if: steps.edit.outcome != 'success' |
| 137 | + env: |
| 138 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 139 | + run: gh pr create --title "${PR_TITLE}" --body-file body.md --repo $GITHUB_REPOSITORY |
0 commit comments