Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Automatically post/update a comment on PRs with the deployment URL
- New inputs: `comment-on-pr`, `github-token`, `comment-header`
- Upsert behavior: updates existing comment instead of creating duplicates
- **cleanup** action: PR comment delete feature
- Delete the deploy PR comment when PR is closed (default: enabled)
- New inputs: `delete-pr-comment`, `comment-header`
- New output: `pr-comment-deleted`

- CI/CD pipeline with ShellCheck, actionlint, and yamllint
- Branch protection and governance files (CODEOWNERS, issue templates, PR template)
- CONTRIBUTING.md with development guidelines
Expand All @@ -63,6 +68,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- No longer necessary to explicitly pass `github-token: ${{ secrets.GITHUB_TOKEN }}`
- Only needed when using a custom PAT for cross-repository operations

### Removed
- **BREAKING** `cleanup` action: `update-pr-comment` input (use `delete-pr-comment` instead)
- **BREAKING** `cleanup` action: `pr-comment-updated` output (use `pr-comment-deleted` instead)

### Internal
- Added justfile for common development tasks
- Added pre-commit.ci configuration (weekly autoupdates, skip duplicates with CI)
Expand Down
58 changes: 26 additions & 32 deletions cleanup/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ Removes a ZAD deployment and optionally cleans up associated GitHub resources (e
| `github-token` | No | `github.token` | GitHub token for deployments/containers/PR (defaults to automatic token) |
| `github-admin-token` | No | `''` | GitHub token for environment deletion (needs repo admin permission) |
| `api-base-url` | No | `https://operations-manager.rig.prd1.gn2.quattro.rijksapps.nl/api` | ZAD Operations Manager API base URL |
| `update-pr-comment` | No | `false` | Update the deploy PR comment to show cleanup status |
| `comment-header` | No | `## 🚀 Preview Deployment` | Header of the deploy comment to find and update |
| `delete-pr-comment` | No | `true` | Delete the deploy PR comment |
| `comment-header` | No | `## 🚀 Preview Deployment` | Header of the deploy comment to find and delete |

## Outputs

| Name | Description |
|------|-------------|
| `zad-deleted` | Whether the ZAD deployment was deleted (`true`/`false`) |
| `github-env-deleted` | Whether the GitHub environment was deleted (`true`/`false`) |
| `github-deployments-deleted` | Whether GitHub deployments were deleted (`true`/`false`) |
| `container-deleted` | Whether the container image was deleted (`true`/`false`) |
| `pr-comment-updated` | Whether the PR comment was updated (`true`/`false`) |
| Name | Description |
|------------------------------|----------------------------------------------------------|
| `zad-deleted` | Whether the ZAD deployment was deleted (`true`/`false`) |
| `github-env-deleted` | Whether the GitHub environment was deleted |
| `github-deployments-deleted` | Whether GitHub deployments were deleted |
| `container-deleted` | Whether the container image was deleted |
| `pr-comment-deleted` | Whether the PR comment was deleted |

## Example Usage

Expand Down Expand Up @@ -71,7 +71,7 @@ cleanup-preview:
permissions:
deployments: write
packages: write
pull-requests: write # For update-pr-comment
pull-requests: write # For delete-pr-comment
steps:
- name: Cleanup PR preview
uses: RijksICTGilde/zad-actions/cleanup@v1
Expand All @@ -86,30 +86,24 @@ cleanup-preview:
container-name: ${{ github.event.repository.name }}
container-tag: pr-${{ github.event.number }}
github-admin-token: ${{ secrets.GITHUB_ADMIN_TOKEN }}
update-pr-comment: true
delete-pr-comment: true
```

### PR Comment Update
### Delete PR Comment

When used with the deploy action's `comment-on-pr` feature, the cleanup action can update the PR comment to show that the deployment was cleaned up:
When used with the deploy action's `comment-on-pr` feature, the cleanup action can remove the PR comment when the deployment is cleaned up:

**Before cleanup:**
> ## 🚀 Preview Deployment
>
> Your changes have been deployed to a preview environment:
>
> **URL:** https://web-pr123-my-project.rig...
>
> This deployment will be automatically cleaned up when the PR is closed.

**After cleanup:**
> ## 🧹 Preview Deployment (Cleaned Up)
>
> ~~https://editor-pr123-my-project.rig...~~
>
> This deployment was automatically cleaned up when the PR was closed.
```yaml
- uses: RijksICTGilde/zad-actions/cleanup@v1
with:
api-key: ${{ secrets.ZAD_API_KEY }}
project-id: my-project
deployment-name: pr${{ github.event.pull_request.number }}
delete-pr-comment: true
github-token: ${{ secrets.GITHUB_TOKEN }}
```

To enable this, add `update-pr-comment: true` to your cleanup step. The action will find the existing deploy comment by its header and update it.
The action finds the deploy comment by its header (`## 🚀 Preview Deployment` by default) and deletes it.

## Permissions

Expand All @@ -119,7 +113,7 @@ To enable this, add `update-pr-comment: true` to your cleanup step. The action w
permissions:
deployments: write # For delete-github-deployments
packages: write # For delete-container
pull-requests: write # For update-pr-comment
pull-requests: write # For delete-pr-comment
```

### Token Requirements
Expand All @@ -130,7 +124,7 @@ permissions:
| Delete GitHub deployments | `github-token` | `deployments: write` |
| Delete GitHub environment | `github-admin-token` | Repository admin access |
| Delete container image | `github-token` | `packages: delete` |
| Update PR comment | `github-token` | `pull-requests: write` |
| Delete PR comment | `github-token` | `pull-requests: write` |

**Note:** The default `GITHUB_TOKEN` cannot delete GitHub environments. You need a Personal Access Token (PAT) or GitHub App token with admin permissions for the repository.

Expand Down Expand Up @@ -212,7 +206,7 @@ Check cleanup results and take action:
2. **Delete GitHub Deployments** (optional): Marks all deployments for the environment as inactive, then deletes them
3. **Delete GitHub Environment** (optional): Deletes the GitHub environment
4. **Delete Container Image** (optional): Finds and deletes the container version with the specified tag
5. **Update PR Comment** (optional): Updates the deploy comment to show cleanup status
5. **Delete PR Comment** (optional): Removes the deploy comment from the PR

Each step runs independently and won't fail the action if it fails (cleanup is best-effort). Check the outputs to see what was actually deleted.

Expand Down
55 changes: 20 additions & 35 deletions cleanup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ inputs:
description: 'ZAD Operations Manager API base URL'
required: false
default: 'https://operations-manager.rig.prd1.gn2.quattro.rijksapps.nl/api'
update-pr-comment:
description: 'Update the deploy PR comment to show cleanup status (requires github-token with pull-requests:write)'
delete-pr-comment:
description: 'Delete the deploy PR comment (requires github-token with pull-requests:write)'
required: false
default: 'false'
default: 'true'
comment-header:
description: 'Header of the deploy comment to find and update (default: "## 🚀 Preview Deployment")'
description: 'Header of the deploy comment to find and delete (default: "## 🚀 Preview Deployment")'
required: false
default: '## 🚀 Preview Deployment'

Expand All @@ -75,9 +75,9 @@ outputs:
container-deleted:
description: 'Whether the container image was deleted'
value: ${{ steps.delete-container.outputs.deleted }}
pr-comment-updated:
description: 'Whether the PR comment was updated'
value: ${{ steps.update-pr-comment.outputs.updated }}
pr-comment-deleted:
description: 'Whether the PR comment was deleted'
value: ${{ steps.delete-pr-comment.outputs.deleted }}

runs:
using: 'composite'
Expand Down Expand Up @@ -273,47 +273,32 @@ runs:

echo "deleted=$DELETED" >> "$GITHUB_OUTPUT"

- name: Update PR Comment
id: update-pr-comment
if: inputs.update-pr-comment == 'true' && github.event_name == 'pull_request'
- name: Delete PR Comment
id: delete-pr-comment
if: inputs.delete-pr-comment == 'true' && github.event_name == 'pull_request'
shell: bash
env:
GH_TOKEN: ${{ inputs.github-token }}
COMMENT_HEADER: ${{ inputs.comment-header }}
PR_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_REPOSITORY: ${{ github.repository }}
DEPLOYMENT_NAME: ${{ inputs.deployment-name }}
PROJECT_ID: ${{ inputs.project-id }}
run: |
# Build the updated comment body
COMMENT_BODY="## 🧹 Preview Deployment (Cleaned Up)

~~https://editor-${DEPLOYMENT_NAME}-${PROJECT_ID}.rig.prd1.gn2.quattro.rijksapps.nl~~

This deployment was automatically cleaned up when the PR was closed."

# Find existing comment by header
EXISTING_COMMENT=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \
--jq ".[] | select(.body | startswith(\"${COMMENT_HEADER}\")) | {id: .id, body: .body}" \
COMMENT_ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" \
--jq ".[] | select(.body | startswith(\"${COMMENT_HEADER}\")) | .id" \
2>/dev/null | head -n1 || echo "")

if [ -z "$EXISTING_COMMENT" ]; then
if [ -z "$COMMENT_ID" ]; then
echo "No existing deploy comment found with header: ${COMMENT_HEADER}"
echo "updated=false" >> "$GITHUB_OUTPUT"
echo "deleted=false" >> "$GITHUB_OUTPUT"
exit 0
fi

COMMENT_ID=$(echo "$EXISTING_COMMENT" | jq -r '.id')

if [ -n "$COMMENT_ID" ] && [ "$COMMENT_ID" != "null" ]; then
echo "Updating PR comment (ID: $COMMENT_ID)"
gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" \
-X PATCH \
-f body="$COMMENT_BODY" \
--silent
echo "PR comment updated to show cleanup status"
echo "updated=true" >> "$GITHUB_OUTPUT"
echo "Deleting PR comment (ID: $COMMENT_ID)"
if gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" -X DELETE 2>/dev/null; then
echo "PR comment deleted successfully"
echo "deleted=true" >> "$GITHUB_OUTPUT"
else
echo "Could not find comment ID"
echo "updated=false" >> "$GITHUB_OUTPUT"
echo "::warning::Failed to delete PR comment"
echo "deleted=false" >> "$GITHUB_OUTPUT"
fi