forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimmutable-infra-pipeline.yml
More file actions
306 lines (268 loc) 路 10.1 KB
/
Copy pathimmutable-infra-pipeline.yml
File metadata and controls
306 lines (268 loc) 路 10.1 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
---
# Immutable Infrastructure Pipeline (issue #556)
# Implements immutable infrastructure principles: all infrastructure components are
# built once, versioned, signed, and never modified after deployment. Any changes
# require building new versions with new version numbers.
name: Immutable Infrastructure Pipeline
on:
push:
branches: [main]
paths:
- 'infrastructure/**'
- '.github/workflows/immutable-infra-pipeline.yml'
pull_request:
paths:
- 'infrastructure/**'
workflow_dispatch:
inputs:
environment:
description: 'Environment to deploy to'
required: true
default: 'staging'
type: choice
options:
- dev
- staging
- production
version:
description: 'Version tag (semver)'
required: false
type: string
env:
# Global environment configuration
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository_owner }}/gistpin-infra
TERRAFORM_VERSION: 1.7.0
HELM_VERSION: 3.14.0
COSIGN_VERSION: 2.2.4
SNYK_VERSION: latest
jobs:
validate:
name: Validate Infrastructure Code
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TERRAFORM_VERSION }}
- name: Set up Helm
uses: azure/setup-helm@v4
with:
version: ${{ env.HELM_VERSION }}
- name: Terraform fmt check
run: |
cd infrastructure/terraform
terraform fmt -check -recursive
- name: Terraform init
run: |
cd infrastructure/terraform/environments/${{ github.event.inputs.environment || 'staging' }}
terraform init -backend=false
- name: Terraform validate
run: |
cd infrastructure/terraform/environments/${{ github.event.inputs.environment || 'staging' }}
terraform validate
- name: Helm lint
run: |
cd infrastructure/k8s
helm lint . --set environment=${{ github.event.inputs.environment || 'staging' }}
- name: Snyk infrastructure scan
uses: snyk/actions/iac@master
with:
args: --sarif-file-output=snyk.sarif
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
- name: Upload Snyk scan results
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: snyk.sarif
build:
name: Build Infrastructure Artifacts
needs: validate
runs-on: ubuntu-latest
outputs:
version: ${{ steps.set-version.outputs.version }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up version
id: set-version
shell: bash
run: |
if [ -n "${{ github.event.inputs.version }}" ]; then
VERSION="${{ github.event.inputs.version }}"
else
TIMESTAMP=$(date +%Y%m%d%H%M%S)
SHORT_SHA=$(git rev-parse --short HEAD)
VERSION="${SHORT_SHA}-${TIMESTAMP}"
fi
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "::set-output name=version::$VERSION"
- name: Log into container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Create infrastructure bundle
run: |
VERSION=${{ steps.set-version.outputs.version }}
mkdir -p .infrabundle
cp -r infrastructure/* .infrabundle/
echo "BUILD_VERSION=$VERSION" >> .infrabundle/metadata.env
echo "GIT_COMMIT=$(git rev-parse HEAD)" >> .infrabundle/metadata.env
echo "BUILD_TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> .infrabundle/metadata.env
echo "BUILT_BY=${{ github.actor }}" >> .infrabundle/metadata.env
- name: Build and tag container images
uses: docker/build-push-action@v5
with:
context: .
file: infrastructure/docker/infra-bundle.Dockerfile
push: false
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.set-version.outputs.version }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
outputs: type=docker,dest=/tmp/infra-bundle.tar
- name: Upload artifact bundle
uses: actions/upload-artifact@v4
with:
name: infra-bundle-${{ steps.set-version.outputs.version }}
path: /tmp/infra-bundle.tar
retention-days: 90
sign:
name: Sign Artifacts (Cosign)
needs: build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download artifact bundle
uses: actions/download-artifact@v4
with:
name: infra-bundle-${{ needs.build.outputs.version }}
path: /tmp
- name: Load docker image
run: docker load --input /tmp/infra-bundle.tar
- name: Set up Cosign
uses: sigstore/cosign-installer@v3.5.0
with:
cosign-release: v${{ env.COSIGN_VERSION }}
- name: Log into container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push and sign image
run: |
IMAGE=${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build.outputs.version }}
docker push $IMAGE
cosign sign --yes $IMAGE
- name: Generate SBOM (Software Bill of Materials)
uses: anchore/sbom-action@v0
with:
image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ needs.build.outputs.version }}
artifact-name: sbom-${{ needs.build.outputs.version }}.spdx.json
output-file: ./sbom.spdx.json
- name: Upload SBOM
uses: actions/upload-artifact@v4
with:
name: sbom-${{ needs.build.outputs.version }}
path: ./sbom.spdx.json
retention-days: 90
deploy:
name: Deploy to ${{ github.event.inputs.environment || 'staging' }}
needs: [build, sign]
runs-on: ubuntu-latest
environment:
name: ${{ github.event.inputs.environment || 'staging' }}
url: https://gistpin.${{ github.event.inputs.environment || 'staging' }}.com
if: github.ref == 'refs/heads/main' || (github.event.inputs.environment != 'production')
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Download artifact bundle
uses: actions/download-artifact@v4
with:
name: infra-bundle-${{ needs.build.outputs.version }}
path: /tmp
- name: Load docker image
run: docker load --input /tmp/infra-bundle.tar
- name: Log into container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Configure kubectl
uses: steebchen/kubectl@v2
with:
config: ${{ secrets.KUBE_CONFIG_DATA }}
- name: Set up Terraform
uses: hashicorp/setup-terraform@v3
with:
terraform_version: ${{ env.TERRAFORM_VERSION }}
- name: Terraform init and apply
run: |
cd infrastructure/terraform/environments/${{ github.event.inputs.environment || 'staging' }}
terraform init
terraform apply -auto-approve \
-var="infra_version=${{ needs.build.outputs.version }}" \
-var="environment=${{ github.event.inputs.environment || 'staging' }}"
- name: Helm upgrade
run: |
cd infrastructure/k8s
helm upgrade --install gistpin . \
--namespace gistpin \
--create-namespace \
--set image.tag=${{ needs.build.outputs.version }} \
--set environment=${{ github.event.inputs.environment || 'staging' }} \
--wait --timeout 15m
- name: Run post-deployment checks
run: |
kubectl get pods -n gistpin
kubectl get services -n gistpin
kubectl get ingress -n gistpin
- name: Send deployment notification
uses: slackapi/slack-github-action@v1
with:
slack-message: "Successfully deployed infrastructure version ${{ needs.build.outputs.version }} to ${{ github.event.inputs.environment || 'staging' }}"
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
promote:
name: Promote to Production
needs: [deploy]
runs-on: ubuntu-latest
environment:
name: production
url: https://gistpin.com
if: github.event.inputs.environment == 'production'
steps:
- name: Verify staging health before promotion
run: |
echo "Running comprehensive health checks in staging before production promotion"
# Add your staging health checks here
curl -f https://api.staging.gistpin.com/health
echo "Staging environment is healthy, proceeding with production deployment"
- name: Log deployment to audit log
run: |
echo "Production deployment initiated:" >> production-audit.log
echo "Version: ${{ needs.build.outputs.version }}" >> production-audit.log
echo "Deployed by: ${{ github.actor }}" >> production-audit.log
echo "Timestamp: $(date -u +"%Y-%m-%dT%H:%M:%SZ")" >> production-audit.log
cat production-audit.log
rollback:
name: Rollback on Failure
if: failure() && github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- name: Trigger rollback workflow
run: |
echo "Deployment failed, initiating rollback to previous stable version"
# Call external rollback script or workflow
curl -X POST https://api.github.com/repos/${{ github.repository }}/actions/workflows/rollback.yml/dispatches \
-H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
-d '{"event_type":"rollback","client_payload":{"version":"${{ needs.build.outputs.version }}","environment":"${{ github.event.inputs.environment || 'staging' }}"}}'