-
Notifications
You must be signed in to change notification settings - Fork 3
242 lines (194 loc) · 11 KB
/
deploy-staging.yml
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# Publish / release / deploy packages after a release
name: deploy
on:
push:
branches: [staging]
workflow_dispatch:
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Print contexts
env:
GITHUB_CONTEXT: ${{ toJson(github) }}
ENV_CONTEXT: ${{ toJson(env) }}
VARS_CONTEXT: ${{ toJson(vars) }}
JOB_CONTEXT: ${{ toJson(job) }}
STEPS_CONTEXT: ${{ toJson(steps) }}
RUNNER_CONTEXT: ${{ toJson(runner) }}
SECRETS_CONTEXT: ${{ toJson(secrets) }}
STRATEGY_CONTEXT: ${{ toJson(strategy) }}
MATRIX_CONTEXT: ${{ toJson(matrix) }}
NEEDS_CONTEXT: ${{ toJson(needs) }}
INPUTS_CONTEXT: ${{ toJson(inputs) }}
run: |
echo "******************************"
echo "github:" "$GITHUB_CONTEXT"
echo "******************************"
echo "env:" "$ENV_CONTEXT"
echo "******************************"
echo "vars:" "$VARS_CONTEXT"
echo "******************************"
echo "job:" "$JOB_CONTEXT"
echo "******************************"
echo "steps:" "$STEPS_CONTEXT"
echo "******************************"
echo "runner:" "$RUNNER_CONTEXT"
echo "******************************"
echo "secrets:" "$SECRETS_CONTEXT"
echo "******************************"
echo "strategy:" "$STRATEGY_CONTEXT"
echo "******************************"
echo "matrix:" "$MATRIX_CONTEXT"
echo "******************************"
echo "needs:" "$NEEDS_CONTEXT"
echo "******************************"
echo "inputs:" "$INPUTS_CONTEXT"
echo "******************************"
# Add support for more platforms with QEMU (optional)
# https://github.com/docker/setup-qemu-action
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Dockerhub login
run: |
echo "Logging into Docker Hub."
echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin 2> /dev/null
- uses: actions/checkout@v3
- name: Next version
id: next_version
run: |
set -euxo pipefail # stop on errors, print commands, fail on pipe fails
VERSION=$(cat package.json | jq -r '.version')
# if version not detected, error
if [[ -z "$VERSION" ]]; then
echo "Failed to get version"
exit 1
fi
# or if version null
if [[ "$VERSION" == "null" ]]; then
echo "Failed to get version"
exit 1
fi
# split into major, minor, patch
MAJOR=$(echo $VERSION | cut -d. -f1)
MINOR=$(echo $VERSION | cut -d. -f2)
PATCH=$(echo $VERSION | cut -d. -f3)
echo "version=$VERSION"
echo "major=$MAJOR"
echo "minor=$MINOR"
echo "patch=$PATCH"
# export the next version numbers
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "major=$MAJOR" >> $GITHUB_OUTPUT
echo "minor=$MINOR" >> $GITHUB_OUTPUT
echo "patch=$PATCH" >> $GITHUB_OUTPUT
- uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
- run: npm i -g npm@$(cat package.json | jq -r .engines.npm)
- run: npm ci
# build from scratch to ensure nothing bought over from cache
- name: Build packages
run: |
echo "Building packages..."
npm run build:all
npm run build:all:cjs
- name: Build JS bundle
run: |
set -euxo pipefail # stop on errors, print commands, fail on pipe fails
# Copy staging env file template to staging env file
echo "Copying the staging env template to staging env file in procaptcha-bundle"
cp ./dev/scripts/env.staging ./packages/procaptcha-bundle/.env.staging
# Navigate to the JS bundle directory and build
echo "Navigating to 'packages/procaptcha-bundle' and building JS bundle..."
cd packages/procaptcha-bundle
NODE_ENV=staging npm run bundle:staging
- name: Build docker js_server
run: |
set -euxo pipefail # stop on errors, print commands, fail on pipe fails
# technically, if the release is for a version which is less than a published version, the following code will have bundles from versions ahead of this one. However, this isn't really a problem, as we just want the latest bundle to be available alongside old ones, not so bothered about newer ones.
# Set the JS location in the container
JS_FOLDER="/usr/share/nginx/html/js"
# Get the most recent version of the js_server image
docker pull prosopo/js_server:latest
# Create a temporary container from the latest image
echo "Building Docker image..."
OLD_CONTAINER_ID=$(docker create prosopo/js_server:$VERSION)
# Remove the old js temp folder
rm -rf ./js_bundles_host_temp
# Copy out the old files
docker cp $OLD_CONTAINER_ID:$JS_FOLDER ./js_bundles_host_temp
# Build the new image
docker build --file ./docker/images/js.server.dockerfile . -t prosopo/js_server:staging --no-cache
# Run the new image
NEW_CONTAINER_ID=$(docker create prosopo/js_server:staging)
# Copy the legacy files across
docker cp ./js_bundles_host_temp/ $NEW_CONTAINER_ID:$JS_FOLDER/
# Copy the new bundle files to the container into a folder with the version name
docker cp packages/procaptcha-bundle/dist/bundle/. $NEW_CONTAINER_ID:$JS_FOLDER
# Start the new container
docker start $NEW_CONTAINER_ID
# Move procaptcha.bundle.js
docker exec $NEW_CONTAINER_ID mv $JS_FOLDER/procaptcha.bundle.js $JS_FOLDER/procaptcha.bundle.${{ steps.next_version.outputs.version }}.js
# Symlink JS_FOLDER/procaptcha.bundle.js to JS_FOLDER/procaptcha.bundle.VERSION.js
docker exec $NEW_CONTAINER_ID ln -sf $JS_FOLDER/procaptcha.bundle.${{ steps.next_version.outputs.version }}.js $JS_FOLDER/procaptcha.bundle.js
# Commit the changes to the container
docker commit $NEW_CONTAINER_ID prosopo/js_server:${{ steps.next_version.outputs.version }}
# Check this new docker image works locally
docker run -d -p 3080:80 prosopo/js_server:staging
# Start the bundle demo & run the cypress tests against the new bundle
# npx concurrently "npm run start:bundle" "npm run -w @prosopo/cypress-shared cypress:run:client-bundle-example:js_server" --success "first" --kill-others
- name: Build the production CLI package
run: |
set -euxo pipefail # stop on errors, print commands, fail on pipe fails
echo "Building the production CLI package..."
# Copy the staging env template file to staging env file
echo "Copying the rococo env to production env file in cli package"
cp ./dev/scripts/env.staging ./packages/cli/.env.staging
# Navigate to the provider CLI directory and build
echo "Navigating to 'packages/cli' and bundling..."
cd packages/cli
NODE_ENV=staging npm run build
NODE_ENV=staging npm run bundle:staging
# Navigate back to the project root
echo "Navigating back to project root..."
cd ../..
- name: Docker js_server release
id: docker_js_server_release
run: |
# Push the new staging image to Docker Hub
echo "Pushing Docker image..."
docker push prosopo/js_server:staging
- name: Redeploy flux docker staging js_server
env:
PROSOPO_ZELCORE_PRIVATE_KEY: ${{ secrets.PROSOPO_ZELCORE_PRIVATE_KEY }}
PROSOPO_ZELCORE_PUBLIC_KEY: ${{ secrets.PROSOPO_ZELCORE_PUBLIC_KEY }}
run: |
if [[ "${{ steps.docker_js_server_release.outcome }}" == 'success' ]]; then
echo "Installing @prosopo/flux..."
npm i -g @prosopo/flux
echo "Soft redeploying flux docker staging js_server."
npx flux redeploy prosopoFluxInfrastructure
else
echo "Skipping flux redeploy."
fi
- name: Build and push the Provider Staging Container
uses: docker/build-push-action@v5
with:
context: ${{github.workspace}}
file: ${{github.workspace}}/docker/images/provider.dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: prosopo/provider:staging
- name: Deploy fail notification
if: failure()
run: |
echo "Deploy failed"
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\":check-failed: Deploy <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|v${{ steps.next_version.outputs.version }}> failed.\"}" ${{ secrets.SLACKBOT_DEVOPS }}
- name: Deploy success notification
if: success()
run: |
echo "Deploy succeeded"
curl -X POST -H 'Content-type: application/json' --data "{\"text\":\":check-passed: Deploy <${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|v${{ steps.next_version.outputs.version }}> succeeded.\"}" ${{ secrets.SLACKBOT_DEVOPS }}