Skip to content

Commit 1bae5ff

Browse files
authored
feat(deploy): add PR commenting feature (#3)
Add ability to automatically post/update a comment on PRs with the deployment URL. This eliminates the need for a separate github-script step in workflows. New inputs: - comment-on-pr: Enable PR commenting (default: false) - github-token: Token with pull-requests:write permission - comment-header: Custom header for the comment Features: - Upsert behavior: updates existing comment instead of creating duplicates - Only runs on pull_request events - Customizable comment header
1 parent 6072d88 commit 1bae5ff

3 files changed

Lines changed: 108 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [Unreleased]
99

1010
### Added
11+
- **deploy** action: PR commenting feature
12+
- Automatically post/update a comment on PRs with the deployment URL
13+
- New inputs: `comment-on-pr`, `github-token`, `comment-header`
14+
- Upsert behavior: updates existing comment instead of creating duplicates
1115
- CI/CD pipeline with ShellCheck, actionlint, and yamllint
1216
- Branch protection and governance files (CODEOWNERS, issue templates, PR template)
1317
- CONTRIBUTING.md with development guidelines

deploy/README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ Deploys a container image to ZAD Operations Manager.
1414
| `clone-from` | No | `''` | Clone configuration from existing deployment |
1515
| `force-clone` | No | `false` | Force clone even if deployment already exists |
1616
| `api-base-url` | No | `https://operations-manager.rig.prd1.gn2.quattro.rijksapps.nl/api` | ZAD Operations Manager API base URL |
17+
| `comment-on-pr` | No | `false` | Post/update a comment on the PR with the deployment URL |
18+
| `github-token` | No | `''` | GitHub token for PR commenting (needs `pull-requests: write`) |
19+
| `comment-header` | No | `## 🚀 Preview Deployment` | Custom header for the PR comment |
1720

1821
## Outputs
1922

@@ -55,6 +58,42 @@ Deploys a container image to ZAD Operations Manager.
5558
clone-from: production
5659
```
5760
61+
### PR Preview with Automatic Comment
62+
63+
Automatically post a comment on the PR with the deployment URL:
64+
65+
```yaml
66+
deploy-preview:
67+
runs-on: ubuntu-latest
68+
if: github.event_name == 'pull_request'
69+
permissions:
70+
pull-requests: write
71+
steps:
72+
- name: Deploy PR Preview
73+
uses: RijksICTGilde/zad-actions/deploy@v1
74+
with:
75+
api-key: ${{ secrets.ZAD_API_KEY }}
76+
project-id: my-project
77+
deployment-name: pr${{ github.event.pull_request.number }}
78+
component: web
79+
image: ghcr.io/org/app:pr-${{ github.event.number }}
80+
clone-from: production
81+
comment-on-pr: true
82+
github-token: ${{ secrets.GITHUB_TOKEN }}
83+
```
84+
85+
The action will create a comment like this on the PR:
86+
87+
> ## 🚀 Preview Deployment
88+
>
89+
> Your changes have been deployed to a preview environment:
90+
>
91+
> **URL:** https://web-pr123-my-project.rig.prd1.gn2.quattro.rijksapps.nl
92+
>
93+
> This deployment will be automatically cleaned up when the PR is closed.
94+
95+
On subsequent deployments to the same PR, the existing comment is updated instead of creating a new one.
96+
5897
### Use with GitHub Environment
5998
6099
```yaml
@@ -77,7 +116,17 @@ deploy:
77116
78117
## Permissions
79118
80-
This action requires no special GitHub permissions. Only the ZAD API key is needed.
119+
| Feature | Required Permission |
120+
|---------|---------------------|
121+
| Basic deployment | None (only ZAD API key) |
122+
| PR commenting | `pull-requests: write` |
123+
124+
For PR commenting, pass `github-token: ${{ secrets.GITHUB_TOKEN }}` and ensure your job has the required permission:
125+
126+
```yaml
127+
permissions:
128+
pull-requests: write
129+
```
81130

82131
## URL Pattern
83132

deploy/action.yml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ inputs:
3535
description: 'ZAD Operations Manager API base URL'
3636
required: false
3737
default: 'https://operations-manager.rig.prd1.gn2.quattro.rijksapps.nl/api'
38+
comment-on-pr:
39+
description: 'Post/update a comment on the PR with the deployment URL (requires github-token)'
40+
required: false
41+
default: 'false'
42+
github-token:
43+
description: 'GitHub token for PR commenting (needs pull-requests: write permission)'
44+
required: false
45+
default: ''
46+
comment-header:
47+
description: 'Custom header for the PR comment (default: "## 🚀 Preview Deployment")'
48+
required: false
49+
default: '## 🚀 Preview Deployment'
3850

3951
outputs:
4052
url:
@@ -168,3 +180,45 @@ runs:
168180
echo "$BODY"
169181
exit 1
170182
fi
183+
184+
- name: Comment on PR
185+
if: inputs.comment-on-pr == 'true' && inputs.github-token != '' && github.event_name == 'pull_request'
186+
shell: bash
187+
env:
188+
GH_TOKEN: ${{ inputs.github-token }}
189+
DEPLOYMENT_URL: ${{ steps.deploy.outputs.url }}
190+
COMMENT_HEADER: ${{ inputs.comment-header }}
191+
PR_NUMBER: ${{ github.event.pull_request.number }}
192+
GITHUB_REPOSITORY: ${{ github.repository }}
193+
run: |
194+
# Build the comment body
195+
COMMENT_BODY="${COMMENT_HEADER}
196+
197+
Your changes have been deployed to a preview environment:
198+
199+
**URL:** ${DEPLOYMENT_URL}
200+
201+
This deployment will be automatically cleaned up when the PR is closed."
202+
203+
# Check for existing comment from this action (identified by header)
204+
EXISTING_COMMENT_ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \
205+
--jq ".[] | select(.body | startswith(\"${COMMENT_HEADER}\")) | .id" \
206+
2>/dev/null | head -n1 || echo "")
207+
208+
if [ -n "$EXISTING_COMMENT_ID" ]; then
209+
# Update existing comment
210+
echo "Updating existing PR comment (ID: $EXISTING_COMMENT_ID)"
211+
gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${EXISTING_COMMENT_ID}" \
212+
-X PATCH \
213+
-f body="$COMMENT_BODY" \
214+
--silent
215+
echo "PR comment updated"
216+
else
217+
# Create new comment
218+
echo "Creating new PR comment"
219+
gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \
220+
-X POST \
221+
-f body="$COMMENT_BODY" \
222+
--silent
223+
echo "PR comment created"
224+
fi

0 commit comments

Comments
 (0)