Skip to content

Commit cc13b68

Browse files
committed
without community action
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 19a3847 commit cc13b68

File tree

1 file changed

+52
-28
lines changed

1 file changed

+52
-28
lines changed

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

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ on:
2121
pull_request:
2222
types: [opened, synchronize, reopened]
2323

24+
concurrency:
25+
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
26+
cancel-in-progress: true
27+
2428
permissions:
2529
contents: read
2630
pull-requests: write
31+
issues: write
2732

2833
jobs:
2934
build:
@@ -52,34 +57,53 @@ jobs:
5257
if-no-files-found: error
5358
retention-days: 10
5459

55-
- name: Calculate artifact expiry date
56-
id: expiry_date
57-
if: success()
58-
run: |
59-
echo "expiry_date=$(date -d '+10 days' '+%B %d, %Y')" >> $GITHUB_OUTPUT
60-
61-
- name: Find existing PR comment
62-
id: find_comment
63-
if: always()
64-
uses: peter-evans/find-comment@v3
60+
- name: Comment or update cmk build artifact on PR
61+
uses: actions/github-script@v7
6562
with:
66-
issue-number: ${{ github.event.pull_request.number }}
67-
comment-author: 'github-actions[bot]'
68-
body-includes: '<!-- cmk-build-artifact-comment -->'
63+
script: |
64+
const { execSync } = require('child_process');
6965
70-
- name: Create/update comment
71-
if: always()
72-
uses: peter-evans/create-or-update-comment@v4
73-
with:
74-
token: ${{ secrets.GITHUB_TOKEN }}
75-
issue-number: ${{ github.event.pull_request.number }}
76-
comment-id: ${{ steps.find_comment.outputs.comment-id }}
77-
body: |
78-
<!-- cmk-build-artifact-comment -->
79-
${{ job.status == 'success' && '✅ Build complete' || '❌ Build failed' }} for PR #${{ github.event.pull_request.number }}.
80-
81-
${{ job.status == 'success'
82-
&& format('🔗 Download the [cmk binaries]({0}) (expires on {1})', steps.upload_artifact.outputs.artifact-url, steps.expiry_date.outputs.expiry_date)
83-
|| format('See the [workflow run](https://github.com/{0}/actions/runs/{1}) for details.', github.repository, github.run_id) }}
84-
edit-mode: replace
66+
const issue_number = context.payload.pull_request.number;
67+
const identifier = "cmk-build-artifact-comment";
68+
const jobStatus = "${{ job.status }}";
69+
const artifactUrl = "${{ steps.upload_artifact.outputs.artifact-url }}";
70+
const runId = "${{ github.run_id }}";
71+
const repo = "${{ github.repository }}";
72+
73+
let commentBody = `<!-- ${identifier} -->\n`;
74+
75+
if (jobStatus === 'success') {
76+
const expiryDate = execSync("date -d '+10 days' '+%B %d, %Y'").toString().trim();
77+
commentBody += `✅ Build complete for PR #${issue_number}.\n\n`;
78+
commentBody += `🔗 Download the [cmk binaries](${artifactUrl}) (expires on ${expiryDate})`;
79+
} else {
80+
commentBody += `❌ Build failed for PR #${issue_number}.\n\n`;
81+
commentBody += `See the [workflow run](https://github.com/${repo}/actions/runs/${runId}) for details.`;
82+
}
83+
84+
const { data: comments } = await github.rest.issues.listComments({
85+
owner: context.repo.owner,
86+
repo: context.repo.repo,
87+
issue_number
88+
});
89+
90+
const existing = comments.find(c =>
91+
c.user.login === 'github-actions[bot]' &&
92+
c.body.includes(identifier)
93+
);
8594
95+
if (existing) {
96+
await github.rest.issues.updateComment({
97+
owner: context.repo.owner,
98+
repo: context.repo.repo,
99+
comment_id: existing.id,
100+
body: commentBody
101+
});
102+
} else {
103+
await github.rest.issues.createComment({
104+
owner: context.repo.owner,
105+
repo: context.repo.repo,
106+
issue_number,
107+
body: commentBody
108+
});
109+
}

0 commit comments

Comments
 (0)