-
Notifications
You must be signed in to change notification settings - Fork 17
205 lines (167 loc) · 7 KB
/
build-deploy.yaml
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
name: Build and Deploy Docker Images
on:
push:
branches: ['main']
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
service:
[
api,
app,
backend,
indexer-balance,
indexer-base,
indexer-events,
indexer-dex,
indexer-multichain,
explorer-selector,
aggregates,
]
fail-fast: false
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
with:
platforms: linux/amd64
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Docker Login
uses: docker/[email protected]
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build and push
id: docker_build
uses: docker/build-push-action@v5
with:
context: .
file: apps/${{ matrix.service }}/Dockerfile
platforms: linux/amd64
push: true
tags: |
ghcr.io/nearblocks/${{ matrix.service }}:latest
ghcr.io/nearblocks/${{ matrix.service }}:${{ github.sha }}
cache-from: type=registry,ref=ghcr.io/nearblocks/${{ matrix.service }}:latest
cache-to: type=inline
- name: Set image output
id: set_image_output
run: |
echo "${{ matrix.service }}=${{ github.sha }}" >> services.json
- name: Save Matrix Result as Artifact
uses: actions/upload-artifact@v3
with:
name: matrix-result
path: services.json
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download Matrix Result Artifact
uses: actions/download-artifact@v3
with:
name: matrix-result
path: .
- name: Set up kubectl
uses: azure/setup-kubectl@v3
with:
version: 'v1.28.0'
- name: Configure kubectl
run: |
echo "${{ secrets.KUBE_CONFIG }}" | base64 -d > kubeconfig
echo "KUBECONFIG=$(pwd)/kubeconfig" >> $GITHUB_ENV
- name: Deploy and verify testnet
run: |
kubectl config set-context --current --namespace=nearblocks-testnet
# Define the list of services for testnet deployment
services=(api app backend indexer-balance indexer-base indexer-events explorer-selector aggregates)
failed_deployments=()
for service in "${services[@]}"; do
service_sha=$(echo '${{ needs.build.outputs.matrix-result }}' | jq -r ".[\"$service\"]")
# Ensure service_sha is valid before updating deployment
if [[ -n "$service" && -n "$service_sha" && "$service_sha" != "false" && "$service_sha" != "null" ]]; then
deployment_name="testnet-$service"
image_name="ghcr.io/nearblocks/$service:$service_sha"
echo "Updating deployment for $deployment_name with image: $image_name"
kubectl set image deployment/$deployment_name $service=$image_name
echo "Verifying rollout for $deployment_name"
if ! kubectl rollout status deployment/$deployment_name --timeout=600s; then
echo "Rollout failed for $deployment_name"
failed_deployments+=("$deployment_name")
fi
else
echo "Skipping deployment for testnet-$service (no updates or invalid SHA)"
fi
done
if [ ${#failed_deployments[@]} -ne 0 ]; then
echo "The following deployments failed:"
printf '%s\n' "${failed_deployments[@]}"
exit 1
fi
- name: Rollback failed testnet deployments
if: failure()
run: |
kubectl config set-context --current --namespace=nearblocks-testnet
# Services to rollback for testnet
services=(api app backend indexer-balance indexer-base indexer-events explorer-selector aggregates)
for service in "${services[@]}"; do
service_sha=$(echo '${{ needs.build.outputs.matrix-result }}' | jq -r ".[\"$service\"]")
if [[ "$service_sha" != "false" && "$service_sha" != "null" ]]; then
deployment_name="testnet-$service"
echo "Attempting rollback for $deployment_name"
kubectl rollout undo deployment/$deployment_name
fi
done
- name: Deploy and verify mainnet
run: |
kubectl config set-context --current --namespace=nearblocks
# Define the list of services for mainnet deployment
services=(api app backend indexer-balance indexer-base indexer-events indexer-dex indexer-multichain explorer-selector aggregates)
failed_deployments=()
for service in "${services[@]}"; do
service_sha=$(echo '${{ needs.build.outputs.matrix-result }}' | jq -r ".[\"$service\"]")
# Ensure service_sha is valid before updating deployment
if [[ -n "$service" && -n "$service_sha" && "$service_sha" != "false" && "$service_sha" != "null" ]]; then
deployment_name="mainnet-$service"
image_name="ghcr.io/nearblocks/$service:$service_sha"
echo "Updating deployment for $deployment_name with image: $image_name"
kubectl set image deployment/$deployment_name $service=$image_name
echo "Verifying rollout for $deployment_name"
if ! kubectl rollout status deployment/$deployment_name --timeout=600s; then
echo "Rollout failed for $deployment_name"
failed_deployments+=("$deployment_name")
fi
else
echo "Skipping deployment for mainnet-$service (no updates or invalid SHA)"
fi
done
if [ ${#failed_deployments[@]} -ne 0 ]; then
echo "The following deployments failed:"
printf '%s\n' "${failed_deployments[@]}"
exit 1
fi
- name: Rollback failed mainnet deployments
if: failure()
run: |
kubectl config set-context --current --namespace=nearblocks
# Services to rollback for mainnet
services=(api app backend indexer-balance indexer-base indexer-events indexer-dex indexer-multichain explorer-selector aggregates)
for service in "${services[@]}"; do
service_sha=$(echo '${{ needs.build.outputs.matrix-result }}' | jq -r ".[\"$service\"]")
deployment_name="mainnet-$service"
if [[ "$service_sha" != "false" && "$service_sha" != "null" ]]; then
echo "Attempting rollback for $deployment_name"
kubectl rollout undo deployment/$deployment_name
fi
done
- name: Notify on failure
if: failure()
run: |
echo "Deployment failed. Manual intervention may be required."