Skip to content

Commit 4b405a2

Browse files
authored
Refactor code (#17)
1 parent e302a13 commit 4b405a2

File tree

11 files changed

+490
-86
lines changed

11 files changed

+490
-86
lines changed

.github/workflows/ci.yml

+78-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
name: LocalStack Test
22
on:
33
push:
4+
paths-ignore:
5+
- ./*.md
6+
- LICENSE
47
pull_request:
8+
paths-ignore:
9+
- ./*.md
10+
- LICENSE
511
workflow_dispatch:
612
schedule:
713
- cron: '48 23 * * 0'
@@ -13,17 +19,24 @@ jobs:
1319
steps:
1420
- uses: actions/checkout@v3
1521

22+
# We must hack the action call as remote to be able to use the relative paths
23+
# Could it break with different CWD? 🤔
1624
- name: Start LocalStack
17-
uses: ./
25+
uses: jenseng/dynamic-uses@v1
1826
with:
19-
image-tag: 'latest'
20-
install-awslocal: 'true'
21-
configuration: DEBUG=1
22-
use-pro: 'true'
27+
uses: LocalStack/setup-localstack@${{ env.GH_ACTION_VERSION }}
28+
with: |-
29+
{
30+
"image-tag": "latest",
31+
"install-awslocal": "true",
32+
"configuration": "DEBUG=1",
33+
"use-pro": "true",
34+
}
2335
env:
2436
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
37+
GH_ACTION_VERSION: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }}
2538

26-
- name: Run Tests against LocalStack
39+
- name: Run Tests Against LocalStack
2740
run: |
2841
awslocal s3 mb s3://test
2942
awslocal s3 ls
@@ -37,23 +50,75 @@ jobs:
3750
uses: actions/checkout@v3
3851

3952
- name: Start LocalStack
40-
uses: ./
53+
uses: jenseng/dynamic-uses@v1
4154
with:
42-
image-tag: 'latest'
43-
install-awslocal: 'true'
44-
use-pro: 'true'
55+
uses: LocalStack/setup-localstack@${{ env.GH_ACTION_VERSION }}
56+
with: |-
57+
{
58+
"image-tag": "latest",
59+
"install-awslocal": "true",
60+
"configuration": "DEBUG=1",
61+
"use-pro": "true",
62+
}
4563
env:
4664
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
65+
GH_ACTION_VERSION: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }}
4766

4867
- name: Run AWS commands
4968
run: |
5069
awslocal s3 mb s3://test
5170
awslocal sqs create-queue --queue-name test-queue
5271
5372
- name: Save the Cloud Pod
54-
uses: LocalStack/setup-localstack/cloud-pods@main
73+
uses: jenseng/dynamic-uses@v1
5574
with:
56-
name: cloud-pods-test
57-
action: save
75+
uses: LocalStack/setup-localstack@${{ env.GH_ACTION_VERSION }}
76+
with: |-
77+
{
78+
"state-name": "cloud-pods-test",
79+
"state-action": "save",
80+
}
5881
env:
5982
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
83+
GH_ACTION_VERSION: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }}
84+
85+
local-state-test:
86+
name: 'Test Local State Action'
87+
runs-on: ubuntu-latest
88+
steps:
89+
- name: ⚡️ Checkout the repository
90+
uses: actions/checkout@v3
91+
92+
- name: Start LocalStack
93+
uses: jenseng/dynamic-uses@v1
94+
with:
95+
uses: LocalStack/setup-localstack@${{ env.GH_ACTION_VERSION }}
96+
with: |-
97+
{
98+
"image-tag": "latest",
99+
"install-awslocal": "true",
100+
"configuration": "DEBUG=1",
101+
"use-pro": "true",
102+
}
103+
env:
104+
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
105+
GH_ACTION_VERSION: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }}
106+
107+
- name: Run AWS Commands
108+
run: |
109+
awslocal s3 mb s3://test
110+
awslocal sqs create-queue --queue-name test-queue
111+
112+
- name: Save the State Artifact
113+
uses: jenseng/dynamic-uses@v1
114+
with:
115+
uses: LocalStack/setup-localstack@${{ env.GH_ACTION_VERSION }}
116+
with: |-
117+
{
118+
"state-name": "cloud-pods-test",
119+
"state-action": "save",
120+
"state-backend": "local",
121+
}
122+
env:
123+
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
124+
GH_ACTION_VERSION: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }}

.github/workflows/ephemeral.yml

+25-7
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,32 @@ jobs:
99
steps:
1010
- uses: actions/checkout@v3
1111

12-
- name: Deploy Preview
13-
uses: ./preview
12+
- name: Deploy Ephemeral Instance
13+
uses: jenseng/dynamic-uses@v1
1414
with:
15-
localstack-api-key: ${{ secrets.LOCALSTACK_API_KEY }}
16-
github-token: ${{ secrets.GITHUB_TOKEN }}
15+
uses: LocalStack/setup-localstack@${{ env.GH_ACTION_VERSION }}
16+
with: |-
17+
{
18+
"github-token": "${{ secrets.GITHUB_TOKEN }}",
19+
"state-backend": "ephemeral",
20+
"state-action": "start",
21+
"skip-ephemeral-stop": "true"
22+
}
23+
env:
24+
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
25+
GH_ACTION_VERSION: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }}
1726

27+
# We want explicit shutdown
1828
- name: Shutdown ephemeral instance
19-
uses: ./ephemeral/shutdown
29+
uses: jenseng/dynamic-uses@v1
2030
with:
21-
localstack-api-key: ${{ secrets.LOCALSTACK_API_KEY }}
22-
github-token: ${{ secrets.GITHUB_TOKEN }}
31+
uses: LocalStack/setup-localstack@${{ env.GH_ACTION_VERSION }}
32+
with: |-
33+
{
34+
"github-token": "${{ secrets.GITHUB_TOKEN }}",
35+
"state-backend": "ephemeral",
36+
"state-action": "stop"
37+
}
38+
env:
39+
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
40+
GH_ACTION_VERSION: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref_name }}

README.md

+109-10
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,120 @@ A GitHub Action to setup [LocalStack](https://github.com/localstack/localstack)
88
- Pulling a specific version of the LocalStack Docker Image into the GitHub Action runner.
99
- Configuring the [LocalStack CLI](https://docs.localstack.cloud/get-started/#localstack-cli) to launch the Docker container with an optional API token for pro usage.
1010
- 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.
11+
- Export/import [LocalStack state](https://docs.localstack.cloud/user-guide/state-management/export-import-state/) as an artifact
12+
- Save/load [LocalStack Cloud Pods](https://docs.localstack.cloud/user-guide/state-management/cloud-pods/)
13+
- Start/stop a [LocalStack Ephemeral Instance](https://docs.localstack.cloud/user-guide/cloud-sandbox/application-previews/) _(EXPERIMENTAL)_
1114

1215
## Usage
1316

14-
To get started, you can use this minimal example:
17+
### Get started with a minimal example
1518

1619
```yml
1720
- name: Start LocalStack
18-
uses: LocalStack/setup-localstack@v0.1.2
21+
uses: LocalStack/setup-localstack@v0.2.0
1922
with:
2023
image-tag: 'latest'
2124
install-awslocal: 'true'
2225
env:
2326
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
2427
```
28+
> **NOTE**: The `LOCALSTACK_API_KEY` environment variable is required to be set if `use-pro` is set to `true`.
29+
If the key is not found LocalStack by default falls back to the CE edition and displays a warning.
2530

26-
### Inputs
31+
### Install only CLIs and startup later
32+
```yml
33+
- name: Install LocalStack CLIs
34+
uses: LocalStack/[email protected]
35+
with:
36+
skip-startup: 'true'
37+
install-awslocal: 'true'
38+
39+
...
40+
41+
- name: Start LocalStack
42+
uses: LocalStack/[email protected]
43+
with:
44+
image-tag: 'latest'
45+
env:
46+
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
47+
```
48+
49+
### Save a state later on in the pipeline
50+
```yml
51+
- name: Save LocalStack State
52+
uses: LocalStack/[email protected]
53+
with:
54+
install-awslocal: 'true'
55+
state-backend: cloud-pods
56+
state-action: save
57+
state-name: my-cloud-pod
58+
env:
59+
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
60+
```
61+
> **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.
62+
63+
### Load an already saved state
64+
```yml
65+
- name: Start LocalStack and Load State
66+
uses: LocalStack/[email protected]
67+
with:
68+
install-awslocal: 'true'
69+
state-backend: cloud-pods
70+
state-action: load
71+
state-name: my-cloud-pod
72+
env:
73+
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
74+
```
75+
> **NOTE**: To load a **local state** from a different GitHub Actions workflow, one must set the `WORKFLOW_ID` environment variable.
76+
77+
> **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.
78+
79+
### Manage App Preview (Ephemeral Instance)
80+
```yml
81+
uses: LocalStack/setup-localstack@$v0.2.0
82+
with:
83+
github-token: ${{ secrets.GITHUB_TOKEN }}
84+
state-backend: ephemeral
85+
state-action: start
86+
# Adding this option prevents Ephemeral Instance to be stopped after the `preview-cmd` run
87+
skip-ephemeral-stop: 'true'
88+
# Optional script/command to run
89+
preview-cmd: deploy.sh
90+
env:
91+
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
92+
93+
...
94+
95+
with:
96+
uses: LocalStack/setup-localstack@${{ env.GH_ACTION_VERSION }}
97+
with:
98+
github-token: ${{ secrets.GITHUB_TOKEN }}
99+
state-backend: ephemeral
100+
state-action: stop
101+
env:
102+
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
103+
```
104+
105+
## Inputs
27106
28107
| Input | Description | Default |
29108
| ------------------ | -------------------------------------------------------------------------------- | -------- |
109+
| `ci-project` | Name of the CI project to track in LocalStack Cloud | |
110+
| `configuration` | Configuration variables to use while starting LocalStack container | `None` |
111+
| `github-token` | Github token used to create PR comments | |
30112
| `image-tag` | Tag of the LocalStack Docker image to use | `latest` |
113+
| `include-preview` | Whether to include the created Ephemeral Instance URL in the PR comment | `false` |
31114
| `install-awslocal` | Whether to install the `awslocal` CLI into the build environment | `true` |
32-
| `configuration` | Configuration variables to use while starting LocalStack container | `None` |
115+
| `preview-cmd` | Command(s) used to create a Ephemeral Instance of the PR (can use `$AWS_ENDPOINT_URL`) | |
116+
| `skip-ephemeral-stop` | Skip stopping LocalStack Ephemeral Instance | `false` |
117+
| `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` |
118+
| `skip-wait` | Skip waiting for LocalStack to start up | `false` |
119+
| `state-action` | Valid values are `load`, `save`, `start`, `stop`, `` (empty, don't manage state). Values `start`/`stop` only usable with app previews. | `` |
120+
| `state-backend` | Either store the state of LocalStack locally, as a Cloud Pod or start an Ephemeral Instance. Valid values are `cloud-pods`, `ephemeral` or `local`. Use this option in unison with `state-action` to control behaviour. | `cloud-pods` |
121+
| `state-name` | Name of the state artifact (without extension) | `false` |
33122
| `use-pro` | Whether to use the Pro version of LocalStack (requires API key to be configured) | `false` |
34123

35-
> **NOTE**: The `LOCALSTACK_API_KEY` environment variable is required to be set if `use-pro` is set to `true`.
36-
37-
### Example workflow
38-
124+
## Example workflow
39125
```yml
40126
name: LocalStack Test
41127
on: [ push, pull_request ]
@@ -48,20 +134,33 @@ jobs:
48134
- uses: actions/checkout@v3
49135
50136
- name: Start LocalStack
51-
uses: LocalStack/setup-localstack@v0.1.2
137+
uses: LocalStack/setup-localstack@v0.2.0
52138
with:
53139
image-tag: 'latest'
54140
install-awslocal: 'true'
55141
configuration: DEBUG=1
56142
use-pro: 'true'
143+
state-backend: cloud-pods
144+
state-action: load
145+
state-name: my-cloud-pod
57146
env:
58147
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
59148
60149
- name: Run Tests against LocalStack
61150
run: |
62151
awslocal s3 mb s3://test
63152
awslocal s3 ls
64-
echo "Test Execution complete!"
153+
echo "Test Execution complete!"
154+
155+
- name: Save LocalStack State
156+
uses: LocalStack/[email protected]
157+
with:
158+
state-backend: local
159+
state-action: save
160+
state-name: my-ls-state-artifact
161+
env:
162+
LOCALSTACK_API_KEY: ${{ secrets.LOCALSTACK_API_KEY }}
163+
WORKFLOW_ID: ${{ env.MY_GOLDEN_LS_STATE }}
65164
```
66165

67166
## License

0 commit comments

Comments
 (0)