-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathaction.yml
More file actions
131 lines (118 loc) · 4.82 KB
/
action.yml
File metadata and controls
131 lines (118 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
name: Create PR Preview
inputs:
github-token:
description: 'Github token used to create PR comments'
required: true
localstack-api-key:
description: 'LocalStack API key used to create the preview environment'
required: false
preview-cmd:
description: 'Command(s) used to create a preview of the PR (can use $AWS_ENDPOINT_URL)'
required: false
default: ''
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'
ephemeral-configuration:
description: 'Configuration for the ephemeral LocalStack environment to be created'
required: false
default: ''
runs:
using: composite
steps:
- run: >
echo "GH_ACTION_ROOT=$(
ls -d $(
ls -d ./../../_actions/* |
grep -i localstack |
tail -n1
)/setup-localstack/* |
grep -v completed |
tail -n1
)" >> $GITHUB_ENV
shell: bash
- name: Initial PR comment
if: inputs.github-token
uses: jenseng/dynamic-uses@v1
with:
uses: ${{ env.GH_ACTION_ROOT }}/prepare
with: |-
{
"github-token": ${{ toJSON(inputs.github-token) }}
}
- name: Download PR artifact
uses: actions/download-artifact@v3
with:
name: pr
- name: Setup preview name
shell: bash
run: |
prId=$(<pr-id.txt)
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 }}"
configuration="${{ inputs.ephemeral-configuration }}"
echo "Creating preview environment with configuration: $configuration"
# Convert configuration to JSON format
IFS=',' read -r -a configArray <<< "$configuration"
envVarsJson=$(jq -n '{}')
for config in "${configArray[@]}"; do
IFS='=' read -r -a kv <<< "$config"
key=${kv[0]// /}
value=${kv[1]// /}
envVarsJson=$(echo $envVarsJson | jq --arg key "$key" --arg value "$value" '. + {($key): $value}')
done
echo "Configuration JSON: $envVarsJson"
envVarsJson=$(echo $envVarsJson | jq --arg autoLoadPod "$autoLoadPod" --arg extensionAutoInstall "$extensionAutoInstall" '. + {AUTO_LOAD_POD: $autoLoadPod, EXTENSION_AUTO_INSTALL: $extensionAutoInstall}')
echo "*** Configuration ***"
echo $envVarsJson
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\": ${envVarsJson}}"\
-H "ls-api-key: ${LOCALSTACK_API_KEY:-${{ inputs.localstack-api-key }}}" \
-H "content-type: application/json" \
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"
exit 1
fi
echo "Created preview environment with endpoint URL: $endpointUrl"
echo $endpointUrl > ./ls-preview-url.txt
echo "LS_PREVIEW_URL=$endpointUrl" >> $GITHUB_ENV
echo "AWS_ENDPOINT_URL=$endpointUrl" >> $GITHUB_ENV
- name: Upload preview instance URL
uses: actions/upload-artifact@v3
with:
name: pr
path: ./ls-preview-url.txt
- name: Run preview deployment
if: ${{ inputs.preview-cmd != '' }}
shell: bash
run:
${{ inputs.preview-cmd }}