1
---
2
name: github-actions-templates
3
description: Create production-ready GitHub Actions workflows for automated testing, building, and deploying applications. Use when setting up CI/CD with GitHub Actions, automating development workflows, or creating reusable workflow templates.
4
---
5
6
# GitHub Actions Templates
7
8
Production-ready GitHub Actions workflow patterns for testing, building, and deploying applications.
9
10
## Purpose
11
12
Create efficient, secure GitHub Actions workflows for continuous integration and deployment across various tech stacks.
13
14
## When to Use
15
16
- Automate testing and deployment
17
- Build Docker images and push to registries
18
- Deploy to Kubernetes clusters
19
- Run security scans
20
- Implement matrix builds for multiple environments
21
22
## Common Workflow Patterns
23
24
### Pattern 1: Test Workflow
25
26
```yaml
27
name: Test
28
29
on:
30
push:
31
branches: [main, develop]
32
pull_request:
33
branches: [main]
34
35
jobs:
36
test:
37
runs-on: ubuntu-latest
38
39
strategy:
40
matrix:
41
node-version: [18.x, 20.x]
42
43
steps:
44
- uses: actions/checkout@v4
45
46
- name: Use Node.js ${{ matrix.node-version }}
47
uses: actions/setup-node@v4
48
with:
49
node-version: ${{ matrix.node-version }}
50
cache: "npm"
51
52
- name: Install dependencies
53
run: npm ci
54
55
- name: Run linter
56
run: npm run lint
57
58
- name: Run tests
59
run: npm test
60
61
- name: Upload coverage
62
uses: codecov/codecov-action@v4
63
with:
64
files: ./coverage/lcov.info
65
```
66
67
### Pattern 2: Build and Push Docker Image
68
69
```yaml
70
name: Build and Push
71
72
on:
73
push:
74
branches: [main]
75
tags: ["v*"]
76
77
env:
78
REGISTRY: ghcr.io
79
IMAGE_NAME: ${{ github.repository }}
80
81
jobs:
82
build:
83
runs-on: ubuntu-latest
84
permissions:
85
contents: read
86
packages: write
87
88
steps:
89
- uses: actions/checkout@v4
90
91
- name: Log in to Container Registry
92
uses: docker/login-action@v3
93
with:
94
registry: ${{ env.REGISTRY }}
95
username: ${{ github.actor }}
96
password: ${{ secrets.GITHUB_TOKEN }}
97
98
- name: Extract metadata
99
id: meta
100
uses: docker/metadata-action@v5
101
with:
102
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
103
tags: |
104
type=ref,event=branch
105
type=ref,event=pr
106
type=semver,pattern={{version}}
107
type=semver,pattern={{major}}.{{minor}}
108
109
- name: Build and push
110
uses: docker/build-push-action@v5
111
with:
112
context: .
113
push: true
114
tags: ${{ steps.meta.outputs.tags }}
115
labels: ${{ steps.meta.outputs.labels }}
116
cache-from: type=gha
117
cache-to: type=gha,mode=max
118
```
119
120
### Pattern 3: Deploy to Kubernetes
121
122
```yaml
123
name: Deploy to Kubernetes
124
125
on:
126
push:
127
branches: [main]
128
129
jobs:
130
deploy:
131
runs-on: ubuntu-latest
132
133
steps:
134
- uses: actions/checkout@v4
135
136
- name: Configure AWS credentials
137
uses: aws-actions/configure-aws-credentials@v4
138
with:
139
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
140
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
141
aws-region: us-west-2
142
143
- name: Update kubeconfig
144
run: |
145
aws eks update-kubeconfig --name production-cluster --region us-west-2
146
147
- name: Deploy to Kubernetes
148
run: |
149
kubectl apply -f k8s/
150
kubectl rollout status deployment/my-app -n production
151
kubectl get services -n production
152
153
- name: Verify deployment
154
run: |
155
kubectl get pods -n production
156
kubectl describe deployment my-app -n production
157
```
158
159
### Pattern 4: Matrix Build
160
161
```yaml
162
name: Matrix Build
163
164
on: [push, pull_request]
165
166
jobs:
167
build:
168
runs-on: ${{ matrix.os }}
169
170
strategy:
171
matrix:
172
os: [ubuntu-latest, macos-latest, windows-latest]
173
python-version: ["3.9", "3.10", "3.11", "3.12"]
174
175
steps:
176
- uses: actions/checkout@v4
177
178
- name: Set up Python
179
uses: actions/setup-python@v5
180
with:
181
python-version: ${{ matrix.python-version }}
182
183
- name: Install dependencies
184
run: |
185
python -m pip install --upgrade pip
186
pip install -r requirements.txt
187
188
- name: Run tests
189
run: pytest
190
```
191
192
## Workflow Best Practices
193
194
1. **Use specific action versions** (@v4, not @latest)
195
2. **Cache dependencies** to speed up builds
196
3. **Use secrets** for sensitive data
197
4. **Implement status checks** on PRs
198
5. **Use matrix builds** for multi-version testing
199
6. **Set appropriate permissions**
200
7. **Use reusable workflows** for common patterns
201
8. **Implement approval gates** for production
202
9. **Add notification steps** for failures
203
10. **Use self-hosted runners** for sensitive workloads
204
205
## Reusable Workflows
206
207
```yaml
208
# .github/workflows/reusable-test.yml
209
name: Reusable Test Workflow
210
211
on:
212
workflow_call:
213
inputs:
214
node-version:
215
required: true
216
type: string
217
secrets:
218
NPM_TOKEN:
219
required: true
220
221
jobs:
222
test:
223
runs-on: ubuntu-latest
224
steps:
225
- uses: actions/checkout@v4
226
- uses: actions/setup-node@v4
227
with:
228
node-version: ${{ inputs.node-version }}
229
- run: npm ci
230
- run: npm test
231
```
232
233
**Use reusable workflow:**
234
235
```yaml
236
jobs:
237
call-test:
238
uses: ./.github/workflows/reusable-test.yml
239
with:
240
node-version: "20.x"
241
secrets:
242
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
243
```
244
245
## Security Scanning
246
247
```yaml
248
name: Security Scan
249
250
on:
251
push:
252
branches: [main]
253
pull_request:
254
branches: [main]
255
256
jobs:
257
security:
258
runs-on: ubuntu-latest
259
260
steps:
261
- uses: actions/checkout@v4
262
263
- name: Run Trivy vulnerability scanner
264
uses: aquasecurity/trivy-action@0.28.0
265
with:
266
scan-type: "fs"
267
scan-ref: "."
268
format: "sarif"
269
output: "trivy-results.sarif"
270
271
- name: Upload Trivy results to GitHub Security
272
uses: github/codeql-action/upload-sarif@v3
273
with:
274
sarif_file: "trivy-results.sarif"
275
276
- name: Run Snyk Security Scan
277
uses: snyk/actions/node@0.4.0
278
env:
279
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
280
```
281
282
## Deployment with Approvals
283
284
```yaml
285
name: Deploy to Production
286
287
on:
288
push:
289
tags: ["v*"]
290
291
jobs:
292
deploy:
293
runs-on: ubuntu-latest
294
environment:
295
name: production
296
url: https://app.example.com
297
298
steps:
299
- uses: actions/checkout@v4
300
301
- name: Deploy application
302
run: |
303
echo "Deploying to production..."
304
# Deployment commands here
305
306
- name: Notify Slack
307
if: success()
308
uses: slackapi/slack-github-action@v1
309
with:
310
webhook-url: ${{ secrets.SLACK_WEBHOOK }}
311
payload: |
312
{
313
"text": "Deployment to production completed successfully!"
314
}
315
```
316
317
318
## Related Skills
319
320
- `gitlab-ci-patterns` - For GitLab CI workflows
321
- `deployment-pipeline-design` - For pipeline architecture
322
- `secrets-management` - For secrets handling
323