Skip to content

Commit 5b17176

Browse files
authored
Improve application previews by preventing naming collision (#36)
* make ephemeral instance deployment smart, by checking for existing instances * add more echoes * update shutdown * remove debug output * remove even more debug output * add -n to echo command --------- Co-authored-by: lukqw <[email protected]>
1 parent 073dcf5 commit 5b17176

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

ephemeral/shutdown/action.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,24 @@ runs:
2222
shell: bash
2323
run: echo "pr_id=$(<pr-id.txt)" >> $GITHUB_OUTPUT
2424

25-
- name: Shutdown ephemeral instance
25+
- name: Setup preview name
2626
shell: bash
2727
run: |
2828
prId=$(<pr-id.txt)
29-
# TODO: make preview_name configurable
30-
previewName=preview-$prId
29+
repoName=$GITHUB_REPOSITORY
30+
repoNameCleaned=$(echo -n "$repoName" | tr -c '[:alnum:]' '-')
31+
previewName=preview-$repoNameCleaned-$prId
32+
echo "previewName=$previewName" >> $GITHUB_ENV
3133
34+
- name: Shutdown ephemeral instance
35+
shell: bash
36+
run: |
3237
response=$(curl -X DELETE \
38+
-s -o /dev/null -w "%{http_code}" \
3339
-H "ls-api-key: ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \
3440
-H "content-type: application/json" \
3541
https://api.localstack.cloud/v1/compute/instances/$previewName)
36-
if [[ "$response" != "{}" ]]; then
42+
if [[ "$response" -ne 200 ]]; then
3743
# In case the deletion fails, e.g. if the instance cannot be found, we raise a proper error on the platform
3844
echo "Unable to delete preview environment. API response: $response"
3945
exit 1

ephemeral/startup/action.yml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,36 @@ runs:
5555
with:
5656
name: pr
5757

58-
- name: Create preview environment
58+
- name: Setup preview name
5959
shell: bash
6060
run: |
6161
prId=$(<pr-id.txt)
62-
# TODO: make preview name configurable!
63-
previewName=preview-$prId
62+
repoName=$GITHUB_REPOSITORY
63+
repoNameCleaned=$(echo -n "$repoName" | tr -c '[:alnum:]' '-')
64+
previewName=preview-$repoNameCleaned-$prId
65+
echo "previewName=$previewName" >> $GITHUB_ENV
6466
67+
- name: Create preview environment
68+
shell: bash
69+
run: |
6570
autoLoadPod="${AUTO_LOAD_POD:-${{ inputs.auto-load-pod }}}"
6671
extensionAutoInstall="${EXTENSION_AUTO_INSTALL:-${{ inputs.extension-auto-install }}}"
6772
lifetime="${{ inputs.lifetime }}"
6873
74+
list_response=$(curl -X GET \
75+
-H "ls-api-key: ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \
76+
-H "content-type: application/json" \
77+
https://api.localstack.cloud/v1/compute/instances)
78+
79+
instance_exists=$(echo "$list_response" | jq --arg NAME "$previewName" '.[] | select(.instance_name == $NAME)')
80+
81+
if [ -n "$instance_exists" ]; then
82+
del_response=$(curl -X DELETE \
83+
-H "ls-api-key: ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \
84+
-H "content-type: application/json" \
85+
https://api.localstack.cloud/v1/compute/instances/$previewName)
86+
fi
87+
6988
response=$(curl -X POST -d "{\"instance_name\": \"${previewName}\", \"lifetime\": ${lifetime} ,\"env_vars\": {\"AUTO_LOAD_POD\": \"${autoLoadPod}\", \"EXTENSION_AUTO_INSTALL\": \"${extensionAutoInstall}\"}}"\
7089
-H "ls-api-key: ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \
7190
-H "content-type: application/json" \

0 commit comments

Comments
 (0)