Repository navigation
Expand file tree
/
Copy pathmulti-cicd.yml
More file actions
532 lines (450 loc) · 14.1 KB
/
Copy pathmulti-cicd.yml
File metadata and controls
532 lines (450 loc) · 14.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# # Multi-CI/CD Platform Integration Example
# This example demonstrates using SecretZero to manage secrets across GitHub Actions,
# GitLab CI/CD, Azure DevOps Services, and Jenkins simultaneously.
# ## Use Case
# Many organizations use multiple CI/CD platforms:
# - GitHub Actions for open-source projects
# - GitLab CI/CD for internal projects
# - Azure DevOps Pipelines for Microsoft-centric teams
# - Jenkins for legacy pipelines
# SecretZero allows you to manage all these secrets from a single source of truth.
# The ``shared_deploy_token`` secret shows a cross-provider pattern: mint a GitLab
# group service account PAT once, then sync it into GitLab group variables and an
# Azure DevOps Library variable group.
# ## Configuration
# ```yaml
variables:
# GitHub variables
github_org: myorg
github_repo: myrepo
# GitLab variables (use `auto` to resolve from CI_PROJECT_PATH or git remote)
gitlab_project: mygroup/myproject
gitlab_group: auto
# See examples/gitlab-project-token.yml / gitlab-group-service-account.yml for token generators.
# Azure DevOps Services
azdo_project: auto
azdo_organization: myorg
# Jenkins variables
jenkins_url: https://jenkins.example.com
providers:
github:
kind: github
auth:
kind: token
config:
token: ${GITHUB_TOKEN}
gitlab:
kind: gitlab
auth:
kind: token
config:
token: ${GITLAB_TOKEN}
azdo:
kind: azure_devops
auth:
kind: token
config:
token: ${AZDO_PAT}
organization: ${azdo_organization}
jenkins:
kind: jenkins
auth:
kind: token
config:
url: ${jenkins_url}
username: ${JENKINS_USERNAME}
token: ${JENKINS_TOKEN}
secrets:
# Shared API key across all platforms
- name: external_api_key
kind: random_string
config:
length: 40
charset: alphanumeric
one_time: true # Generate once, never change
targets:
# Local development
- provider: local
kind: file
config:
path: .env
format: dotenv
# GitHub Actions
- provider: github
kind: github_secret
config:
owner: ${github_org}
repo: ${github_repo}
# GitLab CI/CD
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
masked: true
# Azure DevOps Library
- provider: azdo
kind: azdo_variable_group
config:
project: auto
variable_group: shared-secrets
variable_name: EXTERNAL_API_KEY
is_secret: true
create_if_missing: true
# Jenkins
- provider: jenkins
kind: jenkins_credential
config:
credential_id: external-api-key
description: External API Key (SecretZero managed)
# Database password - production only
- name: database_password
kind: random_password
config:
length: 32
special: true
rotation_period: 90d # Rotate every 90 days
targets:
# GitHub environment secret
- provider: github
kind: github_secret
config:
owner: ${github_org}
repo: ${github_repo}
environment: production
# GitLab protected variable
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
protected: true
masked: true
environment_scope: production
# Jenkins credential
- provider: jenkins
kind: jenkins_credential
config:
credential_id: db-password-prod
description: Production Database Password
# Docker registry credentials
- name: docker_registry_password
kind: static
config:
default: ${DOCKER_PASSWORD}
targets:
# GitHub Actions
- provider: github
kind: github_secret
config:
owner: ${github_org}
repo: ${github_repo}
# GitLab CI/CD
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
masked: true
# Jenkins username/password
- provider: jenkins
kind: jenkins_credential
config:
credential_id: docker-registry
description: Docker Registry Credentials
credential_type: username_password
username: ${DOCKER_USERNAME}
# AWS credentials for deployment
- name: aws_access_key
kind: static
config:
default: ${AWS_ACCESS_KEY_ID}
targets:
- provider: github
kind: github_secret
config:
owner: ${github_org}
repo: ${github_repo}
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
masked: true
- provider: jenkins
kind: jenkins_credential
config:
credential_id: aws-access-key
description: AWS Access Key
- name: aws_secret_key
kind: static
config:
default: ${AWS_SECRET_ACCESS_KEY}
targets:
- provider: github
kind: github_secret
config:
owner: ${github_org}
repo: ${github_repo}
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
masked: true
- provider: jenkins
kind: jenkins_credential
config:
credential_id: aws-secret-key
description: AWS Secret Key
# Slack webhook for notifications (all platforms)
- name: slack_webhook
kind: static
config:
default: ${SLACK_WEBHOOK_URL}
targets:
- provider: github
kind: github_secret
config:
owner: ${github_org}
repo: ${github_repo}
- provider: gitlab
kind: gitlab_variable
config:
project: ${gitlab_project}
- provider: jenkins
kind: jenkins_credential
config:
credential_id: slack-webhook
description: Slack Notification Webhook
# Cross-provider automation identity: GitLab group SA → GitLab group var + ADO library
- name: shared_deploy_token
kind: gitlab_group_service_account
config:
provider: gitlab
group: auto
service_account_name: secretzero-deploy-bot
token_name: deploy-token
scopes: [read_repository, write_repository]
memberships:
- resource_type: project
resource: auto
access_level: 30
targets:
- provider: gitlab
kind: gitlab_group_variable
config:
group: auto
masked: true
value_field: token
- provider: azdo
kind: azdo_variable_group
config:
project: auto
variable_group: production-secrets
variable_name: GITLAB_DEPLOY_TOKEN
is_secret: true
create_if_missing: true
value_field: token
authorize_pipelines: [backend-release]
# ```
# ## Setup
# 1. **Install SecretZero with all CI/CD providers**:
# ```bash
# pip install secretzero[cicd]
# ```
# 2. **Set up authentication for all platforms**:
# ```bash
# # GitHub
# export GITHUB_TOKEN=ghp_your_github_token
# # GitLab
# export GITLAB_TOKEN=glpat-your_gitlab_token
#
# # Azure DevOps Services
# export AZDO_PAT=your_azure_devops_pat
# # Jenkins
# export JENKINS_USERNAME=admin
# export JENKINS_TOKEN=your_jenkins_token
# # External service credentials
# export DOCKER_USERNAME=myuser
# export DOCKER_PASSWORD=mypassword
# export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
# export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
# export SLACK_WEBHOOK_URL=https://hooks.slack.com/services/xxx/yyy/zzz
# ```
# 3. **Test all provider connections**:
# ```bash
# secretzero test -f examples/multi-cicd.yml
# ```
# 4. **Perform a dry-run**:
# ```bash
# secretzero sync -f examples/multi-cicd.yml --dry-run
# ```
# 5. **Sync secrets to all platforms**:
# ```bash
# secretzero sync -f examples/multi-cicd.yml
# ```
# ## Usage in Different Platforms
# ### GitHub Actions Workflow
# ```yaml
# name: Deploy
# on:
# push:
# branches: [main]
# jobs:
# deploy:
# runs-on: ubuntu-latest
# environment: production
# steps:
# - uses: actions/checkout@v3
# - name: Login to Docker
# run: echo "${{ secrets.DOCKER_REGISTRY_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
# - name: Deploy to AWS
# env:
# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY }}
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_KEY }}
# run: ./deploy-aws.sh
# - name: Notify Slack
# run: |
# curl -X POST ${{ secrets.SLACK_WEBHOOK }} \
# -H 'Content-Type: application/json' \
# -d '{"text": "Deployment completed"}'
# ```
# ### GitLab CI/CD Pipeline
# ```yaml
# stages:
# - build
# - deploy
# deploy:
# stage: deploy
# environment:
# name: production
# script:
# - echo "$DOCKER_REGISTRY_PASSWORD" | docker login -u "$DOCKER_USERNAME" --password-stdin
# - export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY
# - export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_KEY
# - ./deploy-aws.sh
# - 'curl -X POST $SLACK_WEBHOOK -H "Content-Type: application/json" -d "{\"text\": \"Deployment completed\"}"'
# only:
# - main
# ```
# ### Jenkins Pipeline
# ```groovy
# pipeline {
# agent any
# stages {
# stage('Deploy') {
# steps {
# withCredentials([
# usernamePassword(
# credentialsId: 'docker-registry',
# usernameVariable: 'DOCKER_USER',
# passwordVariable: 'DOCKER_PASS'
# ),
# string(credentialsId: 'aws-access-key', variable: 'AWS_ACCESS_KEY_ID'),
# string(credentialsId: 'aws-secret-key', variable: 'AWS_SECRET_ACCESS_KEY'),
# string(credentialsId: 'slack-webhook', variable: 'SLACK_WEBHOOK')
# ]) {
# sh '''
# echo "$DOCKER_PASS" | docker login -u "$DOCKER_USER" --password-stdin
# ./deploy-aws.sh
# curl -X POST $SLACK_WEBHOOK -H "Content-Type: application/json" -d '{"text": "Deployment completed"}'
# '''
# }
# }
# }
# }
# }
# ```
# ## Benefits
# ### Single Source of Truth
# - All secrets defined in one configuration file
# - Consistent naming across platforms
# - Centralized secret management
# ### Automated Synchronization
# - Update secrets in all platforms with one command
# - Ensure consistency across CI/CD systems
# - Reduce manual secret management errors
# ### Audit Trail
# - Lockfile tracks all secret changes
# - Know when secrets were created and last updated
# - Compliance-friendly documentation
# ### Easy Rotation
# - Rotate secrets across all platforms simultaneously
# - No need to manually update each platform
# - Automated rotation policies
# ## Secret Lifecycle
# ### Initial Bootstrap
# ```bash
# # First time setup - generates and distributes all secrets
# secretzero sync -f examples/multi-cicd.yml
# ```
# ### Secret Rotation
# ```bash
# # After 90 days (or based on rotation_period)
# secretzero sync -f examples/multi-cicd.yml --force-rotate
# ```
# ### Adding New Platform
# ```yaml
# # Add a new target to existing secret
# targets:
# - provider: new_provider
# kind: new_target_type
# config:
# ...
# ```
# ```bash
# # Sync to distribute to new platform
# secretzero sync -f examples/multi-cicd.yml
# ```
# ## Migration Strategy
# ### Migrating from Manual Secret Management
# 1. **Inventory existing secrets** in each platform
# 2. **Create Secretfile.yml** with all secrets
# 3. **Use `one_time: true`** for secrets that shouldn't change
# 4. **Run dry-run** to verify what will be updated
# 5. **Sync in stages** (one platform at a time if preferred)
# 6. **Verify** in each platform's UI
# 7. **Update** pipelines to use new secret names if needed
# ### Example Migration
# ```yaml
# secrets:
# # Existing secret - preserve current value
# - name: legacy_api_key
# kind: static
# config:
# default: ${LEGACY_API_KEY} # Read from environment
# one_time: true # Don't regenerate
# targets:
# # Add to all platforms
# ```
# ## Best Practices
# 1. **Use descriptive names** that work across all platforms
# 2. **Set `one_time: true`** for external service credentials
# 3. **Use `rotation_period`** for generated secrets
# 4. **Test connectivity** before syncing
# 5. **Use dry-run** before making changes
# 6. **Commit Secretfile.yml** to version control
# 7. **Never commit** the lockfile or authentication tokens
# 8. **Use environment-specific** secrets where appropriate
# 9. **Document** secret purposes in descriptions
# 10. **Rotate regularly** according to security policies
# ## Troubleshooting
# ### Secret Mismatch
# If secrets differ between platforms:
# ```bash
# # Force resync to all platforms
# secretzero sync -f examples/multi-cicd.yml --force
# ```
# ### Platform-Specific Issues
# Test each provider independently:
# ```bash
# secretzero test -f examples/multi-cicd.yml
# ```
# ### Partial Sync Failure
# If one platform fails, others may succeed. Check the output for errors and retry the failed platform.
# ## Security Considerations
# 1. **Token Storage**: Store authentication tokens securely (1Password, HashiCorp Vault, etc.)
# 2. **Token Rotation**: Rotate platform tokens regularly
# 3. **Access Control**: Limit who can run `secretzero sync`
# 4. **Audit Logs**: Review platform audit logs for unexpected changes
# 5. **Encryption**: All secrets are encrypted in transit and at rest by the platforms
# 6. **Principle of Least Privilege**: Grant minimum required permissions to service accounts
# ## Cost Optimization
# - GitHub / GitLab / Jenkins: see each platform’s current documentation for limits and features.
# - SecretZero: open source; vendor fees apply only to the underlying platforms you use.