-
Notifications
You must be signed in to change notification settings - Fork 135
232 lines (201 loc) · 7.33 KB
/
Copy pathdeploy.yml
File metadata and controls
232 lines (201 loc) · 7.33 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
name: Deployment Pipeline
on:
push:
branches:
- main
- staging
- develop
workflow_dispatch:
inputs:
environment:
description: 'Target environment'
required: true
type: choice
options:
- development
- staging
- production
version:
description: 'Version to deploy (tag or commit SHA)'
required: false
type: string
env:
NODE_VERSION: '20'
jobs:
# Determine target environment based on branch or manual input
determine-environment:
name: Determine Environment
runs-on: ubuntu-latest
outputs:
environment: ${{ steps.set-env.outputs.environment }}
should-deploy: ${{ steps.set-env.outputs.should-deploy }}
steps:
- name: Set environment
id: set-env
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "environment=${{ inputs.environment }}" >> $GITHUB_OUTPUT
echo "should-deploy=true" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" = "refs/heads/main" ]; then
echo "environment=production" >> $GITHUB_OUTPUT
echo "should-deploy=true" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" = "refs/heads/staging" ]; then
echo "environment=staging" >> $GITHUB_OUTPUT
echo "should-deploy=true" >> $GITHUB_OUTPUT
elif [ "${{ github.ref }}" = "refs/heads/develop" ]; then
echo "environment=development" >> $GITHUB_OUTPUT
echo "should-deploy=true" >> $GITHUB_OUTPUT
else
echo "environment=none" >> $GITHUB_OUTPUT
echo "should-deploy=false" >> $GITHUB_OUTPUT
fi
# Build and test
build-and-test:
name: Build and Test
runs-on: ubuntu-latest
needs: determine-environment
if: needs.determine-environment.outputs.should-deploy == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run linter
run: npm run lint
continue-on-error: true
- name: Run tests
run: npm test -- --coverage
- name: Check test coverage
run: |
COVERAGE=$(npm test -- --coverage --coverageReporters=text-summary 2>&1 | grep "Lines" | awk '{print $3}' | sed 's/%//')
echo "Test coverage: ${COVERAGE}%"
if (( $(echo "$COVERAGE < 95" | bc -l) )); then
echo "::warning::Test coverage is below 95% (current: ${COVERAGE}%)"
fi
- name: Build application
run: npm run build
- name: Upload build artifacts
uses: actions/upload-artifact@v4
with:
name: build-artifacts
path: |
dist/
package.json
package-lock.json
retention-days: 7
# Security scanning
security-scan:
name: Security Scan
runs-on: ubuntu-latest
needs: determine-environment
if: needs.determine-environment.outputs.should-deploy == 'true'
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Run npm audit
run: npm audit --audit-level=moderate
continue-on-error: true
- name: Check for vulnerabilities
run: |
VULNS=$(npm audit --json | jq '.metadata.vulnerabilities.total')
if [ "$VULNS" -gt 0 ]; then
echo "::warning::Found $VULNS vulnerabilities"
fi
# Deployment validation
validate-deployment:
name: Validate Deployment
runs-on: ubuntu-latest
needs: [determine-environment, build-and-test]
environment: ${{ needs.determine-environment.outputs.environment }}
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Validate environment configuration
env:
NODE_ENV: ${{ needs.determine-environment.outputs.environment }}
run: |
node -e "
const { loadEnvironmentConfig } = require('./dist/config/environment');
const { validateDeploymentConfig } = require('./dist/deployment/validator');
try {
const config = loadEnvironmentConfig();
const validation = validateDeploymentConfig(config);
console.log('Environment:', config.environment);
console.log('Validation result:', validation.valid ? 'PASSED' : 'FAILED');
if (validation.errors.length > 0) {
console.error('Errors:', validation.errors);
process.exit(1);
}
if (validation.warnings.length > 0) {
console.warn('Warnings:', validation.warnings);
}
} catch (error) {
console.error('Validation failed:', error.message);
process.exit(1);
}
"
# Deploy to environment
deploy:
name: Deploy to ${{ needs.determine-environment.outputs.environment }}
runs-on: ubuntu-latest
needs: [determine-environment, build-and-test, security-scan, validate-deployment]
environment: ${{ needs.determine-environment.outputs.environment }}
steps:
- name: Download build artifacts
uses: actions/download-artifact@v4
with:
name: build-artifacts
- name: Deploy to ${{ needs.determine-environment.outputs.environment }}
run: |
echo "Deploying to ${{ needs.determine-environment.outputs.environment }}"
echo "Version: ${{ github.sha }}"
# Add actual deployment commands here (e.g., AWS, Azure, GCP, etc.)
# Example: aws s3 sync dist/ s3://bucket-name/
# Example: kubectl apply -f k8s/deployment.yaml
- name: Create deployment record
run: |
echo "Deployment completed at $(date -u +"%Y-%m-%dT%H:%M:%SZ")"
echo "Environment: ${{ needs.determine-environment.outputs.environment }}"
echo "Commit: ${{ github.sha }}"
echo "Actor: ${{ github.actor }}"
# Post-deployment health check
health-check:
name: Post-Deployment Health Check
runs-on: ubuntu-latest
needs: [determine-environment, deploy]
steps:
- name: Wait for deployment to stabilize
run: sleep 30
- name: Health check
run: |
echo "Running health check for ${{ needs.determine-environment.outputs.environment }}"
# Add actual health check commands here
# Example: curl -f https://api.example.com/health || exit 1
- name: Notify deployment success
if: success()
run: |
echo "✅ Deployment to ${{ needs.determine-environment.outputs.environment }} successful"
- name: Notify deployment failure
if: failure()
run: |
echo "❌ Deployment to ${{ needs.determine-environment.outputs.environment }} failed"
# Add notification logic (Slack, email, etc.)