Skip to content

Commit

Permalink
Merge pull request #430 from danskernesdigitalebibliotek/cms-env-cleanup
Browse files Browse the repository at this point in the history
Cleanup development environments in our core Lagoon project
  • Loading branch information
kasperg authored Sep 2, 2024
2 parents 4316128 + e596d18 commit adf1032
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
55 changes: 55 additions & 0 deletions infrastructure/task/scripts/cleanup-dev-envs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/usr/bin/env bash
#
# Cleanup development environments in our core Lagoon project.
#
# With our setup Lagoon creates a development environment automatically each
# time a pull request is created. Such an environments should also be deleted
# when the pull request is closed. This is usually the case when the PR is
# merged but often failed when the pull request is closed without merging.
#
# To work around this issue this script tries to close all development
# environments in Lagoon where the corresponding pull request has been either
# closed or merged.

LAGOON_PROJECT="dpl-cms"
REPO="danskernesdigitalebibliotek/dpl-cms"

if ! gh auth status >/dev/null 2>&1; then
echo "Error: You are not logged into GitHub. Please log in using 'gh auth login' and try again."
exit 1
fi

DRY_RUN=false
if [ "$1" == "--dry-run" ]; then
DRY_RUN=true
fi

DEV_ENVS=$(lagoon raw --raw "query dplCmsEnvs {
projectByName(name: \"$LAGOON_PROJECT\") {
environments {
name
environmentType
}
}
}" | jq -c '.projectByName.environments[] | select(.environmentType == "development" and .name != "develop")')

echo "$DEV_ENVS" | while read -r environment; do
ENV_NAME=$(echo "$environment" | jq -r '.name')
# Lagoon development environments are named after the number of their
# corresponding pull request.
PR_NUMBER=$(echo "$environment" | jq -r '.name | gsub("^pr-"; "") | tonumber')

# Check the status of the PR using the gh CLI
PR_STATUS=$(gh pr view "$PR_NUMBER" --repo $REPO --json state -q '.state')

if [[ "$PR_STATUS" == "CLOSED" || "$PR_STATUS" == "MERGED" ]]; then

echo "PR #$PR_NUMBER is $PR_STATUS. Closing development environment: $ENV_NAME."

if [ "$DRY_RUN" = true ]; then
echo "Dry-run: lagoon delete environment -p \"$LAGOON_PROJECT\" -e \"$ENV_NAME\""
else
lagoon delete environment -p "$LAGOON_PROJECT" -e "$ENV_NAME"
fi
fi
done;
1 change: 1 addition & 0 deletions tools/dplsh/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ RUN apk add --no-cache \
docker-cli \
gettext \
git \
github-cli \
rsync \
shadow \
vim \
Expand Down

0 comments on commit adf1032

Please sign in to comment.