Skip to content

Commit a81707a

Browse files
authored
add initial action to create preview environments from PR builds (#4)
1 parent 4a1f865 commit a81707a

File tree

3 files changed

+88
-3
lines changed

3 files changed

+88
-3
lines changed

action.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ inputs:
3131
description: 'Github token used to create PR comments'
3232
required: false
3333
default: ''
34+
preview-cmd:
35+
description: 'Command(s) used to create a preview of the PR (can use $AWS_ENDPOINT_URL)'
36+
required: false
37+
default: ''
3438

3539
runs:
3640
using: "composite"
@@ -70,3 +74,8 @@ runs:
7074
INSTALL_AWSLOCAL: "${{ inputs.install-awslocal }}"
7175
USE_PRO: "${{ inputs.use-pro }}"
7276
CONFIGURATION: "${{ inputs.configuration }}"
77+
78+
# TODO: potentially add an additional step here to create preview envs, and add a switch to
79+
# enable/disable the "Start LocalStack" action above. This way we could make this action
80+
# the single entry point which then delegates to sub-actions in this repo, based on the
81+
# user-provided configuration...

finish/action.yml

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ inputs:
55
description: 'Github token used to create PR comments'
66
required: true
77
ci-project:
8-
description: 'Name of the CI project to track in LocalStack Cloud'
9-
required: true
8+
description: 'Name of the CI project tracked in LocalStack Cloud'
9+
required: false
10+
include-preview:
11+
description: 'Whether to include the created preview URL in the PR comment'
12+
type: boolean
13+
default: false
1014

1115
runs:
1216
using: composite
@@ -22,12 +26,23 @@ runs:
2226
shell: bash
2327
run: echo "pr_id=$(<pr-id.txt)" >> $GITHUB_OUTPUT
2428

29+
- name: Load the preview URL
30+
shell: bash
31+
if: inputs.include-preview
32+
run: |
33+
if [[ -e ls-preview-url.txt ]]; then
34+
echo "LS_PREVIEW_URL=$(<ls-preview-url.txt)" >> $GITHUB_ENV
35+
else
36+
echo "LS_PREVIEW_URL=Unable to determine preview URL" >> $GITHUB_ENV
37+
fi
38+
2539
- name: Update status comment
2640
uses: actions-cool/maintain-one-comment@v3.1.1
2741
with:
2842
token: ${{ inputs.github-token }}
2943
body: |
30-
🚀 LocalStack Stack Insights and Cloud Pod state for this CI run: https://app.localstack.cloud/ci/${{ inputs.ci-project }}
44+
${{ inputs.ci-project && format('{0}{1}', '🚀 LocalStack Stack Insights and Cloud Pod state for this CI run: https://app.localstack.cloud/ci/', inputs.ci-project) }}
45+
${{ inputs.include-preview && format('{0}{1}', '🚀 Preview for this PR: ', env.LS_PREVIEW_URL) }}
3146
<!-- Sticky Pull Request Comment -->
3247
body-include: '<!-- Sticky Pull Request Comment -->'
3348
number: ${{ steps.pr.outputs.pr_id }}

preview/action.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Create PR Preview
2+
3+
inputs:
4+
github-token:
5+
description: 'Github token used to create PR comments'
6+
required: true
7+
localstack-api-key:
8+
description: 'LocalStack API key used to create the preview environment'
9+
required: true
10+
preview-cmd:
11+
description: 'Command(s) used to create a preview of the PR (can use $AWS_ENDPOINT_URL)'
12+
required: false
13+
default: ''
14+
15+
runs:
16+
using: composite
17+
steps:
18+
- name: Initial PR comment
19+
# TODO: potentially replace with Action version number over time
20+
uses: LocalStack/setup-localstack/prepare@main
21+
if: inputs.github-token
22+
with:
23+
github-token: ${{ inputs.github-token }}
24+
25+
- name: Download PR artifact
26+
uses: actions/download-artifact@v3
27+
with:
28+
name: pr
29+
30+
- name: Create preview environment
31+
shell: bash
32+
run: |
33+
prId=$(<pr-id.txt)
34+
# TODO: make preview name configurable!
35+
previewName=preview-$prId
36+
37+
response=$(curl -X POST -d '{}' \
38+
-H 'authorization: token ${{ inputs.localstack-api-key }}' \
39+
-H 'content-type: application/json' \
40+
https://api.localstack.cloud/v1/previews/$previewName)
41+
endpointUrl=$(echo "$response" | jq -r .endpoint_url)
42+
if [ "$endpointUrl" = "null" ] || [ "$endpointUrl" = "" ]; then
43+
echo "Unable to create preview environment. API response: $response"
44+
exit 1
45+
fi
46+
echo "Created preview environment with endpoint URL: $endpointUrl"
47+
48+
echo $endpointUrl > ./ls-preview-url.txt
49+
echo "LS_PREVIEW_URL=$endpointUrl" >> $GITHUB_ENV
50+
echo "AWS_ENDPOINT_URL=$endpointUrl" >> $GITHUB_ENV
51+
52+
- name: Upload preview instance URL
53+
uses: actions/upload-artifact@v3
54+
with:
55+
name: pr
56+
path: ./ls-preview-url.txt
57+
58+
- name: Run preview deployment
59+
shell: bash
60+
run:
61+
${{ inputs.preview-cmd }}

0 commit comments

Comments
 (0)