Change-Id: I06ceef68817d5bb368d176006f13b86be1a8273b
This commit is contained in:
@@ -0,0 +1,84 @@
|
||||
|
||||
name: cleanup-expired
|
||||
|
||||
on:
|
||||
schedule:
|
||||
- cron: "17 * * * *"
|
||||
workflow_dispatch:
|
||||
|
||||
jobs:
|
||||
cleanup-expired:
|
||||
runs-on:
|
||||
- toolzoo-host
|
||||
env:
|
||||
AWS_REGION: us-east-1
|
||||
APP_STACK_PREFIX: temp-stack
|
||||
TTL_GRACE_SECONDS: "600"
|
||||
steps:
|
||||
- name: Cleanup expired app stacks
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
repo_full="${GITHUB_REPOSITORY:-}"
|
||||
current_repo_raw="${repo_full##*/}"
|
||||
catalog_updated="0"
|
||||
now_epoch="$(date -u +%s)"
|
||||
prefix="${APP_STACK_PREFIX}-"
|
||||
stacks="$(aws cloudformation list-stacks \
|
||||
--stack-status-filter CREATE_COMPLETE UPDATE_COMPLETE UPDATE_ROLLBACK_COMPLETE \
|
||||
--query "StackSummaries[?starts_with(StackName, \`${prefix}\`)].StackName" \
|
||||
--output text)"
|
||||
|
||||
for stack in $stacks; do
|
||||
expires_at="$(aws cloudformation describe-stacks --stack-name "$stack" --query "Stacks[0].Tags[?Key=='ExpiresAt'].Value | [0]" --output text)"
|
||||
owner_raw="$(aws cloudformation describe-stacks --stack-name "$stack" --query "Stacks[0].Tags[?Key=='Owner'].Value | [0]" --output text)"
|
||||
repo_raw="$(aws cloudformation describe-stacks --stack-name "$stack" --query "Stacks[0].Tags[?Key=='Repository'].Value | [0]" --output text)"
|
||||
|
||||
if [[ "$expires_at" == "None" || -z "$expires_at" ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
expires_epoch="$(date -u -d "$expires_at" +%s 2>/dev/null || echo 0)"
|
||||
if [[ "$expires_epoch" -eq 0 ]]; then
|
||||
continue
|
||||
fi
|
||||
|
||||
if (( now_epoch >= expires_epoch + TTL_GRACE_SECONDS )); then
|
||||
aws cloudformation delete-stack --stack-name "$stack"
|
||||
aws cloudformation wait stack-delete-complete --stack-name "$stack"
|
||||
|
||||
if [[ "$owner_raw" != "None" && "$repo_raw" != "None" && -n "$owner_raw" && -n "$repo_raw" ]]; then
|
||||
repo_slug="$(printf '%s' "$repo_raw" | tr '[:upper:]' '[:lower:]')"
|
||||
ecr_repo="temp/${repo_slug}"
|
||||
if aws ecr describe-repositories --repository-names "$ecr_repo" >/dev/null 2>&1; then
|
||||
aws ecr delete-repository --repository-name "$ecr_repo" --force
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ "$repo_raw" == "$current_repo_raw" && -f catalog-info.yaml ]]; then
|
||||
awk '
|
||||
BEGIN { in_block=0 }
|
||||
/^ # BEGIN TOOLZOO MANAGED LINK$/ { in_block=1; next }
|
||||
/^ # END TOOLZOO MANAGED LINK$/ { in_block=0; next }
|
||||
in_block==1 { next }
|
||||
{ print }
|
||||
' catalog-info.yaml > /tmp/catalog-info.yaml.cleaned
|
||||
|
||||
if ! cmp -s catalog-info.yaml /tmp/catalog-info.yaml.cleaned; then
|
||||
mv /tmp/catalog-info.yaml.cleaned catalog-info.yaml
|
||||
catalog_updated="1"
|
||||
else
|
||||
rm -f /tmp/catalog-info.yaml.cleaned
|
||||
fi
|
||||
fi
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ "$catalog_updated" == "1" ]]; then
|
||||
git config user.name "toolzoo-bot"
|
||||
git config user.email "toolzoo-bot@local"
|
||||
git add catalog-info.yaml
|
||||
git commit -m "Remove stale service URL after cleanup"
|
||||
git push origin HEAD:main
|
||||
fi
|
||||
|
||||
@@ -0,0 +1,217 @@
|
||||
|
||||
name: deploy
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- main
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
ttl:
|
||||
description: "TTL for deploy operation (1h | 4h | 24h)"
|
||||
required: false
|
||||
default: 1h
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
runs-on:
|
||||
- toolzoo-host
|
||||
env:
|
||||
BACKSTAGE_STACK_NAME: toolzoo-backstage
|
||||
AWS_REGION: us-east-1
|
||||
APP_STACK_PREFIX: temp-stack
|
||||
steps:
|
||||
- name: Checkout repository
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Derive deployment settings
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
repo_full="${GITHUB_REPOSITORY}"
|
||||
owner_raw="${repo_full%%/*}"
|
||||
repo_raw="${repo_full##*/}"
|
||||
repo_slug="$(printf '%s' "$repo_raw" | tr '[:upper:]' '[:lower:]')"
|
||||
service_slug="${repo_slug}"
|
||||
stack_name="${APP_STACK_PREFIX}-${repo_slug}"
|
||||
account_id="$(aws sts get-caller-identity --query Account --output text)"
|
||||
image_tag="${GITHUB_SHA:-manual}"
|
||||
short_tag="${image_tag:0:12}"
|
||||
registry="${account_id}.dkr.ecr.${AWS_REGION}.amazonaws.com"
|
||||
ecr_repo="temp/${repo_slug}"
|
||||
image_uri="${registry}/${ecr_repo}:${short_tag}"
|
||||
techdocs_bucket="$(aws cloudformation describe-stacks --stack-name "$BACKSTAGE_STACK_NAME" --query "Stacks[0].Outputs[?OutputKey=='TechDocsBucketName'].OutputValue" --output text)"
|
||||
ttl_input="${{ github.event.inputs.ttl }}"
|
||||
case "${ttl_input:-}" in
|
||||
""|"1h") ttl_hours="1" ;;
|
||||
"4h") ttl_hours="4" ;;
|
||||
"24h") ttl_hours="24" ;;
|
||||
*) ttl_hours="1" ;;
|
||||
esac
|
||||
expires_at="$(date -u -d "+${ttl_hours} hour" +%Y-%m-%dT%H:%M:%SZ)"
|
||||
|
||||
{
|
||||
echo "TOOLZOO_OWNER=${owner_raw}"
|
||||
echo "TOOLZOO_REPO=${repo_raw}"
|
||||
echo "TOOLZOO_STACK_NAME=${stack_name}"
|
||||
echo "TOOLZOO_SERVICE_NAME=${service_slug}"
|
||||
echo "TOOLZOO_ECR_REPO=${ecr_repo}"
|
||||
echo "TOOLZOO_IMAGE_URI=${image_uri}"
|
||||
echo "TOOLZOO_TECHDOCS_BUCKET=${techdocs_bucket}"
|
||||
echo "TOOLZOO_EXPIRES_AT=${expires_at}"
|
||||
} >> "$GITHUB_ENV"
|
||||
|
||||
- name: Ensure ECR repository exists
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
aws ecr describe-repositories --repository-names "$TOOLZOO_ECR_REPO" >/dev/null 2>&1 || \
|
||||
aws ecr create-repository \
|
||||
--repository-name "$TOOLZOO_ECR_REPO" \
|
||||
--image-scanning-configuration scanOnPush=true >/dev/null
|
||||
|
||||
- name: Log in to ECR
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
aws ecr get-login-password --region "$AWS_REGION" | \
|
||||
docker login --username AWS --password-stdin "${TOOLZOO_IMAGE_URI%/*}"
|
||||
|
||||
- name: Build and push container image
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
docker build -t "$TOOLZOO_IMAGE_URI" .
|
||||
docker push "$TOOLZOO_IMAGE_URI"
|
||||
|
||||
- name: Deploy ECS service stack
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
aws cloudformation deploy \
|
||||
--stack-name "$TOOLZOO_STACK_NAME" \
|
||||
--template-file infra/cloudformation/service.yaml \
|
||||
--capabilities CAPABILITY_NAMED_IAM \
|
||||
--parameter-overrides \
|
||||
EnvironmentName="$TOOLZOO_STACK_NAME" \
|
||||
ContainerImage="$TOOLZOO_IMAGE_URI" \
|
||||
ServiceName="$TOOLZOO_SERVICE_NAME" \
|
||||
--tags \
|
||||
Owner="$TOOLZOO_OWNER" \
|
||||
Repository="$TOOLZOO_REPO" \
|
||||
ExpiresAt="$TOOLZOO_EXPIRES_AT" \
|
||||
ManagedBy="gitea-actions"
|
||||
|
||||
- name: Write Service URL to catalog metadata
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
service_url="$(aws cloudformation describe-stacks --stack-name "$TOOLZOO_STACK_NAME" --query "Stacks[0].Outputs[?OutputKey=='ServiceUrl'].OutputValue" --output text)"
|
||||
if [[ -z "$service_url" || "$service_url" == "None" ]]; then
|
||||
echo "ServiceUrl output not found for stack $TOOLZOO_STACK_NAME"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
printf '%s\n' \
|
||||
' # BEGIN TOOLZOO MANAGED LINK' \
|
||||
" - url: \"$service_url\"" \
|
||||
' title: Service URL' \
|
||||
' icon: web' \
|
||||
' # END TOOLZOO MANAGED LINK' \
|
||||
> /tmp/toolzoo-managed-link-item.yaml
|
||||
|
||||
awk '
|
||||
BEGIN { in_block=0 }
|
||||
/^ # BEGIN TOOLZOO MANAGED LINK$/ { in_block=1; next }
|
||||
/^ # END TOOLZOO MANAGED LINK$/ { in_block=0; next }
|
||||
in_block==1 { next }
|
||||
{ print }
|
||||
' catalog-info.yaml > /tmp/catalog-info.yaml.base
|
||||
|
||||
if grep -q '^ links:$' /tmp/catalog-info.yaml.base; then
|
||||
awk '
|
||||
BEGIN { inserted=0 }
|
||||
{ print }
|
||||
/^ links:$/ && inserted==0 {
|
||||
while ((getline l < "/tmp/toolzoo-managed-link-item.yaml") > 0) {
|
||||
print l
|
||||
}
|
||||
close("/tmp/toolzoo-managed-link-item.yaml")
|
||||
inserted=1
|
||||
}
|
||||
' /tmp/catalog-info.yaml.base > /tmp/catalog-info.yaml.updated
|
||||
else
|
||||
awk '
|
||||
BEGIN { inserted=0 }
|
||||
/^ labels:$/ && inserted==0 {
|
||||
print " links:"
|
||||
while ((getline l < "/tmp/toolzoo-managed-link-item.yaml") > 0) {
|
||||
print l
|
||||
}
|
||||
close("/tmp/toolzoo-managed-link-item.yaml")
|
||||
inserted=1
|
||||
}
|
||||
/^ annotations:$/ && inserted==0 {
|
||||
print " links:"
|
||||
while ((getline l < "/tmp/toolzoo-managed-link-item.yaml") > 0) {
|
||||
print l
|
||||
}
|
||||
close("/tmp/toolzoo-managed-link-item.yaml")
|
||||
inserted=1
|
||||
}
|
||||
/^spec:$/ && inserted==0 {
|
||||
print " links:"
|
||||
while ((getline l < "/tmp/toolzoo-managed-link-item.yaml") > 0) {
|
||||
print l
|
||||
}
|
||||
close("/tmp/toolzoo-managed-link-item.yaml")
|
||||
inserted=1
|
||||
}
|
||||
{ print }
|
||||
' /tmp/catalog-info.yaml.base > /tmp/catalog-info.yaml.updated
|
||||
fi
|
||||
|
||||
mv /tmp/catalog-info.yaml.updated catalog-info.yaml
|
||||
|
||||
- name: Commit catalog metadata update
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if git diff --quiet -- catalog-info.yaml; then
|
||||
echo "No catalog-info.yaml changes to commit."
|
||||
exit 0
|
||||
fi
|
||||
git config user.name "toolzoo-bot"
|
||||
git config user.email "toolzoo-bot@local"
|
||||
git add catalog-info.yaml
|
||||
git commit -m "Update catalog service URL from deployment output"
|
||||
git push origin HEAD:main
|
||||
|
||||
- name: Publish TechDocs to S3
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
python3 -m venv .techdocs-venv
|
||||
. .techdocs-venv/bin/activate
|
||||
python -m pip install --upgrade pip
|
||||
python -m pip install mkdocs-techdocs-core
|
||||
npx -y @techdocs/cli generate --no-docker
|
||||
npx -y @techdocs/cli publish \
|
||||
--publisher-type awsS3 \
|
||||
--storage-name "$TOOLZOO_TECHDOCS_BUCKET" \
|
||||
--entity "default/Component/${TOOLZOO_REPO}"
|
||||
|
||||
- name: Emit deployment summary
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
: "${GITHUB_STEP_SUMMARY:=/tmp/toolzoo-step-summary}"
|
||||
service_url="$(aws cloudformation describe-stacks --stack-name "$TOOLZOO_STACK_NAME" --query "Stacks[0].Outputs[?OutputKey=='ServiceUrl'].OutputValue" --output text)"
|
||||
{
|
||||
echo "Deployment stack: $TOOLZOO_STACK_NAME"
|
||||
echo "Image: $TOOLZOO_IMAGE_URI"
|
||||
echo "TechDocs bucket: $TOOLZOO_TECHDOCS_BUCKET"
|
||||
echo "ExpiresAt (UTC): $TOOLZOO_EXPIRES_AT"
|
||||
echo "Service URL: $service_url"
|
||||
} | tee -a "$GITHUB_STEP_SUMMARY"
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
|
||||
name: destroy
|
||||
|
||||
on:
|
||||
workflow_dispatch:
|
||||
inputs:
|
||||
confirm_destroy:
|
||||
description: "Type the repository name to confirm destroy"
|
||||
required: true
|
||||
default: ""
|
||||
|
||||
jobs:
|
||||
destroy:
|
||||
runs-on:
|
||||
- toolzoo-host
|
||||
env:
|
||||
AWS_REGION: us-east-1
|
||||
APP_STACK_PREFIX: temp-stack
|
||||
steps:
|
||||
- name: Destroy stack and ECR images
|
||||
shell: bash
|
||||
run: |
|
||||
set -euo pipefail
|
||||
repo_full="${GITHUB_REPOSITORY}"
|
||||
owner_raw="${repo_full%%/*}"
|
||||
repo_raw="${repo_full##*/}"
|
||||
confirm="${{ github.event.inputs.confirm_destroy }}"
|
||||
if [[ "$confirm" != "$repo_raw" ]]; then
|
||||
echo "Refusing destroy: confirm_destroy must exactly match '$repo_raw'."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
repo_slug="$(printf '%s' "$repo_raw" | tr '[:upper:]' '[:lower:]')"
|
||||
stack_name="${APP_STACK_PREFIX}-${repo_slug}"
|
||||
ecr_repo="temp/${repo_slug}"
|
||||
|
||||
aws cloudformation delete-stack --stack-name "$stack_name"
|
||||
aws cloudformation wait stack-delete-complete --stack-name "$stack_name"
|
||||
|
||||
if aws ecr describe-repositories --repository-names "$ecr_repo" >/dev/null 2>&1; then
|
||||
aws ecr delete-repository --repository-name "$ecr_repo" --force
|
||||
fi
|
||||
|
||||
if [[ -f catalog-info.yaml ]]; then
|
||||
awk '
|
||||
BEGIN { in_block=0 }
|
||||
/^ # BEGIN TOOLZOO MANAGED LINK$/ { in_block=1; next }
|
||||
/^ # END TOOLZOO MANAGED LINK$/ { in_block=0; next }
|
||||
in_block==1 { next }
|
||||
{ print }
|
||||
' catalog-info.yaml > /tmp/catalog-info.yaml.cleaned
|
||||
|
||||
if ! cmp -s catalog-info.yaml /tmp/catalog-info.yaml.cleaned; then
|
||||
mv /tmp/catalog-info.yaml.cleaned catalog-info.yaml
|
||||
git config user.name "toolzoo-bot"
|
||||
git config user.email "toolzoo-bot@local"
|
||||
git add catalog-info.yaml
|
||||
git commit -m "Remove stale service URL after destroy"
|
||||
git push origin HEAD:main
|
||||
else
|
||||
rm -f /tmp/catalog-info.yaml.cleaned
|
||||
fi
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user