Skip to content

github-action: add pr builder for easier testing #15

github-action: add pr builder for easier testing

github-action: add pr builder for easier testing #15

Workflow file for this run

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
name: Build cmk on PR
on:
pull_request:
types: [opened, synchronize, reopened]
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number }}
cancel-in-progress: true
permissions:
contents: read
pull-requests: write
issues: write
jobs:
build:
runs-on: ubuntu-24.04
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '1.22'
- name: Build dist
run: make dist
continue-on-error: true
- name: Upload zipped dist artifact
id: upload_artifact
uses: actions/upload-artifact@v4
if: success()
with:
name: cmk-binaries.pr${{ github.event.pull_request.number }}
path: dist/
if-no-files-found: error
retention-days: 10
- name: Calculate artifact expiry date
id: expiry_date
if: success()
run: |
echo "expiry_date=$(date -d '+10 days' '+%B %d, %Y')" >> $GITHUB_OUTPUT
- name: Comment or update cmk build artifact on PR
uses: actions/github-script@v7
with:
script: |
const issue_number = context.payload.pull_request.number;
const identifier = "cmk-build-artifact-comment";
const jobStatus = "${{ job.status }}";
const artifactUrl = "${{ steps.upload_artifact.outputs.artifact-url }}";
const runId = "${{ github.run_id }}";
const repo = "${{ github.repository }}";
let commentBody = `<!-- ${identifier} -->\n`;
if (jobStatus === 'success') {
const expiryDate = execSync("date -d '+10 days' '+%B %d, %Y'").toString().trim();
commentBody += `✅ Build complete for PR #${issue_number}.\n\n`;
commentBody += `🔗 Download the [cmk binaries](${artifactUrl}) (expires on ${expiryDate})`;
} else {
commentBody += `❌ Build failed for PR #${issue_number}.\n\n`;
commentBody += `See the [workflow run](https://github.com/${repo}/actions/runs/${runId}) for details.`;
}
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number
});
const existing = comments.find(c =>
c.user.login === 'github-actions[bot]' &&
c.body.includes(identifier)
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: commentBody
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number,
body: commentBody
});
}