|
| 1 | +name: PR Command (/build, /merge) |
| 2 | +on: |
| 3 | + issue_comment: |
| 4 | + types: [created] |
| 5 | + |
| 6 | +jobs: |
| 7 | + # Gate: only run on PR comments that start with /build or /merge |
| 8 | + check-command: |
| 9 | + if: > |
| 10 | + github.event.issue.pull_request && |
| 11 | + (startsWith(github.event.comment.body, '/build') || startsWith(github.event.comment.body, '/merge')) |
| 12 | + runs-on: ubuntu-latest |
| 13 | + outputs: |
| 14 | + command: ${{ steps.parse.outputs.command }} |
| 15 | + steps: |
| 16 | + - name: Parse command |
| 17 | + id: parse |
| 18 | + run: | |
| 19 | + COMMENT="${{ github.event.comment.body }}" |
| 20 | + if [[ "$COMMENT" == /build* ]]; then |
| 21 | + echo "command=build" >> "$GITHUB_OUTPUT" |
| 22 | + elif [[ "$COMMENT" == /merge* ]]; then |
| 23 | + echo "command=merge" >> "$GITHUB_OUTPUT" |
| 24 | + fi |
| 25 | +
|
| 26 | + # ── /build ────────────────────────────────────────────────────────── |
| 27 | + build: |
| 28 | + needs: check-command |
| 29 | + if: needs.check-command.outputs.command == 'build' |
| 30 | + runs-on: ubuntu-latest |
| 31 | + permissions: |
| 32 | + pull-requests: write |
| 33 | + steps: |
| 34 | + - name: Get PR ref |
| 35 | + id: pr |
| 36 | + uses: actions/github-script@v7 |
| 37 | + with: |
| 38 | + script: | |
| 39 | + const pr = await github.rest.pulls.get({ |
| 40 | + owner: context.repo.owner, |
| 41 | + repo: context.repo.repo, |
| 42 | + pull_number: context.issue.number, |
| 43 | + }); |
| 44 | + core.setOutput('ref', pr.data.head.ref); |
| 45 | + core.setOutput('sha', pr.data.head.sha); |
| 46 | +
|
| 47 | + - uses: actions/checkout@v4 |
| 48 | + with: |
| 49 | + ref: ${{ steps.pr.outputs.ref }} |
| 50 | + |
| 51 | + - name: Free up disk space |
| 52 | + uses: ./.github/actions/free-up-disk-space |
| 53 | + |
| 54 | + - name: Build NVCR Image |
| 55 | + run: | |
| 56 | + docker build \ |
| 57 | + -t fms-hf-tuning:pr-${{ github.event.issue.number }}-nvcr \ |
| 58 | + -f build/nvcr.Dockerfile . |
| 59 | +
|
| 60 | + - name: Comment build result |
| 61 | + if: always() |
| 62 | + uses: actions/github-script@v7 |
| 63 | + with: |
| 64 | + script: | |
| 65 | + const status = '${{ job.status }}' === 'success' |
| 66 | + ? 'Build succeeded' |
| 67 | + : 'Build failed'; |
| 68 | + const sha = '${{ steps.pr.outputs.sha }}'.substring(0, 7); |
| 69 | + await github.rest.issues.createComment({ |
| 70 | + owner: context.repo.owner, |
| 71 | + repo: context.repo.repo, |
| 72 | + issue_number: context.issue.number, |
| 73 | + body: `**${status}** for \`${sha}\` (NVCR image)\n\n[View run](${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId})`, |
| 74 | + }); |
| 75 | +
|
| 76 | + # ── /merge ────────────────────────────────────────────────────────── |
| 77 | + merge: |
| 78 | + needs: check-command |
| 79 | + if: needs.check-command.outputs.command == 'merge' |
| 80 | + runs-on: ubuntu-latest |
| 81 | + permissions: |
| 82 | + contents: write |
| 83 | + pull-requests: write |
| 84 | + steps: |
| 85 | + - name: Merge PR |
| 86 | + uses: actions/github-script@v7 |
| 87 | + with: |
| 88 | + script: | |
| 89 | + const pr = await github.rest.pulls.get({ |
| 90 | + owner: context.repo.owner, |
| 91 | + repo: context.repo.repo, |
| 92 | + pull_number: context.issue.number, |
| 93 | + }); |
| 94 | +
|
| 95 | + if (pr.data.mergeable_state === 'blocked' || !pr.data.mergeable) { |
| 96 | + await github.rest.issues.createComment({ |
| 97 | + owner: context.repo.owner, |
| 98 | + repo: context.repo.repo, |
| 99 | + issue_number: context.issue.number, |
| 100 | + body: '**Merge failed**: PR is not in a mergeable state (`' + pr.data.mergeable_state + '`). Check required status checks and reviews.', |
| 101 | + }); |
| 102 | + core.setFailed('PR is not mergeable'); |
| 103 | + return; |
| 104 | + } |
| 105 | +
|
| 106 | + await github.rest.pulls.merge({ |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + pull_number: context.issue.number, |
| 110 | + merge_method: 'merge', |
| 111 | + }); |
| 112 | +
|
| 113 | + await github.rest.issues.createComment({ |
| 114 | + owner: context.repo.owner, |
| 115 | + repo: context.repo.repo, |
| 116 | + issue_number: context.issue.number, |
| 117 | + body: '**PR merged** successfully.', |
| 118 | + }); |
0 commit comments