-
Notifications
You must be signed in to change notification settings - Fork 103
357 lines (304 loc) · 11.8 KB
/
Copy pathci.yml
File metadata and controls
357 lines (304 loc) · 11.8 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
name: CI
on:
pull_request:
branches:
- '**'
push:
branches:
- main
permissions:
contents: write # Needed to commit coverage badge on main branch
pull-requests: write
security-events: write # Needed for Datadog SAST results
id-token: write # Needed to federate STS token to upload coverage
env:
DD_ENV: ci
DD_SERVICE: pup
jobs:
test:
name: Test and Coverage
runs-on: ubuntu-latest
env:
DD_API_KEY: ${{ secrets.DD_API_KEY }}
DD_SITE: ${{ secrets.DD_SITE || 'datadoghq.com' }}
DD_CIVISIBILITY_AGENTLESS_ENABLED: true
DD_CIVISIBILITY_GIT_UPLOAD_ENABLED: true
DD_CIVISIBILITY_ENABLED: true
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0 # Fetch full git history for Datadog CI Visibility
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: '1.25'
cache: true
- name: Get Datadog credentials
id: dd-sts # Needed to be able to reference this step's output later
uses: DataDog/dd-sts-action@main # Pin to the main branch to get auto updates for free, or to a specific commit hash if you want stability
with:
policy: public-datadog-pup-sts
- name: Configure Datadog Test Optimization
uses: datadog/test-visibility-github-action@v2
with:
languages: go
api_key: ${{ steps.dd-sts.outputs.api_key }}
site: datadoghq.com
- name: Install Datadog CI tools
run: |
# Install orchestrion for Go test instrumentation
go install github.com/DataDog/orchestrion@latest
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
# Install datadog-ci CLI for coverage upload
npm install -g @datadog/datadog-ci
- name: Install bc for floating-point math
run: sudo apt-get update && sudo apt-get install -y bc
- name: Run tests with coverage
env:
DD_CIVISIBILITY_ENABLED: true
DD_ENV: ci
run: |
# Run tests on all packages with race detection
# Use orchestrion to instrument tests for Datadog CI Visibility
# NOTE: Do NOT use -parallel N with orchestrion — it auto-injects t.Parallel()
# into subtests, which deadlocks table-driven tests when the parallel slot limit
# is lower than the number of subtests.
if [ -n "$DD_API_KEY" ]; then
echo "Running tests with Datadog CI Visibility enabled"
orchestrion go test -v -race ./...
# Calculate coverage with orchestrion instrumentation
# Use -count=1 to disable test caching and get accurate coverage
orchestrion go test -count=1 -coverprofile=coverage.out -covermode=atomic ./pkg/...
else
echo "Running tests without Datadog CI Visibility (DD_API_KEY not set)"
go test -v -race ./...
go test -count=1 -coverprofile=coverage.out -covermode=atomic ./pkg/...
fi
# Generate HTML coverage report
go tool cover -html=coverage.out -o coverage.html
- name: Calculate coverage
id: coverage
run: |
# Calculate total coverage
COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//')
echo "coverage=$COVERAGE" >> $GITHUB_OUTPUT
echo "Total coverage: $COVERAGE%"
# Calculate coverage by package
echo "## Coverage by Package" > coverage_report.txt
echo "" >> coverage_report.txt
go tool cover -func=coverage.out | grep -v "total:" | awk '{print $1, $3}' | sort -t: -k1,1 -u | while read line; do
echo "- $line" >> coverage_report.txt
done
# Get coverage summary
echo "" >> coverage_report.txt
echo "## Summary" >> coverage_report.txt
echo "" >> coverage_report.txt
go tool cover -func=coverage.out | tail -1 >> coverage_report.txt
- name: Upload coverage to Datadog
if: env.DD_API_KEY != ''
env:
DATADOG_API_KEY: ${{ steps.dd-sts.outputs.api_key }}
DD_SITE: datadoghq.com
run: |
# Upload coverage reports to Datadog
datadog-ci coverage upload --format=go-cover coverage.out
- name: Check coverage threshold
run: |
COVERAGE=${{ steps.coverage.outputs.coverage }}
# Lower threshold to account for skipped keychain tests in CI (headless environment)
# Local macOS coverage is ~87%, CI is ~77% due to keychain tests being skipped
THRESHOLD=75.0
echo "Coverage: $COVERAGE%"
echo "Threshold: $THRESHOLD%"
# Use bc for floating point comparison
if [ $(echo "$COVERAGE < $THRESHOLD" | bc -l) -eq 1 ]; then
echo "❌ Coverage $COVERAGE% is below threshold $THRESHOLD%"
exit 1
else
echo "✅ Coverage $COVERAGE% meets threshold $THRESHOLD%"
fi
- name: Generate coverage badge data
if: github.event_name == 'pull_request'
id: badge
run: |
COVERAGE=${{ steps.coverage.outputs.coverage }}
# Determine badge color based on coverage
if [ $(echo "$COVERAGE >= 90" | bc -l) -eq 1 ]; then
COLOR="brightgreen"
elif [ $(echo "$COVERAGE >= 80" | bc -l) -eq 1 ]; then
COLOR="green"
elif [ $(echo "$COVERAGE >= 70" | bc -l) -eq 1 ]; then
COLOR="yellow"
elif [ $(echo "$COVERAGE >= 60" | bc -l) -eq 1 ]; then
COLOR="orange"
else
COLOR="red"
fi
echo "color=$COLOR" >> $GITHUB_OUTPUT
- name: Generate PR comment body
if: github.event_name == 'pull_request'
id: comment
env:
COVERAGE: ${{ steps.coverage.outputs.coverage }}
BADGE_COLOR: ${{ steps.badge.outputs.color }}
COMMIT_SHA: ${{ github.event.pull_request.head.sha }}
run: |
# Determine status
if [ $(echo "$COVERAGE >= 80" | bc -l) -eq 1 ]; then
STATUS="✅ PASSED - Coverage meets minimum threshold"
STATUS_EMOJI="✅"
else
STATUS="❌ FAILED - Coverage below minimum threshold"
STATUS_EMOJI="❌"
fi
# Create comment body using heredoc
cat > comment_final.txt << EOF
## 📊 Test Coverage Report
**Overall Coverage:** ${COVERAGE}% 
**Threshold:** 80% ${STATUS_EMOJI}
<details>
<summary>Coverage by Package</summary>
\`\`\`
$(cat coverage_report.txt)
\`\`\`
</details>
---
📈 **Coverage Status:** ${STATUS}
<sub>Updated for commit ${COMMIT_SHA}</sub>
EOF
- name: Comment on PR
if: github.event_name == 'pull_request'
continue-on-error: true # Don't fail CI if comment posting fails
uses: actions/github-script@v8
env:
COMMENT_BODY: ${{ steps.comment.outputs.comment_body }}
with:
script: |
const fs = require('fs');
const commentBody = fs.readFileSync('comment_final.txt', 'utf8');
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const botComment = comments.find(comment =>
comment.user.type === 'Bot' && comment.body.includes('📊 Test Coverage Report')
);
if (botComment) {
// Update existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body: commentBody
});
} else {
// Create new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: commentBody
});
}
- name: Generate coverage badge for main branch
if: github.ref == 'refs/heads/main' && github.event_name == 'push'
env:
COVERAGE: ${{ steps.coverage.outputs.coverage }}
run: |
# Determine badge color
if [ $(echo "$COVERAGE >= 90" | bc -l) -eq 1 ]; then
COLOR="brightgreen"
elif [ $(echo "$COVERAGE >= 80" | bc -l) -eq 1 ]; then
COLOR="green"
elif [ $(echo "$COVERAGE >= 70" | bc -l) -eq 1 ]; then
COLOR="yellow"
elif [ $(echo "$COVERAGE >= 60" | bc -l) -eq 1 ]; then
COLOR="orange"
else
COLOR="red"
fi
# Create badge JSON for shields.io endpoint schema
mkdir -p .github/badges
cat > .github/badges/coverage.json << EOF
{
"schemaVersion": 1,
"label": "coverage",
"message": "${COVERAGE}%",
"color": "${COLOR}"
}
EOF
echo "Generated coverage badge: ${COVERAGE}% (${COLOR})"
# Note: Auto-committing badge disabled due to branch protection requiring signed commits
# Badge can be updated manually or via a separate workflow with appropriate permissions
- name: Upload coverage artifacts
uses: actions/upload-artifact@v6
with:
name: coverage-report
path: |
coverage.out
coverage.html
coverage_report.txt
retention-days: 30
lint:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: '1.25'
cache: true
- name: Install golangci-lint
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin latest
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Run golangci-lint
run: golangci-lint run --timeout=5m
build:
name: Build
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v6
- name: Set up Go
uses: actions/setup-go@v6
with:
go-version: '1.25'
cache: true
- name: Build
run: go build -v ./...
- name: Build CLI binary
run: go build -o pup .
- name: Verify binary
run: ./pup --version
sast:
name: Datadog Static Analysis
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
env:
DD_API_KEY: ${{ secrets.DD_API_KEY }}
DD_APP_KEY: ${{ secrets.DD_APP_KEY }}
DD_SITE: ${{ secrets.DD_SITE || 'datadoghq.com' }}
steps:
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0 # Full history for better SAST analysis
- name: Run Datadog Static Analysis
if: env.DD_API_KEY != ''
run: |
# Install datadog-ci if not already installed
npm install -g @datadog/datadog-ci
# Run static analysis
# This will analyze the code for security vulnerabilities, code quality issues, etc.
datadog-ci sast scan --service=${{ env.DD_SERVICE }} --env=${{ env.DD_ENV }}
- name: SAST disabled notice
if: env.DD_API_KEY == ''
run: |
echo "⚠️ Datadog Static Analysis skipped: DD_API_KEY not configured"
echo "To enable SAST, add DD_API_KEY and DD_APP_KEY as repository secrets"