From f74a81e79dbab971c4687bfb3236f8344ce73a23 Mon Sep 17 00:00:00 2001 From: neil Date: Thu, 24 Nov 2022 11:06:02 +0800 Subject: [PATCH] #22 delete preview on close --- .github/remove-distribution.sh | 37 ++++++++++++++++++++++++++++++++++ .github/workflows/cleanup.yml | 35 ++++++++++++++++++++++++++++++++ packages/gui/README.md | 2 ++ 3 files changed, 74 insertions(+) create mode 100755 .github/remove-distribution.sh create mode 100644 .github/workflows/cleanup.yml diff --git a/.github/remove-distribution.sh b/.github/remove-distribution.sh new file mode 100755 index 0000000..16d5dca --- /dev/null +++ b/.github/remove-distribution.sh @@ -0,0 +1,37 @@ +#!/bin/bash +rm ./test.json +rm ./final.json +prefix=$1 + +echo $(aws cloudfront list-distributions --output json) >> ./test.json + +data=$(cat ./test.json) +distribution_id="NONE" +for row in $(jq '.DistributionList.Items[] | @base64' < './test.json'); do + _jq() { + echo $row | tr -d '"' | base64 --decode | jq -r "${1}" | tr -d '"' + } + comment=$(_jq '.Comment') + if [[ $comment = $prefix ]] + then + distribution_id=$(_jq '.Id') + fi +done + + +if [[ $distribution_id = NONE ]] +then + echo "distribution does not exist" +else + echo $distribution_id + config=$(aws cloudfront get-distribution-config --id $distribution_id) + echo $config | jq '.DistributionConfig' | jq '.Enabled = false' >> final.json + etag_get=$(aws cloudfront get-distribution --id $distribution_id | jq '.ETag' | tr -d '"') + do_nothing=$(aws cloudfront update-distribution --id $distribution_id --if-match $etag_get --distribution-config file://final.json) + echo "disabling distribution" + aws cloudfront wait distribution-deployed --id $distribution_id + echo "distribution disabled" + etag_delete=$(aws cloudfront get-distribution --id $distribution_id | jq '.ETag' | tr -d '"') + aws cloudfront delete-distribution --id $distribution_id --if-match $etag_delete + echo "done" +fi \ No newline at end of file diff --git a/.github/workflows/cleanup.yml b/.github/workflows/cleanup.yml new file mode 100644 index 0000000..2bf20bd --- /dev/null +++ b/.github/workflows/cleanup.yml @@ -0,0 +1,35 @@ +on: + pull_request: + types: + - closed + +jobs: + clean-up: + runs-on: ubuntu-latest + steps: + - name: inject slug/short variables + uses: rlespinasse/github-slug-action@v3.x + - name: get s3 preview folder + id: preview + run: | + preview=$(echo $GITHUB_REF_NAME | sed -e 's/\//-/g') + echo "folder=$preview" >> $GITHUB_OUTPUT + - uses: actions/checkout@v3 + + - uses: actions/setup-node@v3 + with: + node-version: 16 + + - uses: aws-actions/configure-aws-credentials@v1 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: us-east-1 + + - name: remove distribution preview + run: .github/remove-distribution.sh ${{ steps.preview.outputs.folder }} + + # consider to rm the folder later, maybe someone might need to download the installer + # - name: remove s3 folder + # run: aws s3 rm s3://preview.gui.tea.xyz/ --recursive --exclude "*" --include "${{ steps.preview.outputs.folder }}/*" + diff --git a/packages/gui/README.md b/packages/gui/README.md index 722c83b..b6f750d 100644 --- a/packages/gui/README.md +++ b/packages/gui/README.md @@ -30,3 +30,5 @@ You can preview the production build with `npm run preview`. ## Intuition Building Links * [Rust module system is weird?](https://www.sheshbabu.com/posts/rust-module-system/) + +TEST PREVIEW