Skip to content

Commit 791bc00

Browse files
authored
Update build-pr-cmk.yml
1 parent f2b9048 commit 791bc00

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

.github/workflows/build-pr-cmk.yml

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ jobs:
3232
runs-on: ubuntu-24.04
3333
env:
3434
GITHUB_TOKEN: ""
35+
outputs:
36+
outcome: ${{ steps.meta.outputs.outcome }}
37+
artifact_url: ${{ steps.meta.outputs.artifact_url }}
3538
steps:
3639
- name: Checkout PR HEAD
3740
uses: actions/checkout@v4
@@ -51,7 +54,7 @@ jobs:
5154

5255
- name: Upload zipped dist artifact
5356
id: upload_artifact
54-
if: success()
57+
if: ${{ steps.build.outcome == 'success' }} # gate on build outcome
5558
uses: actions/upload-artifact@v4
5659
with:
5760
name: cmk-binaries.pr${{ github.event.pull_request.number }}
@@ -64,13 +67,14 @@ jobs:
6467
if: always()
6568
run: |
6669
echo "outcome=${{ steps.build.outcome }}" >> $GITHUB_OUTPUT
67-
# upload-artifact v4 exposes artifact-url output; echo empty if missing
6870
echo "artifact_url=${{ steps.upload_artifact.outputs.artifact-url }}" >> $GITHUB_OUTPUT
6971
7072
comment:
73+
if: always() # run even if build job failed
7174
needs: build
7275
permissions:
7376
contents: read
77+
issues: write # <-- required for issues.createComment
7478
pull-requests: write
7579
runs-on: ubuntu-24.04
7680
steps:
@@ -79,52 +83,38 @@ jobs:
7983
with:
8084
script: |
8185
const { execSync } = require('child_process');
86+
8287
const issue_number = context.payload.pull_request.number;
8388
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 }}";
9089
91-
// Fallback: use provided build outcome var if needs.* resolution is odd
92-
const finalOutcome = buildOutcome || outcome || 'failure';
90+
const owner = context.payload.repository.owner.login; // base repo (pull_request_target)
91+
const repo = context.payload.repository.name;
92+
93+
const buildOutcome = "${{ needs.build.outputs.outcome }}";
94+
const artifactUrl = "${{ needs.build.outputs.artifact_url }}";
95+
const runId = "${{ github.run_id }}";
9396
94-
let commentBody = `<!-- ${identifier} -->\n`;
97+
core.info(`Will comment on ${owner}/${repo}#${issue_number}`);
98+
core.info(`Outcome=${buildOutcome || '(empty)'} Artifact=${artifactUrl || '(none)'}`);
9599
96-
if (finalOutcome === 'success' && artifactUrl) {
100+
let body = `<!-- ${identifier} -->\n`;
101+
if (buildOutcome === 'success' && artifactUrl) {
97102
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})`;
103+
body += `✅ Build complete for PR #${issue_number}.\n\n`;
104+
body += `🔗 Download the [cmk binaries](${artifactUrl}) (expires on ${expiryDate})`;
100105
} 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.`;
106+
body += `❌ Build failed for PR #${issue_number}.\n\n`;
107+
body += `See the run: https://github.com/${owner}/${repo}/actions/runs/${runId}`;
103108
}
104109
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-
);
110+
const { data: comments } = await github.rest.issues.listComments({ owner, repo, issue_number });
111+
const existing = comments.find(c => c.user.login === 'github-actions[bot]' && c.body.includes(identifier));
115112
116113
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-
});
114+
core.info(`Updating comment id ${existing.id}`);
115+
await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body });
123116
} else {
124-
await github.rest.issues.createComment({
125-
owner: context.repo.owner,
126-
repo: context.repo.repo,
127-
issue_number,
128-
body: commentBody
129-
});
117+
core.info(`Creating new comment`);
118+
await github.rest.issues.createComment({ owner, repo, issue_number, body });
130119
}
120+

0 commit comments

Comments
 (0)