Skip to content

Commit 03ee5ca

Browse files
authored
add S3 preview setup and teardown actions (#8)
* add action to set up S3 preview deployments * add action to teardown S3 preview deployments * parametrize S3 setup and teardown actions with configurable keys * update readme with S3 preview setup and teardown examples * update S3 preview setup and teardown to support configurable keys * reorder inputs and update defaults for S3 preview actions * refactor secret-name input handling in S3 preview actions * update aws-cli image version in S3 preview job templates
1 parent b9ecb45 commit 03ee5ca

5 files changed

Lines changed: 442 additions & 0 deletions

File tree

readme.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,69 @@ The following inputs can be used as `step.with` keys:
9090
| `timeout` | `5m` | The timeout for the Helm command |
9191
| `working-directory` | `.` | The working directory where the action commands will operate |
9292

93+
### Setup S3 Preview
94+
95+
Creates an S3 preview prefix by copying from the main prefix.
96+
97+
#### Example
98+
99+
```yaml
100+
- uses: aboutbits/github-actions-kubernetes/setup-s3-preview@v3
101+
with:
102+
configmap-name: my-app-environments
103+
namespace: my-namespace
104+
preview-number: ${{ github.event.number }}
105+
```
106+
107+
#### Inputs
108+
109+
The following inputs can be used as `step.with` keys:
110+
111+
| Name | Required/Default | Description |
112+
|-----------------------------|-----------------------------|-----------------------------------------------------|
113+
| `configmap-name` | `app-spring-deployment-environments` | Name of the ConfigMap |
114+
| `namespace` | required | Kubernetes namespace |
115+
| `preview-number` | required | Preview number (PR number) |
116+
| `secret-name` | `app-secrets` | Name of the secret containing credentials |
117+
| `s3-bucket-key` | `S3_BUCKET` | Key for S3_BUCKET in ConfigMap |
118+
| `s3-endpoint-key` | `S3_ENDPOINT` | Key for S3_ENDPOINT in ConfigMap |
119+
| `s3-region-key` | `S3_REGION` | Key for S3_REGION in ConfigMap |
120+
| `s3-root-folder-key` | `S3_ROOT_FOLDER` | Key for S3_ROOT_FOLDER in ConfigMap |
121+
| `s3-force-path-style-access-key` | `S3_FORCE_PATH_STYLE_ACCESS` | Key for S3_FORCE_PATH_STYLE_ACCESS in ConfigMap |
122+
| `s3-access-key-key` | `S3_ACCESS_KEY` | Key for S3_ACCESS_KEY in Secret |
123+
| `s3-secret-key-key` | `S3_SECRET_KEY` | Key for S3_SECRET_KEY in Secret |
124+
125+
### Teardown S3 Preview
126+
127+
Deletes the S3 preview prefix.
128+
129+
#### Example
130+
131+
```yaml
132+
- uses: aboutbits/github-actions-kubernetes/teardown-s3-preview@v3
133+
with:
134+
configmap-name: my-app-environments
135+
namespace: my-namespace
136+
preview-number: ${{ github.event.number }}
137+
```
138+
139+
#### Inputs
140+
141+
The following inputs can be used as `step.with` keys:
142+
143+
| Name | Required/Default | Description |
144+
|-----------------------------|-----------------------------|-----------------------------------------------------|
145+
| `configmap-name` | `app-spring-deployment-environments` | Name of the ConfigMap |
146+
| `namespace` | required | Kubernetes namespace |
147+
| `preview-number` | required | Preview number (PR number) |
148+
| `secret-name` | `app-secrets` | Name of the secret containing credentials |
149+
| `s3-bucket-key` | `S3_BUCKET` | Key for S3_BUCKET in ConfigMap |
150+
| `s3-endpoint-key` | `S3_ENDPOINT` | Key for S3_ENDPOINT in ConfigMap |
151+
| `s3-region-key` | `S3_REGION` | Key for S3_REGION in ConfigMap |
152+
| `s3-force-path-style-access-key` | `S3_FORCE_PATH_STYLE_ACCESS` | Key for S3_FORCE_PATH_STYLE_ACCESS in ConfigMap |
153+
| `s3-access-key-key` | `S3_ACCESS_KEY` | Key for S3_ACCESS_KEY in Secret |
154+
| `s3-secret-key-key` | `S3_SECRET_KEY` | Key for S3_SECRET_KEY in Secret |
155+
93156
### Setup PostgreSQL Preview Schema
94157

95158
Sets up a PostgreSQL preview schema by cloning a base schema.

setup-s3-preview/action.yml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
name: 'Setup S3 Preview'
2+
description: 'Creates an S3 preview prefix by copying from the main prefix'
3+
inputs:
4+
namespace:
5+
description: 'Kubernetes namespace'
6+
required: true
7+
preview-number:
8+
description: 'Preview number (PR number)'
9+
required: true
10+
configmap-name:
11+
description: 'Name of the ConfigMap'
12+
required: false
13+
default: 'app-spring-deployment-environments'
14+
secret-name:
15+
description: 'Name of the secret containing credentials'
16+
required: false
17+
default: 'app-secrets'
18+
s3-bucket-key:
19+
description: 'Key for S3_BUCKET in ConfigMap'
20+
required: false
21+
default: 'S3_BUCKET'
22+
s3-endpoint-key:
23+
description: 'Key for S3_ENDPOINT in ConfigMap'
24+
required: false
25+
default: 'S3_ENDPOINT'
26+
s3-region-key:
27+
description: 'Key for S3_REGION in ConfigMap'
28+
required: false
29+
default: 'S3_REGION'
30+
s3-root-folder-key:
31+
description: 'Key for S3_ROOT_FOLDER in ConfigMap'
32+
required: false
33+
default: 'S3_ROOT_FOLDER'
34+
s3-force-path-style-access-key:
35+
description: 'Key for S3_FORCE_PATH_STYLE_ACCESS in ConfigMap'
36+
required: false
37+
default: 'S3_FORCE_PATH_STYLE_ACCESS'
38+
s3-access-key-key:
39+
description: 'Key for S3_ACCESS_KEY in Secret'
40+
required: false
41+
default: 'S3_ACCESS_KEY'
42+
s3-secret-key-key:
43+
description: 'Key for S3_SECRET_KEY in Secret'
44+
required: false
45+
default: 'S3_SECRET_KEY'
46+
47+
runs:
48+
using: "composite"
49+
steps:
50+
- name: Prepare setup-s3 job
51+
env:
52+
JOB_NAME: "prepare-s3-preview-${{ inputs.preview-number }}"
53+
CONFIGMAP_NAME: "${{ inputs.configmap-name }}"
54+
SECRET_NAME: "${{ inputs.secret-name }}"
55+
S3_BUCKET_KEY: "${{ inputs.s3-bucket-key }}"
56+
S3_ENDPOINT_KEY: "${{ inputs.s3-endpoint-key }}"
57+
S3_REGION_KEY: "${{ inputs.s3-region-key }}"
58+
S3_ROOT_FOLDER_KEY: "${{ inputs.s3-root-folder-key }}"
59+
S3_FORCE_PATH_STYLE_ACCESS_KEY: "${{ inputs.s3-force-path-style-access-key }}"
60+
S3_ACCESS_KEY_KEY: "${{ inputs.s3-access-key-key }}"
61+
S3_SECRET_KEY_KEY: "${{ inputs.s3-secret-key-key }}"
62+
PREVIEW_NUMBER: "${{ inputs.preview-number }}"
63+
run: |
64+
envsubst '
65+
$JOB_NAME
66+
$CONFIGMAP_NAME
67+
$SECRET_NAME
68+
$S3_BUCKET_KEY
69+
$S3_ENDPOINT_KEY
70+
$S3_REGION_KEY
71+
$S3_ROOT_FOLDER_KEY
72+
$S3_FORCE_PATH_STYLE_ACCESS_KEY
73+
$S3_ACCESS_KEY_KEY
74+
$S3_SECRET_KEY_KEY
75+
$PREVIEW_NUMBER
76+
' < ${{ github.action_path }}/job-template.yml > job-setup-s3.yml
77+
78+
echo "Prepared job manifest: job-setup-s3.yml"
79+
shell: bash
80+
81+
- name: Create setup-s3 job
82+
run: |
83+
kubectl apply --namespace ${{ inputs.namespace }} -f job-setup-s3.yml
84+
shell: bash
85+
86+
- name: Wait for setup-s3 job to complete
87+
env:
88+
JOB_NAME: "prepare-s3-preview-${{ inputs.preview-number }}"
89+
NAMESPACE: "${{ inputs.namespace }}"
90+
run: |
91+
echo "Waiting for job $JOB_NAME in namespace $NAMESPACE..."
92+
93+
if kubectl wait --namespace $NAMESPACE --for=condition=complete --timeout=10m job/$JOB_NAME; then
94+
echo "Job finished with status: Complete"
95+
kubectl logs job/$JOB_NAME --namespace $NAMESPACE
96+
elif kubectl wait --namespace $NAMESPACE --for=condition=failed --timeout=1s job/$JOB_NAME; then
97+
echo "Job finished with status: Failed"
98+
kubectl logs job/$JOB_NAME --namespace $NAMESPACE
99+
exit 1
100+
else
101+
echo "Timeout waiting for job to complete."
102+
kubectl logs job/$JOB_NAME --namespace $NAMESPACE || true
103+
exit 1
104+
fi
105+
shell: bash

setup-s3-preview/job-template.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
apiVersion: batch/v1
2+
kind: Job
3+
metadata:
4+
name: ${JOB_NAME}
5+
labels:
6+
app.kubernetes.io/managed-by: github-actions
7+
app.kubernetes.io/component: s3-preview-setup
8+
spec:
9+
ttlSecondsAfterFinished: 60
10+
template:
11+
spec:
12+
restartPolicy: Never
13+
containers:
14+
- name: prepare-s3-preview
15+
image: amazon/aws-cli:2.34.31
16+
command: [ "bash", "-c" ]
17+
args:
18+
- |
19+
set -e
20+
21+
echo "### START $(date --iso-8601=seconds) ###"
22+
23+
if [ "$S3_FORCE_PATH_STYLE_ACCESS" = "true" ]; then
24+
aws configure set default.s3.addressing_style path
25+
fi
26+
27+
S3_ARGS=""
28+
if [ -n "$S3_ENDPOINT" ]; then
29+
S3_ARGS="--endpoint-url $S3_ENDPOINT"
30+
fi
31+
32+
S3_SOURCE_PREFIX=""
33+
if [ -n "$S3_ROOT_FOLDER" ]; then
34+
S3_SOURCE_PREFIX="s3://$S3_BUCKET/${S3_ROOT_FOLDER%/}/"
35+
else
36+
S3_SOURCE_PREFIX="s3://$S3_BUCKET/"
37+
fi
38+
39+
S3_PREVIEW_PREFIX="s3://$S3_BUCKET/preview-${PREVIEW_NUMBER}/"
40+
41+
echo "Checking if $S3_PREVIEW_PREFIX already exists..."
42+
if [ "$(aws s3 $S3_ARGS ls "$S3_PREVIEW_PREFIX" | wc -l)" -gt 0 ]; then
43+
echo "$S3_PREVIEW_PREFIX already exists. Skipping setup."
44+
echo "Script finished successfully!"
45+
echo "### END $(date --iso-8601=seconds) ###"
46+
exit 0
47+
fi
48+
49+
echo "Syncing from $S3_SOURCE_PREFIX to $S3_PREVIEW_PREFIX ..."
50+
aws s3 $S3_ARGS sync "$S3_SOURCE_PREFIX" "$S3_PREVIEW_PREFIX"
51+
52+
echo "Script finished successfully!"
53+
echo "### END $(date --iso-8601=seconds) ###"
54+
env:
55+
- name: S3_BUCKET
56+
valueFrom:
57+
configMapKeyRef:
58+
name: ${CONFIGMAP_NAME}
59+
key: ${S3_BUCKET_KEY}
60+
- name: S3_ENDPOINT
61+
valueFrom:
62+
configMapKeyRef:
63+
name: ${CONFIGMAP_NAME}
64+
key: ${S3_ENDPOINT_KEY}
65+
optional: true
66+
- name: AWS_DEFAULT_REGION
67+
valueFrom:
68+
configMapKeyRef:
69+
name: ${CONFIGMAP_NAME}
70+
key: ${S3_REGION_KEY}
71+
- name: S3_ROOT_FOLDER
72+
valueFrom:
73+
configMapKeyRef:
74+
name: ${CONFIGMAP_NAME}
75+
key: ${S3_ROOT_FOLDER_KEY}
76+
optional: true
77+
- name: S3_FORCE_PATH_STYLE_ACCESS
78+
valueFrom:
79+
configMapKeyRef:
80+
name: ${CONFIGMAP_NAME}
81+
key: ${S3_FORCE_PATH_STYLE_ACCESS_KEY}
82+
optional: true
83+
- name: AWS_ACCESS_KEY_ID
84+
valueFrom:
85+
secretKeyRef:
86+
name: ${SECRET_NAME}
87+
key: ${S3_ACCESS_KEY_KEY}
88+
- name: AWS_SECRET_ACCESS_KEY
89+
valueFrom:
90+
secretKeyRef:
91+
name: ${SECRET_NAME}
92+
key: ${S3_SECRET_KEY_KEY}
93+
- name: PREVIEW_NUMBER
94+
value: "${PREVIEW_NUMBER}"

teardown-s3-preview/action.yml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
name: 'Teardown S3 Preview'
2+
description: 'Deletes the S3 preview prefix'
3+
inputs:
4+
namespace:
5+
description: 'Kubernetes namespace'
6+
required: true
7+
preview-number:
8+
description: 'Preview number (PR number)'
9+
required: true
10+
configmap-name:
11+
description: 'Name of the ConfigMap'
12+
required: false
13+
default: 'app-spring-deployment-environments'
14+
secret-name:
15+
description: 'Name of the secret containing credentials'
16+
required: false
17+
default: 'app-secrets'
18+
s3-bucket-key:
19+
description: 'Key for S3_BUCKET in ConfigMap'
20+
required: false
21+
default: 'S3_BUCKET'
22+
s3-endpoint-key:
23+
description: 'Key for S3_ENDPOINT in ConfigMap'
24+
required: false
25+
default: 'S3_ENDPOINT'
26+
s3-region-key:
27+
description: 'Key for S3_REGION in ConfigMap'
28+
required: false
29+
default: 'S3_REGION'
30+
s3-force-path-style-access-key:
31+
description: 'Key for S3_FORCE_PATH_STYLE_ACCESS in ConfigMap'
32+
required: false
33+
default: 'S3_FORCE_PATH_STYLE_ACCESS'
34+
s3-access-key-key:
35+
description: 'Key for S3_ACCESS_KEY in Secret'
36+
required: false
37+
default: 'S3_ACCESS_KEY'
38+
s3-secret-key-key:
39+
description: 'Key for S3_SECRET_KEY in Secret'
40+
required: false
41+
default: 'S3_SECRET_KEY'
42+
43+
runs:
44+
using: "composite"
45+
steps:
46+
- name: Prepare teardown-s3 job
47+
env:
48+
JOB_NAME: "teardown-s3-preview-${{ inputs.preview-number }}"
49+
CONFIGMAP_NAME: "${{ inputs.configmap-name }}"
50+
SECRET_NAME: "${{ inputs.secret-name }}"
51+
S3_BUCKET_KEY: "${{ inputs.s3-bucket-key }}"
52+
S3_ENDPOINT_KEY: "${{ inputs.s3-endpoint-key }}"
53+
S3_REGION_KEY: "${{ inputs.s3-region-key }}"
54+
S3_FORCE_PATH_STYLE_ACCESS_KEY: "${{ inputs.s3-force-path-style-access-key }}"
55+
S3_ACCESS_KEY_KEY: "${{ inputs.s3-access-key-key }}"
56+
S3_SECRET_KEY_KEY: "${{ inputs.s3-secret-key-key }}"
57+
PREVIEW_NUMBER: "${{ inputs.preview-number }}"
58+
run: |
59+
envsubst '
60+
$JOB_NAME
61+
$CONFIGMAP_NAME
62+
$SECRET_NAME
63+
$S3_BUCKET_KEY
64+
$S3_ENDPOINT_KEY
65+
$S3_REGION_KEY
66+
$S3_FORCE_PATH_STYLE_ACCESS_KEY
67+
$S3_ACCESS_KEY_KEY
68+
$S3_SECRET_KEY_KEY
69+
$PREVIEW_NUMBER
70+
' < ${{ github.action_path }}/job-template.yml > job-teardown-s3.yml
71+
72+
echo "Prepared job manifest: job-teardown-s3.yml"
73+
shell: bash
74+
75+
- name: Create teardown-s3 job
76+
run: |
77+
kubectl apply --namespace ${{ inputs.namespace }} -f job-teardown-s3.yml
78+
shell: bash
79+
80+
- name: Wait for teardown-s3 job to complete
81+
env:
82+
JOB_NAME: "teardown-s3-preview-${{ inputs.preview-number }}"
83+
NAMESPACE: "${{ inputs.namespace }}"
84+
run: |
85+
echo "Waiting for job $JOB_NAME in namespace $NAMESPACE..."
86+
87+
if kubectl wait --namespace $NAMESPACE --for=condition=complete --timeout=5m job/$JOB_NAME; then
88+
echo "Job finished with status: Complete"
89+
kubectl logs job/$JOB_NAME --namespace $NAMESPACE
90+
elif kubectl wait --namespace $NAMESPACE --for=condition=failed --timeout=1s job/$JOB_NAME; then
91+
echo "Job finished with status: Failed"
92+
kubectl logs job/$JOB_NAME --namespace $NAMESPACE
93+
exit 1
94+
else
95+
echo "Timeout waiting for job to complete."
96+
kubectl logs job/$JOB_NAME --namespace $NAMESPACE || true
97+
exit 1
98+
fi
99+
shell: bash

0 commit comments

Comments
 (0)