Skip to content

Commit

Permalink
Improve application previews by preventing naming collision (#36)
Browse files Browse the repository at this point in the history
* 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 <lukqw@users.noreply.github.com>
  • Loading branch information
lukqw and lukqw authored Aug 9, 2024
1 parent 073dcf5 commit 5b17176
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
14 changes: 10 additions & 4 deletions ephemeral/shutdown/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,24 @@ runs:
shell: bash
run: echo "pr_id=$(<pr-id.txt)" >> $GITHUB_OUTPUT

- name: Shutdown ephemeral instance
- name: Setup preview name
shell: bash
run: |
prId=$(<pr-id.txt)
# TODO: make preview_name configurable
previewName=preview-$prId
repoName=$GITHUB_REPOSITORY
repoNameCleaned=$(echo -n "$repoName" | tr -c '[:alnum:]' '-')
previewName=preview-$repoNameCleaned-$prId
echo "previewName=$previewName" >> $GITHUB_ENV
- name: Shutdown ephemeral instance
shell: bash
run: |
response=$(curl -X DELETE \
-s -o /dev/null -w "%{http_code}" \
-H "ls-api-key: ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \
-H "content-type: application/json" \
https://api.localstack.cloud/v1/compute/instances/$previewName)
if [[ "$response" != "{}" ]]; then
if [[ "$response" -ne 200 ]]; then
# In case the deletion fails, e.g. if the instance cannot be found, we raise a proper error on the platform
echo "Unable to delete preview environment. API response: $response"
exit 1
Expand Down
25 changes: 22 additions & 3 deletions ephemeral/startup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,36 @@ runs:
with:
name: pr

- name: Create preview environment
- name: Setup preview name
shell: bash
run: |
prId=$(<pr-id.txt)
# TODO: make preview name configurable!
previewName=preview-$prId
repoName=$GITHUB_REPOSITORY
repoNameCleaned=$(echo -n "$repoName" | tr -c '[:alnum:]' '-')
previewName=preview-$repoNameCleaned-$prId
echo "previewName=$previewName" >> $GITHUB_ENV
- name: Create preview environment
shell: bash
run: |
autoLoadPod="${AUTO_LOAD_POD:-${{ inputs.auto-load-pod }}}"
extensionAutoInstall="${EXTENSION_AUTO_INSTALL:-${{ inputs.extension-auto-install }}}"
lifetime="${{ inputs.lifetime }}"
list_response=$(curl -X GET \
-H "ls-api-key: ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \
-H "content-type: application/json" \
https://api.localstack.cloud/v1/compute/instances)
instance_exists=$(echo "$list_response" | jq --arg NAME "$previewName" '.[] | select(.instance_name == $NAME)')
if [ -n "$instance_exists" ]; then
del_response=$(curl -X DELETE \
-H "ls-api-key: ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \
-H "content-type: application/json" \
https://api.localstack.cloud/v1/compute/instances/$previewName)
fi
response=$(curl -X POST -d "{\"instance_name\": \"${previewName}\", \"lifetime\": ${lifetime} ,\"env_vars\": {\"AUTO_LOAD_POD\": \"${autoLoadPod}\", \"EXTENSION_AUTO_INSTALL\": \"${extensionAutoInstall}\"}}"\
-H "ls-api-key: ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \
-H "content-type: application/json" \
Expand Down

0 comments on commit 5b17176

Please sign in to comment.