Skip to content

Commit 0b43955

Browse files
committed
add action to teardown S3 preview deployments
1 parent 591e636 commit 0b43955

2 files changed

Lines changed: 141 additions & 0 deletions

File tree

teardown-s3-preview/action.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: 'Teardown S3 Preview'
2+
description: 'Deletes the S3 preview prefix'
3+
inputs:
4+
deployment-name:
5+
description: 'Name of the deployment (used for ConfigMap name)'
6+
required: true
7+
namespace:
8+
description: 'Kubernetes namespace'
9+
required: true
10+
preview-number:
11+
description: 'Preview number (PR number)'
12+
required: true
13+
secret-name:
14+
description: 'Name of the secret containing AWS credentials'
15+
required: false
16+
default: 'app-secrets'
17+
18+
runs:
19+
using: "composite"
20+
steps:
21+
- name: Prepare teardown-s3 job
22+
run: |
23+
JOB_NAME="teardown-s3-preview-${{ inputs.preview-number }}"
24+
CONFIGMAP_NAME="${{ inputs.deployment-name }}-environments"
25+
26+
# Create a temporary file for the manifest
27+
cp ${{ github.action_path }}/job-template.yml job-teardown-s3.yml
28+
29+
# Replace placeholders
30+
sed -i "s/JOB_NAME_PLACEHOLDER/$JOB_NAME/g" job-teardown-s3.yml
31+
sed -i "s/CONFIGMAP_NAME_PLACEHOLDER/$CONFIGMAP_NAME/g" job-teardown-s3.yml
32+
sed -i "s/SECRET_NAME_PLACEHOLDER/${{ inputs.secret-name }}/g" job-teardown-s3.yml
33+
sed -i "s/PREVIEW_NUMBER_PLACEHOLDER/${{ inputs.preview-number }}/g" job-teardown-s3.yml
34+
35+
echo "Prepared job manifest: job-teardown-s3.yml"
36+
shell: bash
37+
38+
- name: Create teardown-s3 job
39+
run: |
40+
kubectl apply --namespace ${{ inputs.namespace }} -f job-teardown-s3.yml
41+
shell: bash
42+
43+
- name: Wait for teardown-s3 job to complete
44+
run: |
45+
JOB_NAME="teardown-s3-preview-${{ inputs.preview-number }}"
46+
NAMESPACE="${{ inputs.namespace }}"
47+
48+
echo "Waiting for job $JOB_NAME in namespace $NAMESPACE..."
49+
50+
if kubectl wait --namespace $NAMESPACE --for=condition=complete --timeout=5m job/$JOB_NAME; then
51+
echo "Job finished with status: Complete"
52+
kubectl logs job/$JOB_NAME --namespace $NAMESPACE
53+
elif kubectl wait --namespace $NAMESPACE --for=condition=failed --timeout=1s job/$JOB_NAME; then
54+
echo "Job finished with status: Failed"
55+
kubectl logs job/$JOB_NAME --namespace $NAMESPACE
56+
exit 1
57+
else
58+
echo "Timeout waiting for job to complete."
59+
kubectl logs job/$JOB_NAME --namespace $NAMESPACE || true
60+
exit 1
61+
fi
62+
shell: bash
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
apiVersion: batch/v1
2+
kind: Job
3+
metadata:
4+
name: JOB_NAME_PLACEHOLDER
5+
spec:
6+
ttlSecondsAfterFinished: 60
7+
template:
8+
spec:
9+
restartPolicy: Never
10+
containers:
11+
- name: teardown-s3-preview
12+
image: amazon/aws-cli:2
13+
command: [ "bash", "-c" ]
14+
args:
15+
- |
16+
set -e
17+
18+
echo "### START $(date --iso-8601=seconds) ###"
19+
20+
S3_ARGS=""
21+
if [ -n "$S3_ENDPOINT" ]; then
22+
S3_ARGS="--endpoint-url $S3_ENDPOINT"
23+
fi
24+
25+
S3_PREVIEW_PREFIX="s3://$S3_BUCKET/preview-$PREVIEW_NUMBER/"
26+
27+
echo "Checking if $S3_PREVIEW_PREFIX exists..."
28+
if [ "$(aws s3 $S3_ARGS ls "$S3_PREVIEW_PREFIX" | wc -l)" -eq 0 ]; then
29+
echo "$S3_PREVIEW_PREFIX does not exist or is empty. Skipping teardown."
30+
echo "Script finished successfully!"
31+
echo "### END $(date --iso-8601=seconds) ###"
32+
exit 0
33+
fi
34+
35+
echo "Removing $S3_PREVIEW_PREFIX recursively..."
36+
aws s3 $S3_ARGS rm "$S3_PREVIEW_PREFIX" --recursive
37+
38+
echo "Script finished successfully!"
39+
echo "### END $(date --iso-8601=seconds) ###"
40+
env:
41+
- name: S3_BUCKET
42+
valueFrom:
43+
configMapKeyRef:
44+
name: CONFIGMAP_NAME_PLACEHOLDER
45+
key: S3_BUCKET
46+
- name: S3_ENDPOINT
47+
valueFrom:
48+
configMapKeyRef:
49+
name: CONFIGMAP_NAME_PLACEHOLDER
50+
key: S3_ENDPOINT
51+
optional: true
52+
- name: AWS_REGION
53+
valueFrom:
54+
configMapKeyRef:
55+
name: CONFIGMAP_NAME_PLACEHOLDER
56+
key: AWS_REGION
57+
- name: AWS_DEFAULT_REGION
58+
valueFrom:
59+
configMapKeyRef:
60+
name: CONFIGMAP_NAME_PLACEHOLDER
61+
key: AWS_REGION
62+
- name: AWS_ACCESS_KEY_ID
63+
valueFrom:
64+
secretKeyRef:
65+
name: SECRET_NAME_PLACEHOLDER
66+
key: AWS_ACCESS_KEY_ID
67+
- name: AWS_SECRET_ACCESS_KEY
68+
valueFrom:
69+
secretKeyRef:
70+
name: SECRET_NAME_PLACEHOLDER
71+
key: AWS_SECRET_ACCESS_KEY
72+
- name: AWS_SESSION_TOKEN
73+
valueFrom:
74+
secretKeyRef:
75+
name: SECRET_NAME_PLACEHOLDER
76+
key: AWS_SESSION_TOKEN
77+
optional: true
78+
- name: PREVIEW_NUMBER
79+
value: "PREVIEW_NUMBER_PLACEHOLDER"

0 commit comments

Comments
 (0)