-
Notifications
You must be signed in to change notification settings - Fork 0
388 lines (348 loc) · 14.3 KB
/
docker.yml
File metadata and controls
388 lines (348 loc) · 14.3 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
# =============================================================================
# TelemetryFlow Go SDK - Docker Image Builder Workflow
# =============================================================================
#
# TelemetryFlow Go SDK - Community Enterprise Observability Platform (CEOP)
# Copyright (c) 2024-2026 DevOpsCorner Indonesia. All rights reserved.
#
# This workflow builds and publishes Docker images for TelemetryFlow SDK tools:
# - Multi-platform support: linux/amd64, linux/arm64
# - Semantic versioning tags
# - Docker Hub
#
# Triggers:
# - Push tags matching v*.*.*
# - Push to main/master branch (latest tag)
# - Manual workflow dispatch
#
# =============================================================================
name: Docker Build - TFO Go SDK
on:
push:
branches:
- main
- master
tags:
- 'v*.*.*'
paths:
- 'Dockerfile*'
- 'cmd/**'
- 'internal/**'
- 'pkg/**'
- 'go.mod'
- 'go.sum'
- '.github/workflows/docker.yml'
pull_request:
branches:
- main
- master
paths:
- 'Dockerfile*'
- 'cmd/**'
- 'internal/**'
- 'pkg/**'
- 'go.mod'
- 'go.sum'
workflow_dispatch:
inputs:
version:
description: 'Version tag (e.g., 1.0.0)'
required: false
default: ''
push:
description: 'Push images to registry'
required: false
type: boolean
default: true
platforms:
description: 'Target platforms'
required: false
default: 'linux/amd64,linux/arm64'
env:
REGISTRY_DOCKER: docker.io
IMAGE_NAME: telemetryflow/telemetryflow-go-sdk
PRODUCT_NAME: TelemetryFlow Go SDK
permissions:
contents: read
packages: write
id-token: write
security-events: write
jobs:
# ===========================================================================
# Prepare Build Context
# ===========================================================================
prepare:
name: Prepare Build
runs-on: ubuntu-latest
outputs:
version: ${{ steps.meta.outputs.version }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
commit: ${{ steps.info.outputs.commit }}
branch: ${{ steps.info.outputs.branch }}
build_time: ${{ steps.info.outputs.build_time }}
go_version: ${{ steps.info.outputs.go_version }}
push: ${{ steps.check.outputs.push }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go (for version info)
uses: actions/setup-go@v5
with:
go-version: '1.24'
cache: true
- name: Get build info
id: info
run: |
echo "commit=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
echo "branch=$(git rev-parse --abbrev-ref HEAD)" >> $GITHUB_OUTPUT
echo "build_time=$(date -u '+%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
# Get Go version from Makefile
echo "go_version=$(go version | cut -d ' ' -f 3)" >> $GITHUB_OUTPUT
- name: Check if should push
id: check
run: |
if [ "${{ github.event_name }}" = "pull_request" ]; then
echo "push=false" >> $GITHUB_OUTPUT
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "push=${{ github.event.inputs.push }}" >> $GITHUB_OUTPUT
else
echo "push=true" >> $GITHUB_OUTPUT
fi
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ env.REGISTRY_DOCKER }}/${{ env.IMAGE_NAME }}
flavor: |
latest=false
tags: |
# Semantic versioning from tags
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
# Manual version input
type=raw,value=${{ github.event.inputs.version }},enable=${{ github.event.inputs.version != '' }}
# Branch name for non-tag pushes
type=ref,event=branch
# PR number
type=ref,event=pr
# Latest for default branch
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') || github.ref == format('refs/heads/{0}', 'master') }}
# Git SHA
type=sha,prefix=sha-,format=short
labels: |
org.opencontainers.image.title=${{ env.PRODUCT_NAME }}
org.opencontainers.image.description=Go SDK and code generators for TelemetryFlow integration
org.opencontainers.image.vendor=TelemetryFlow
io.telemetryflow.product=${{ env.PRODUCT_NAME }}
io.telemetryflow.component=sdk
io.telemetryflow.platform=CEOP
# ===========================================================================
# Build and Push Docker Image
# ===========================================================================
build:
name: Build & Push
runs-on: ubuntu-latest
needs: prepare
steps:
- name: Free up disk space
run: |
echo "=== Disk space before cleanup ==="
df -h
# Remove unnecessary tools and SDKs
sudo rm -rf /usr/share/dotnet
sudo rm -rf /usr/local/lib/android
sudo rm -rf /opt/ghc
sudo rm -rf /opt/hostedtoolcache/CodeQL
sudo rm -rf /usr/local/share/boost
sudo rm -rf /usr/share/swift
sudo rm -rf "$AGENT_TOOLSDIRECTORY"
# Clean apt cache
sudo apt-get clean
# Remove Docker images we don't need
docker system prune -af --volumes || true
echo "=== Disk space after cleanup ==="
df -h
- name: Checkout code
uses: actions/checkout@v4
- name: Create Dockerfile
run: |
cat > Dockerfile << 'EOF'
# =============================================================================
# TelemetryFlow Go SDK - Multi-stage Dockerfile
# =============================================================================
FROM golang:1.24-alpine AS builder
ARG VERSION=dev
ARG GIT_COMMIT=unknown
ARG GIT_BRANCH=unknown
ARG BUILD_TIME=unknown
WORKDIR /build
# Install build dependencies
RUN apk add --no-cache git make
# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download
# Copy source
COPY . .
# Build generators
RUN CGO_ENABLED=0 go build \
-ldflags "-s -w \
-X github.com/telemetryflow/telemetryflow-go-sdk/internal/version.Version=${VERSION} \
-X github.com/telemetryflow/telemetryflow-go-sdk/internal/version.GitCommit=${GIT_COMMIT} \
-X github.com/telemetryflow/telemetryflow-go-sdk/internal/version.GitBranch=${GIT_BRANCH} \
-X github.com/telemetryflow/telemetryflow-go-sdk/internal/version.BuildTime=${BUILD_TIME}" \
-o /bin/telemetryflow-gen ./cmd/generator
RUN CGO_ENABLED=0 go build \
-ldflags "-s -w \
-X github.com/telemetryflow/telemetryflow-go-sdk/internal/version.Version=${VERSION} \
-X github.com/telemetryflow/telemetryflow-go-sdk/internal/version.GitCommit=${GIT_COMMIT} \
-X github.com/telemetryflow/telemetryflow-go-sdk/internal/version.GitBranch=${GIT_BRANCH} \
-X github.com/telemetryflow/telemetryflow-go-sdk/internal/version.BuildTime=${BUILD_TIME}" \
-o /bin/telemetryflow-restapi ./cmd/generator-restfulapi
# =============================================================================
# Final image
# =============================================================================
FROM alpine:3.21
# Update packages to get security patches (CVE fixes)
RUN apk upgrade --no-cache && \
apk add --no-cache ca-certificates tzdata
COPY --from=builder /bin/telemetryflow-gen /usr/local/bin/
COPY --from=builder /bin/telemetryflow-restapi /usr/local/bin/
# Labels
LABEL org.opencontainers.image.title="TelemetryFlow Go SDK"
LABEL org.opencontainers.image.description="Go SDK and code generators for TelemetryFlow"
LABEL org.opencontainers.image.vendor="TelemetryFlow"
LABEL org.opencontainers.image.licenses="Apache-2.0"
WORKDIR /workspace
ENTRYPOINT ["/usr/local/bin/telemetryflow-gen"]
CMD ["--help"]
EOF
- name: Set up QEMU
uses: docker/setup-qemu-action@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to Docker Hub
if: needs.prepare.outputs.push == 'true' && vars.DOCKERHUB_USERNAME != ''
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY_DOCKER }}
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Determine version
id: version
run: |
if [ "${{ github.event.inputs.version }}" != "" ]; then
VERSION="${{ github.event.inputs.version }}"
elif [[ "${{ github.ref }}" == refs/tags/v* ]]; then
VERSION="${GITHUB_REF#refs/tags/v}"
else
VERSION="${{ needs.prepare.outputs.commit }}"
fi
echo "version=${VERSION}" >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: ${{ github.event.inputs.platforms || 'linux/amd64,linux/arm64' }}
push: ${{ needs.prepare.outputs.push == 'true' }}
tags: ${{ needs.prepare.outputs.tags }}
labels: ${{ needs.prepare.outputs.labels }}
build-args: |
VERSION=${{ steps.version.outputs.version }}
GIT_COMMIT=${{ needs.prepare.outputs.commit }}
GIT_BRANCH=${{ needs.prepare.outputs.branch }}
BUILD_TIME=${{ needs.prepare.outputs.build_time }}
cache-from: type=gha
cache-to: type=gha,mode=max
provenance: true
sbom: true
- name: Generate SBOM
if: needs.prepare.outputs.push == 'true'
uses: anchore/sbom-action@v0
with:
image: ${{ env.REGISTRY_DOCKER }}/${{ env.IMAGE_NAME }}:sha-${{ needs.prepare.outputs.commit }}
format: spdx-json
output-file: sbom-${{ steps.version.outputs.version }}.spdx.json
upload-release-assets: false
- name: Upload SBOM
if: needs.prepare.outputs.push == 'true'
uses: actions/upload-artifact@v4
with:
name: sbom-${{ needs.prepare.outputs.commit }}
path: sbom-*.spdx.json
retention-days: 90
# ===========================================================================
# Security Scan
# ===========================================================================
scan:
name: Security Scan
runs-on: ubuntu-latest
needs: [prepare, build]
if: needs.prepare.outputs.push == 'true' && vars.DOCKERHUB_USERNAME != ''
steps:
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY_DOCKER }}
username: ${{ vars.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.REGISTRY_DOCKER }}/${{ env.IMAGE_NAME }}:sha-${{ needs.prepare.outputs.commit }}
format: 'sarif'
output: 'trivy-results.sarif'
severity: 'CRITICAL,HIGH'
- name: Upload Trivy scan results
uses: github/codeql-action/upload-sarif@v4
if: always()
with:
sarif_file: 'trivy-results.sarif'
# ===========================================================================
# Summary
# ===========================================================================
summary:
name: Build Summary
runs-on: ubuntu-latest
needs: [prepare, build]
if: always()
steps:
- name: Summary
run: |
echo "## Docker Build Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "| Item | Value |" >> $GITHUB_STEP_SUMMARY
echo "|------|-------|" >> $GITHUB_STEP_SUMMARY
echo "| **Product** | ${{ env.PRODUCT_NAME }} |" >> $GITHUB_STEP_SUMMARY
echo "| **Go Version** | ${{ needs.prepare.outputs.go_version }} |" >> $GITHUB_STEP_SUMMARY
echo "| **Commit** | ${{ needs.prepare.outputs.commit }} |" >> $GITHUB_STEP_SUMMARY
echo "| **Branch** | ${{ needs.prepare.outputs.branch }} |" >> $GITHUB_STEP_SUMMARY
echo "| **Build Time** | ${{ needs.prepare.outputs.build_time }} |" >> $GITHUB_STEP_SUMMARY
echo "| **Push** | ${{ needs.prepare.outputs.push }} |" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Tags" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "${{ needs.prepare.outputs.tags }}" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Pull Commands" >> $GITHUB_STEP_SUMMARY
echo '```bash' >> $GITHUB_STEP_SUMMARY
echo "docker pull ${{ env.IMAGE_NAME }}:latest" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "### Usage" >> $GITHUB_STEP_SUMMARY
echo '```bash' >> $GITHUB_STEP_SUMMARY
echo "# Generate example project" >> $GITHUB_STEP_SUMMARY
echo "docker run --rm -v \$(pwd):/workspace \\" >> $GITHUB_STEP_SUMMARY
echo " ${{ env.IMAGE_NAME }}:latest example --output /workspace/my-project" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
echo "# Generate RESTful API project" >> $GITHUB_STEP_SUMMARY
echo "docker run --rm -v \$(pwd):/workspace \\" >> $GITHUB_STEP_SUMMARY
echo " --entrypoint telemetryflow-restapi \\" >> $GITHUB_STEP_SUMMARY
echo " ${{ env.IMAGE_NAME }}:latest init --name my-api --output /workspace/my-api" >> $GITHUB_STEP_SUMMARY
echo '```' >> $GITHUB_STEP_SUMMARY