-
Notifications
You must be signed in to change notification settings - Fork 22
283 lines (252 loc) · 9.95 KB
/
Copy pathstaging-pipeline.yml
File metadata and controls
283 lines (252 loc) · 9.95 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
name: staging-pipeline
on:
push:
branches:
- staging
# Security: Set minimal default permissions for all jobs
permissions:
contents: read # All jobs can read code, but can't write by default
jobs:
linters-and-build:
name: Run linters and build
runs-on: ubuntu-latest
permissions:
contents: read # Read code
pull-requests: write # Post lint comments
statuses: write # Update check status
steps:
- name: Check out Git repository
uses: actions/checkout@v5
with:
fetch-depth: 1
- name: Set up Node.js
uses: actions/setup-node@v6
with:
node-version: 22.18.0
cache: 'npm'
# ESLint and Prettier must be in `package.json`
- name: Install Node.js dependencies
run: npm ci
- name: Run linters
uses: wearerequired/lint-action@v2.3.0
with:
eslint: true
#prettier: true
continue_on_error: true
- name: Build project
run: |
echo "🔨 Building TypeScript..."
npm run build
echo "✅ Build complete"
publish:
needs: linters-and-build
runs-on: ubuntu-latest
permissions:
contents: read # Read code
packages: write # Push to GHCR
id-token: write # Cosign signing
outputs:
image-digest: ${{ steps.build.outputs.digest }}
steps:
- name: Check out the repo
uses: actions/checkout@v5
with:
fetch-depth: 1
- name: Login to GitHub Container Registry
uses: docker/login-action@v3.6.0
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3.11.1
# Performance: Build with cache for faster builds
- name: Build image and push to GitHub Packages
id: build
uses: docker/build-push-action@v6.18.0
with:
context: .
push: true
tags: |
ghcr.io/giveth/impact-graph:staging
ghcr.io/giveth/impact-graph:${{ github.sha }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: true
sbom: true
# Security: Sign container image with Cosign
- name: Install Cosign
uses: sigstore/cosign-installer@v4.0.0
- name: Sign container image
run: |
cosign sign --yes ghcr.io/giveth/impact-graph@${{ steps.build.outputs.digest }}
deploy:
needs: publish
runs-on: ubuntu-latest
permissions:
contents: read # Read code for actions
id-token: write # Cosign signature verification
steps:
# Security: Verify image signature before deployment
- name: Install Cosign
uses: sigstore/cosign-installer@v4.0.0
- name: Verify Image Signature
run: |
echo "🔐 Verifying image signature..."
cosign verify ghcr.io/giveth/impact-graph@${{ needs.publish.outputs.image-digest }} \
--certificate-identity "${{ github.server_url }}/${{ github.repository }}/.github/workflows/staging-pipeline.yml@${{ github.ref }}" \
--certificate-oidc-issuer="https://token.actions.githubusercontent.com"
echo "✅ Image signature verified successfully! Image is authentic and untampered."
# Only deploys if signature verification passes
- name: SSH and Pull Verified Image
uses: appleboy/ssh-action@v1.2.2
with:
host: ${{ secrets.STAGING_HOST_ALL }}
username: ${{ secrets.STAGING_USERNAME_ALL }}
key: ${{ secrets.STAGING_PRIVATE_KEY_ALL }}
port: ${{ secrets.SSH_PORT }}
script: |
cd giveth-all
# Pull the verified image by its immutable SHA
docker pull ghcr.io/giveth/impact-graph@${{ needs.publish.outputs.image-digest }}
# Export the verified SHA for docker-compose to use
echo "IMPACT_GRAPH_IMAGE=ghcr.io/giveth/impact-graph@${{ needs.publish.outputs.image-digest }}" > .env.impact-graph
echo "✅ Verified image pulled: ghcr.io/giveth/impact-graph@${{ needs.publish.outputs.image-digest }}"
rollout-deploy-1:
needs: deploy
runs-on: ubuntu-latest
permissions:
contents: none # Minimal - only for actions to work
steps:
- name: SSH and Redeploy
uses: appleboy/ssh-action@v1.2.2
with:
host: ${{ secrets.STAGING_HOST_ALL }}
username: ${{ secrets.STAGING_USERNAME_ALL }}
key: ${{ secrets.STAGING_PRIVATE_KEY_ALL }}
port: ${{ secrets.SSH_PORT }}
script: |
cd giveth-all
# Load the verified image SHA from environment
export $(cat .env.impact-graph | xargs)
## Update each backend service one by one
## First Deployment - uses IMPACT_GRAPH_IMAGE with verified SHA
docker compose up -d --no-deps --force-recreate impact-graph-ql1
docker compose up -d --no-deps --force-recreate impact-graph-jobs
echo "First deployment phase triggered using verified image: $IMPACT_GRAPH_IMAGE"
rollout-deploy-2:
needs: rollout-deploy-1
runs-on: ubuntu-latest
permissions:
contents: none # Minimal - only for actions to work
steps:
- name: SSH and Redeploy
uses: appleboy/ssh-action@v1.2.2
with:
host: ${{ secrets.STAGING_HOST_ALL }}
username: ${{ secrets.STAGING_USERNAME_ALL }}
key: ${{ secrets.STAGING_PRIVATE_KEY_ALL }}
port: ${{ secrets.SSH_PORT }}
script: |
cd giveth-all
# Load the verified image SHA from environment
export $(cat .env.impact-graph | xargs)
## Update each backend service one by one
## Second Deployment - uses IMPACT_GRAPH_IMAGE with verified SHA
docker compose up -d --no-deps --force-recreate impact-graph-ql2
echo "Second deployment phase triggered using verified image: $IMPACT_GRAPH_IMAGE"
rollout-deploy-3:
needs: rollout-deploy-2
runs-on: ubuntu-latest
permissions:
contents: none # Minimal - only for actions to work
steps:
- name: SSH and Redeploy
uses: appleboy/ssh-action@v1.2.2
with:
host: ${{ secrets.STAGING_HOST_ALL }}
username: ${{ secrets.STAGING_USERNAME_ALL }}
key: ${{ secrets.STAGING_PRIVATE_KEY_ALL }}
port: ${{ secrets.SSH_PORT }}
script: |
cd giveth-all
# Load the verified image SHA from environment
export $(cat .env.impact-graph | xargs)
## Update each backend service one by one
## Third Deployment - uses IMPACT_GRAPH_IMAGE with verified SHA
docker compose up -d --no-deps --force-recreate impact-graph-ql3
echo "Third deployment phase triggered using verified image: $IMPACT_GRAPH_IMAGE"
rollout-deploy-4:
needs: rollout-deploy-3
runs-on: ubuntu-latest
permissions:
contents: none # Minimal - only for actions to work
steps:
- name: SSH and Redeploy
uses: appleboy/ssh-action@v1.2.2
with:
host: ${{ secrets.STAGING_HOST_ALL }}
username: ${{ secrets.STAGING_USERNAME_ALL }}
key: ${{ secrets.STAGING_PRIVATE_KEY_ALL }}
port: ${{ secrets.SSH_PORT }}
script: |
set -e
cd giveth-all
# Load the verified image SHA from environment
export $(cat .env.impact-graph | xargs)
## Update each backend service one by one
## Fourth Deployment - uses IMPACT_GRAPH_IMAGE with verified SHA
docker compose up -d --no-deps --force-recreate impact-graph-ql4
echo "Fourth deployment phase triggered using verified image: $IMPACT_GRAPH_IMAGE"
verify-health:
needs:
- rollout-deploy-1
- rollout-deploy-2
- rollout-deploy-3
- rollout-deploy-4
runs-on: ubuntu-latest
permissions:
contents: none # Minimal - only for actions to work
steps:
- name: Wait for services to stabilize
run: sleep 60
- name: SSH and Verify Health
uses: appleboy/ssh-action@v1.2.2
with:
host: ${{ secrets.STAGING_HOST_ALL }}
username: ${{ secrets.STAGING_USERNAME_ALL }}
key: ${{ secrets.STAGING_PRIVATE_KEY_ALL }}
port: ${{ secrets.SSH_PORT }}
script: |
set -e
cd giveth-all
services=("impact-graph-ql1" "impact-graph-ql2" "impact-graph-ql3" "impact-graph-ql4")
echo "=== Final Health Check for All Services ==="
for svc in "${services[@]}"; do
echo "Checking health for $svc ..."
ok=0
for i in {1..12}; do # up to ~2 minutes
status=$(docker inspect --format='{{json .State.Health.Status}}' "$svc" 2>/dev/null || echo '""')
if [ "$status" = "\"healthy\"" ]; then
echo "✓ $svc is healthy"
ok=1
break
fi
echo "Current health: $status; waiting..."
sleep 10
done
if [ $ok -ne 1 ]; then
echo "::error::$svc did not reach healthy status"
# Get container logs for debugging
docker logs --tail 100 "$svc" || true
exit 1
fi
done
echo "Checking impact-graph-jobs is running ..."
job_status=$(docker inspect --format='{{json .State.Status}}' impact-graph-jobs 2>/dev/null || echo '""')
if [ "$job_status" != "\"running\"" ]; then
echo "::error::impact-graph-jobs is not running (status: $job_status)"
exit 1
fi
echo "All services healthy."
docker image prune -f