From 4ae40bc658f04cfd1090a620b7c65db63dc2b676 Mon Sep 17 00:00:00 2001 From: lukqw <39307517+lukqw@users.noreply.github.com> Date: Wed, 17 Jul 2024 17:56:03 +0200 Subject: [PATCH 1/2] Update actions around ephemeral instances to new api surface (#31) --- .github/workflows/ephemeral.yml | 19 ++++++++++++++++++- action.yml | 12 ++++++++++++ ephemeral/shutdown/action.yml | 3 +-- ephemeral/startup/action.yml | 18 +++++++++++++++--- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/.github/workflows/ephemeral.yml b/.github/workflows/ephemeral.yml index 0c46669..5b85d35 100644 --- a/.github/workflows/ephemeral.yml +++ b/.github/workflows/ephemeral.yml @@ -19,7 +19,8 @@ jobs: "state-backend": "ephemeral", "state-action": "start", "skip-ephemeral-stop": "true", - "preview-cmd": ${{ toJSON(env.PREVIEW_CMD) }} + "preview-cmd": ${{ toJSON(env.PREVIEW_CMD) }}, + "lifetime": 5, } env: LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }} @@ -28,9 +29,25 @@ jobs: awslocal s3 mb s3://test-bucket awslocal sqs create-queue --queue-name=test-queue echo "Deploy is done." + EXTENSION_AUTO_INSTALL: "localstack-extension-mailhog" + + - name: Assertion step + run: | + sudo apt-get install jq jo + response=$(curl ${AWS_ENDPOINT_URL}/_localstack/extensions/list) + match=$(echo "$response" | jq -r '.[] | select(.distribution.name == "localstack-extension-mailhog") | .distribution.name') + if [ "$match" == "localstack-extension-mailhog" ]; then + echo "Match found: localstack-extension-mailhog" + else + echo "No match found" + echo $response + exit 1 + fi + # We want explicit shutdown - name: Shutdown ephemeral instance + if: ${{ always() }} uses: jenseng/dynamic-uses@v1 with: uses: LocalStack/setup-localstack@${{ env.GH_ACTION_VERSION }} diff --git a/action.yml b/action.yml index b0c4bad..275757b 100644 --- a/action.yml +++ b/action.yml @@ -68,6 +68,18 @@ inputs: state-name: description: 'Name of the state artifact (without extension)' required: false + auto-load-pod: + description: 'The pod to load on startup of LocalStack, the env var AUTO_LOAD_POD' + required: false + default: '' + extension-auto-install: + description: 'The extension(s) to automatically install on startup of LocalStack, the env var EXTENSION_AUTO_INSTALL' + required: false + default: '' + lifetime: + description: 'The lifetime of the ephemeral instance, how long the instance should be available for' + required: false + default: '30' runs: using: "composite" diff --git a/ephemeral/shutdown/action.yml b/ephemeral/shutdown/action.yml index 664e7f7..a2169b4 100644 --- a/ephemeral/shutdown/action.yml +++ b/ephemeral/shutdown/action.yml @@ -31,9 +31,8 @@ runs: response=$(curl -X DELETE \ -H "ls-api-key: ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \ - -H "authorization: token ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \ -H "content-type: application/json" \ - https://api.localstack.cloud/v1/previews/$previewName) + https://api.localstack.cloud/v1/compute/instances/$previewName) if [[ "$response" != "{}" ]]; 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" diff --git a/ephemeral/startup/action.yml b/ephemeral/startup/action.yml index 326a788..fd60ed0 100644 --- a/ephemeral/startup/action.yml +++ b/ephemeral/startup/action.yml @@ -15,6 +15,15 @@ inputs: description: 'The pod to load on startup of LocalStack, the env var AUTO_LOAD_POD' required: false default: '' + extension-auto-install: + description: 'The extension(s) to automatically install on startup of LocalStack, the env var EXTENSION_AUTO_INSTALL' + required: false + default: '' + lifetime: + description: 'The lifetime of the ephemeral instance, how long the instance should be available for' + required: false + default: '30' + runs: using: composite @@ -53,11 +62,14 @@ runs: # TODO: make preview name configurable! previewName=preview-$prId - response=$(curl -X POST -d "{\"auto_load_pod\": \"${AUTO_LOAD_POD:-${{ inputs.auto-load-pod }}}\"}" \ + autoLoadPod="${AUTO_LOAD_POD:-${{ inputs.auto-load-pod }}}" + extensionAutoInstall="${EXTENSION_AUTO_INSTALL:-${{ inputs.extension-auto-install }}}" + lifetime="${{ inputs.lifetime }}" + + 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 "authorization: token ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \ -H "content-type: application/json" \ - https://api.localstack.cloud/v1/previews/$previewName) + https://api.localstack.cloud/v1/compute/instances) endpointUrl=$(echo "$response" | jq -r .endpoint_url) if [ "$endpointUrl" = "null" ] || [ "$endpointUrl" = "" ]; then echo "Unable to create preview environment. API response: $response" From 7bc0b195ea4bef7a215f1ec0f71e41fedc6f0e15 Mon Sep 17 00:00:00 2001 From: lukqw <39307517+lukqw@users.noreply.github.com> Date: Wed, 17 Jul 2024 18:21:44 +0200 Subject: [PATCH 2/2] update README for upcoming release (#32) --- .github/workflows/ci.yml | 8 ++------ .github/workflows/ephemeral.yml | 2 +- README.md | 7 +++++-- action.yml | 4 +++- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6e45678..a70cfe4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,9 +1,5 @@ name: LocalStack Test on: - push: - paths-ignore: - - ./*.md - - LICENSE pull_request: paths-ignore: - ./*.md @@ -137,10 +133,10 @@ jobs: - name: Run AWS Commands run: | - awslocal s3 rb s3://test awslocal s3 mb s3://test - awslocal sqs delete-queue --queue-url $(awslocal sqs get-queue-url --queue-name test-queue --output text) + awslocal s3 rb s3://test awslocal sqs create-queue --queue-name test-queue + awslocal sqs delete-queue --queue-url $(awslocal sqs get-queue-url --queue-name test-queue --output text) - name: Save the State Artifact uses: jenseng/dynamic-uses@v1 diff --git a/.github/workflows/ephemeral.yml b/.github/workflows/ephemeral.yml index 5b85d35..d2ddff8 100644 --- a/.github/workflows/ephemeral.yml +++ b/.github/workflows/ephemeral.yml @@ -21,6 +21,7 @@ jobs: "skip-ephemeral-stop": "true", "preview-cmd": ${{ toJSON(env.PREVIEW_CMD) }}, "lifetime": 5, + "extension-auto-install": "localstack-extension-mailhog", } env: LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }} @@ -29,7 +30,6 @@ jobs: awslocal s3 mb s3://test-bucket awslocal sqs create-queue --queue-name=test-queue echo "Deploy is done." - EXTENSION_AUTO_INSTALL: "localstack-extension-mailhog" - name: Assertion step run: | diff --git a/README.md b/README.md index 17fc5be..7899633 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ A GitHub Action to setup [LocalStack](https://github.com/localstack/localstack) - Installing [LocalStack AWS CLI](https://github.com/localstack/awscli-local), a thin wrapper around the `aws` command line interface for use with LocalStack to run integration tests over AWS services. - Export/import [LocalStack state](https://docs.localstack.cloud/user-guide/state-management/export-import-state/) as an artifact - Save/load [LocalStack Cloud Pods](https://docs.localstack.cloud/user-guide/state-management/cloud-pods/) -- Start/stop a [LocalStack Ephemeral Instance](https://docs.localstack.cloud/user-guide/cloud-sandbox/application-previews/) _(EXPERIMENTAL)_ +- Start/stop a [LocalStack Ephemeral Instance](https://docs.localstack.cloud/user-guide/cloud-sandbox/ephemeral-instance/) _(PREVIEW)_ ## Usage @@ -76,7 +76,7 @@ If the key is not found LocalStack by default falls back to the CE edition and d > **NOTE**: The `LOCALSTACK_API_KEY` environment variable is required to be set to **save/load** LocalStack's state either as a Cloud Pod or as a file artifact. -### Manage App Preview (Ephemeral Instance) +### Manage Application Previews (on an Ephemeral Instance) ```yml uses: LocalStack/setup-localstack@$v0.2.0 with: @@ -106,12 +106,15 @@ with: | Input | Description | Default | | ------------------ | -------------------------------------------------------------------------------- | -------- | +| `auto-load-pod` | Which pod to load on startup of LocalStack (application preview) | `None` | | `ci-project` | Name of the CI project to track in LocalStack Cloud | | | `configuration` | Configuration variables to use while starting LocalStack container | `None` | +| `extension-auto-install` | Which extensions to install on startup of LocalStack (application preview) | `None` | | `github-token` | Github token used to create PR comments | | | `image-tag` | Tag of the LocalStack Docker image to use | `latest` | | `include-preview` | Whether to include the created Ephemeral Instance URL in the PR comment | `false` | | `install-awslocal` | Whether to install the `awslocal` CLI into the build environment | `true` | +| `lifetime` | How long an ephemeral instance should live | 30 | | `preview-cmd` | Command(s) used to create a Ephemeral Instance of the PR (can use `$AWS_ENDPOINT_URL`) | | | `skip-ephemeral-stop` | Skip stopping LocalStack Ephemeral Instance | `false` | | `skip-startup` | Explicitly prevent LocalStack start up, only installs CLI(s). Recommended to manage state later on in the pipeline or start up an ephemeral instance. | `false` | diff --git a/action.yml b/action.yml index 275757b..587adc5 100644 --- a/action.yml +++ b/action.yml @@ -133,7 +133,9 @@ runs: { "github-token": ${{ toJSON(inputs.github-token) }}, "preview-cmd": ${{ toJSON(inputs.preview-cmd) }}, - "auto-load-pod": ${{ toJSON(inputs.state-name) }} + "extension-auto-install": ${{ toJSON(inputs.extension-auto-install )}}, + "auto-load-pod": ${{ toJSON(inputs.auto-load-pod) }}, + "lifetime": ${{ toJSON(inputs.lifetime )}} } # Use different artifact from current workflow's by passing the workflow's id as WORKFLOW_ID env variable