|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +name: Build cmk binaries on PR |
| 19 | + |
| 20 | +on: |
| 21 | + pull_request_target: |
| 22 | + types: [opened, synchronize, reopened] |
| 23 | + |
| 24 | +concurrency: |
| 25 | + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} |
| 26 | + cancel-in-progress: true |
| 27 | + |
| 28 | +jobs: |
| 29 | + build: |
| 30 | + permissions: |
| 31 | + contents: read |
| 32 | + runs-on: ubuntu-24.04 |
| 33 | + env: |
| 34 | + GITHUB_TOKEN: "" |
| 35 | + steps: |
| 36 | + - name: Checkout PR HEAD |
| 37 | + uses: actions/checkout@v4 |
| 38 | + with: |
| 39 | + ref: ${{ github.event.pull_request.head.sha }} |
| 40 | + persist-credentials: false |
| 41 | + |
| 42 | + - name: Set up Go |
| 43 | + uses: actions/setup-go@v5 |
| 44 | + with: |
| 45 | + go-version: '1.22' |
| 46 | + |
| 47 | + - name: Build dist |
| 48 | + id: build |
| 49 | + run: make dist |
| 50 | + continue-on-error: true |
| 51 | + |
| 52 | + - name: Upload zipped dist artifact |
| 53 | + id: upload_artifact |
| 54 | + if: success() |
| 55 | + uses: actions/upload-artifact@v4 |
| 56 | + with: |
| 57 | + name: cmk-binaries.pr${{ github.event.pull_request.number }} |
| 58 | + path: dist/ |
| 59 | + if-no-files-found: error |
| 60 | + retention-days: 10 |
| 61 | + |
| 62 | + - name: Expose build outcome & artifact link |
| 63 | + id: meta |
| 64 | + if: always() |
| 65 | + run: | |
| 66 | + echo "outcome=${{ steps.build.outcome }}" >> $GITHUB_OUTPUT |
| 67 | + # upload-artifact v4 exposes artifact-url output; echo empty if missing |
| 68 | + echo "artifact_url=${{ steps.upload_artifact.outputs.artifact-url }}" >> $GITHUB_OUTPUT |
| 69 | +
|
| 70 | + comment: |
| 71 | + needs: build |
| 72 | + permissions: |
| 73 | + contents: read |
| 74 | + pull-requests: write |
| 75 | + runs-on: ubuntu-24.04 |
| 76 | + steps: |
| 77 | + - name: Comment or update cmk build artifact on PR |
| 78 | + uses: actions/github-script@v7 |
| 79 | + with: |
| 80 | + script: | |
| 81 | + const { execSync } = require('child_process'); |
| 82 | + const issue_number = context.payload.pull_request.number; |
| 83 | + const identifier = "cmk-build-artifact-comment"; |
| 84 | + const outcome = "${{ needs.build.outputs.outcome || steps.build.outcome }}".trim() || "${{ needs.build.outputs.outcome }}"; |
| 85 | + // Prefer job output from previous step: |
| 86 | + const buildOutcome = "${{ needs.build.outputs.outcome }}"; |
| 87 | + const artifactUrl = "${{ needs.build.outputs.artifact_url }}"; |
| 88 | + const runId = "${{ github.run_id }}"; |
| 89 | + const repo = "${{ github.repository }}"; |
| 90 | +
|
| 91 | + // Fallback: use provided build outcome var if needs.* resolution is odd |
| 92 | + const finalOutcome = buildOutcome || outcome || 'failure'; |
| 93 | +
|
| 94 | + let commentBody = `<!-- ${identifier} -->\n`; |
| 95 | +
|
| 96 | + if (finalOutcome === 'success' && artifactUrl) { |
| 97 | + const expiryDate = execSync("date -d '+10 days' '+%B %d, %Y'").toString().trim(); |
| 98 | + commentBody += `✅ Build complete for PR #${issue_number}.\n\n`; |
| 99 | + commentBody += `🔗 Download the [cmk binaries](${artifactUrl}) (expires on ${expiryDate})`; |
| 100 | + } else { |
| 101 | + commentBody += `❌ Build failed for PR #${issue_number}.\n\n`; |
| 102 | + commentBody += `See the [workflow run](https://github.com/${repo}/actions/runs/${runId}) for details.`; |
| 103 | + } |
| 104 | +
|
| 105 | + const { data: comments } = await github.rest.issues.listComments({ |
| 106 | + owner: context.repo.owner, |
| 107 | + repo: context.repo.repo, |
| 108 | + issue_number |
| 109 | + }); |
| 110 | +
|
| 111 | + const existing = comments.find(c => |
| 112 | + c.user.login === 'github-actions[bot]' && |
| 113 | + c.body.includes(identifier) |
| 114 | + ); |
| 115 | +
|
| 116 | + if (existing) { |
| 117 | + await github.rest.issues.updateComment({ |
| 118 | + owner: context.repo.owner, |
| 119 | + repo: context.repo.repo, |
| 120 | + comment_id: existing.id, |
| 121 | + body: commentBody |
| 122 | + }); |
| 123 | + } else { |
| 124 | + await github.rest.issues.createComment({ |
| 125 | + owner: context.repo.owner, |
| 126 | + repo: context.repo.repo, |
| 127 | + issue_number, |
| 128 | + body: commentBody |
| 129 | + }); |
| 130 | + } |
0 commit comments