Skip to content

Instantly share code, notes, and snippets.

@arilotter
Last active January 17, 2025 00:03
Show Gist options
  • Save arilotter/b91dc90362083934ac71eec9215583b9 to your computer and use it in GitHub Desktop.
Save arilotter/b91dc90362083934ac71eec9215583b9 to your computer and use it in GitHub Desktop.
a github workflow to add a link to garnix deployments to PRs
name: Update Deployment Comment
on:
check_suite:
types: [completed]
jobs:
update-comment:
runs-on: ubuntu-latest
# Only run if there are associated pull requests
if: github.event.check_suite.pull_requests[0].number != null
steps:
- name: Get PR number
id: get-pr
run: |
PR_NUMBER=$(echo ${{ github.event.check_suite.pull_requests[0].number }})
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
- name: Get deployment status and URL
id: get-deployment
uses: actions/github-script@v6
with:
script: |
const checks = await github.rest.checks.listForRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: context.sha
});
const deploymentCheck = checks.data.check_runs.find(check =>
check.name.startsWith('deployment ')
);
if (!deploymentCheck) {
core.setFailed('Deployment check not found');
return;
}
const outputLines = deploymentCheck.output.text.split('\n');
let deploymentUrl;
// the URL isn't in the very first line, but it should be near the top, so this should be cheap.
for (const line of outputLines) {
const match = line.match(/Server has been successfully deployed to: (.+)/);
if (match) {
deploymentUrl = match[1];
break;
}
}
if (!deploymentUrl) {
core.setFailed('Deployment URL not found in output');
return;
}
core.setOutput('status', deploymentCheck.conclusion);
core.setOutput('url', deploymentUrl);
- name: Find Comment
uses: peter-evans/find-comment@v2
id: find-comment
continue-on-error: true
with:
issue-number: ${{ steps.get-pr.outputs.pr_number }}
comment-author: "github-actions[bot]"
body-includes: "Deployment Status"
- name: Create or Update Comment
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.find-comment.outcome == 'success' && steps.find-comment.outputs.comment-id || '' }}
issue-number: ${{ steps.get-pr.outputs.pr_number }}
body: |
## Deployment Status: ${{ steps.get-deployment.outputs.status }}
🔗 [View Deployment](https://${{ steps.get-deployment.outputs.url }})
Last updated: ${{ github.event.check_suite.updated_at }}
Deployed commit hash: ${{ github.event.check_suite.head_sha }}
edit-mode: replace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment