|
| 1 | +# Lab 3 Bonus — Go CI/CD Pipeline |
| 2 | + |
| 3 | +## 1. Overview |
| 4 | + |
| 5 | +### Testing Framework |
| 6 | +**Framework:** Go's built-in `testing` package |
| 7 | +**Why built-in testing?** |
| 8 | +- No external dependencies required |
| 9 | +- Standard in the Go ecosystem |
| 10 | +- Simple, fast, and well-documented |
| 11 | +- Integrated with `go test` command |
| 12 | +- IDE support out of the box |
| 13 | + |
| 14 | +### Test Coverage |
| 15 | +**Endpoints Tested:** |
| 16 | +- `GET /` — Test cases covering: |
| 17 | + - HTTP 200 status code |
| 18 | + - Valid JSON response structure |
| 19 | + - Service information fields |
| 20 | + - System information fields |
| 21 | + - Runtime information fields |
| 22 | + - Request information fields |
| 23 | + |
| 24 | +- `GET /health` — Test cases covering: |
| 25 | + - HTTP 200 status code |
| 26 | + - Valid JSON response |
| 27 | + - Status field ("healthy") |
| 28 | + - Timestamp and uptime fields |
| 29 | + |
| 30 | +**Total:** 9 test functions in `main_test.go` |
| 31 | + |
| 32 | +### CI Workflow Configuration |
| 33 | +**Trigger Strategy:** |
| 34 | +```yaml |
| 35 | +on: |
| 36 | + push: |
| 37 | + branches: [ master, lab03 ] |
| 38 | + paths: |
| 39 | + - 'app_go/**' |
| 40 | + - '.github/workflows/go-ci.yml' |
| 41 | + pull_request: |
| 42 | + branches: [ master ] |
| 43 | + paths: |
| 44 | + - 'app_go/**' |
| 45 | +``` |
| 46 | +
|
| 47 | +**Rationale:** |
| 48 | +- **Path filters** ensure workflow only runs when Go app changes (not for Python) |
| 49 | +- **Independent from Python CI** — both can run in parallel |
| 50 | +- **Monorepo efficiency** — don't waste CI minutes on unrelated changes |
| 51 | +
|
| 52 | +### Versioning Strategy |
| 53 | +**Strategy:** Calendar Versioning (CalVer) with SHA suffix |
| 54 | +**Format:** `YYYY.MM.DD-<short-sha>` |
| 55 | + |
| 56 | +**Example Tags:** |
| 57 | +- `3llimi/devops-info-service-go:latest` |
| 58 | +- `3llimi/devops-info-service-go:2026.02.11-c30868b` |
| 59 | + |
| 60 | +**Rationale:** Same as Python app — time-based releases make sense for continuous deployment |
| 61 | + |
| 62 | +--- |
| 63 | + |
| 64 | +## 2. Workflow Evidence |
| 65 | + |
| 66 | +### ✅ Successful Workflow Run |
| 67 | +**Link:** [Go CI #1 - Success](https://github.com/3llimi/DevOps-Core-Course/actions/runs/21924646855) |
| 68 | +- **Commit:** `c30868b` (Bonus Task) |
| 69 | +- **Status:** ✅ All jobs passed |
| 70 | +- **Jobs:** test → docker |
| 71 | +- **Duration:** ~1m 45s |
| 72 | + |
| 73 | +### ✅ Tests Passing Locally |
| 74 | +```bash |
| 75 | +$ cd app_go |
| 76 | +$ go test -v ./... |
| 77 | +=== RUN TestHomeEndpoint |
| 78 | +--- PASS: TestHomeEndpoint (0.00s) |
| 79 | +=== RUN TestHomeReturnsJSON |
| 80 | +--- PASS: TestHomeReturnsJSON (0.00s) |
| 81 | +=== RUN TestHomeHasServiceInfo |
| 82 | +--- PASS: TestHomeHasServiceInfo (0.00s) |
| 83 | +=== RUN TestHomeHasSystemInfo |
| 84 | +--- PASS: TestHomeHasSystemInfo (0.00s) |
| 85 | +=== RUN TestHealthEndpoint |
| 86 | +--- PASS: TestHealthEndpoint (0.00s) |
| 87 | +=== RUN TestHealthReturnsJSON |
| 88 | +--- PASS: TestHealthReturnsJSON (0.00s) |
| 89 | +=== RUN TestHealthHasStatus |
| 90 | +--- PASS: TestHealthHasStatus (0.00s) |
| 91 | +=== RUN TestHealthHasUptime |
| 92 | +--- PASS: TestHealthHasUptime (0.00s) |
| 93 | +PASS |
| 94 | +ok devops-info-service 0.245s |
| 95 | +``` |
| 96 | + |
| 97 | +### ✅ Docker Image on Docker Hub |
| 98 | +**Link:** [3llimi/devops-info-service-go](https://hub.docker.com/r/3llimi/devops-info-service-go) |
| 99 | +- **Latest tag:** `2026.02.11-c30868b` |
| 100 | +- **Size:** ~15 MB compressed (6x smaller than Python!) |
| 101 | +- **Platform:** linux/amd64 |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## 3. Go-Specific Best Practices |
| 106 | + |
| 107 | +### 1. **Go Module Caching** |
| 108 | +**Implementation:** |
| 109 | +```yaml |
| 110 | +- name: Set up Go |
| 111 | + uses: actions/setup-go@v5 |
| 112 | + with: |
| 113 | + go-version: '1.23' |
| 114 | + cache-dependency-path: app_go/go.sum |
| 115 | +``` |
| 116 | +**Why it helps:** Caches downloaded modules, speeds up `go mod download` by ~80% |
| 117 | + |
| 118 | +### 2. **Code Formatting Check (gofmt)** |
| 119 | +**Implementation:** |
| 120 | +```yaml |
| 121 | +- name: Run gofmt |
| 122 | + run: | |
| 123 | + gofmt -l . |
| 124 | + test -z "$(gofmt -l .)" |
| 125 | +``` |
| 126 | +**Why it helps:** Enforces Go's official code style, prevents formatting debates |
| 127 | + |
| 128 | +### 3. **Static Analysis (go vet)** |
| 129 | +**Implementation:** |
| 130 | +```yaml |
| 131 | +- name: Run go vet |
| 132 | + run: go vet ./... |
| 133 | +``` |
| 134 | +**Why it helps:** Catches common mistakes (unreachable code, suspicious constructs, Printf errors) |
| 135 | + |
| 136 | +### 4. **Conditional Docker Push** |
| 137 | +**Implementation:** |
| 138 | +```yaml |
| 139 | +docker: |
| 140 | + needs: test |
| 141 | + if: github.event_name == 'push' # Only push on direct pushes, not PRs |
| 142 | +``` |
| 143 | +**Why it helps:** Prevents pushing to Docker Hub from untrusted PR forks |
| 144 | + |
| 145 | +### 5. **Multi-Stage Docker Build (from Lab 2)** |
| 146 | +**Why it helps:** |
| 147 | +- Builder stage: 336 MB (golang:1.25-alpine) |
| 148 | +- Final image: 15 MB (alpine:3.19 + binary) |
| 149 | +- **97.7% size reduction!** |
| 150 | + |
| 151 | +--- |
| 152 | + |
| 153 | +## 4. Comparison: Python vs Go CI |
| 154 | + |
| 155 | +| Aspect | Python CI | Go CI | |
| 156 | +|--------|-----------|-------| |
| 157 | +| **Test Framework** | pytest (external) | testing (built-in) | |
| 158 | +| **Linting** | ruff | gofmt + go vet | |
| 159 | +| **Coverage Tool** | pytest-cov → Coveralls | go test -cover (not uploaded) | |
| 160 | +| **Security Scan** | Snyk | None (Go has fewer dependency vulns) | |
| 161 | +| **Dependency Install** | pip install (45s → 8s cached) | go mod download (20s → 3s cached) | |
| 162 | +| **Docker Build Time** | ~2m (uncached) | ~1m 30s (uncached) | |
| 163 | +| **Docker Image Size** | 86 MB | 15 MB (6x smaller!) | |
| 164 | +| **Total CI Time** | ~3m (uncached), ~1m 12s (cached) | ~2m (uncached), ~45s (cached) | |
| 165 | +| **Jobs** | 3 (test, docker, security) | 2 (test, docker) | |
| 166 | + |
| 167 | +--- |
| 168 | + |
| 169 | +## 5. Path Filters in Action |
| 170 | + |
| 171 | +### Example: Commit to `app_python/` |
| 172 | +```bash |
| 173 | +$ git commit -m "Update Python app" |
| 174 | +``` |
| 175 | +**Result:** |
| 176 | +- ✅ **Python CI triggers** (path matches `app_python/**`) |
| 177 | +- ❌ **Go CI does NOT trigger** (path doesn't match `app_go/**`) |
| 178 | + |
| 179 | +### Example: Commit to `app_go/` |
| 180 | +```bash |
| 181 | +$ git commit -m "Update Go app" |
| 182 | +``` |
| 183 | +**Result:** |
| 184 | +- ❌ **Python CI does NOT trigger** |
| 185 | +- ✅ **Go CI triggers** (path matches `app_go/**`) |
| 186 | + |
| 187 | +### Example: Commit to both |
| 188 | +```bash |
| 189 | +$ git add app_python/ app_go/ |
| 190 | +$ git commit -m "Update both apps" |
| 191 | +``` |
| 192 | +**Result:** |
| 193 | +- ✅ **Both workflows trigger in parallel** |
| 194 | +- Total time: ~2m (parallel) vs ~5m (sequential) |
| 195 | + |
| 196 | +--- |
| 197 | + |
| 198 | +## 6. Why No Snyk for Go? |
| 199 | + |
| 200 | +**Rationale:** |
| 201 | +1. **Go has a smaller dependency surface** — this app has ZERO external dependencies |
| 202 | +2. **Static binaries** — dependencies are compiled in, not loaded at runtime |
| 203 | +3. **Go's security model** — Standard library is well-audited |
| 204 | +4. **govulncheck exists** — Could add `go run golang.org/x/vuln/cmd/govulncheck@latest ./...` in future |
| 205 | + |
| 206 | +**If we had dependencies:** |
| 207 | +```yaml |
| 208 | +- name: Run govulncheck |
| 209 | + run: | |
| 210 | + go install golang.org/x/vuln/cmd/govulncheck@latest |
| 211 | + govulncheck ./... |
| 212 | +``` |
| 213 | + |
| 214 | +--- |
| 215 | + |
| 216 | +## 7. Key Decisions |
| 217 | + |
| 218 | +### **Testing Framework: Built-in vs External** |
| 219 | +**Choice:** Go's built-in `testing` package |
| 220 | + |
| 221 | +**Why not testify or ginkgo?** |
| 222 | +- **Zero dependencies** aligns with Lab 1 goal (single binary) |
| 223 | +- **Standard library is enough** for HTTP endpoint testing |
| 224 | +- **Simpler CI** (no extra install step) |
| 225 | + |
| 226 | +### **Linting: gofmt + go vet** |
| 227 | +**Why this combo?** |
| 228 | +- `gofmt` — Formatting (all Go code should be gofmt'd) |
| 229 | +- `go vet` — Logic errors and suspicious constructs |
| 230 | +- Could add `golangci-lint` later for more advanced checks |
| 231 | + |
| 232 | +### **Docker Image Naming** |
| 233 | +**Image:** `3llimi/devops-info-service-go` |
| 234 | + |
| 235 | +**Why `-go` suffix?** |
| 236 | +- Distinguishes from Python image (`3llimi/devops-info-service`) |
| 237 | +- Clear for users: "I need the Go version" |
| 238 | +- Same tagging strategy (latest + CalVer) |
| 239 | + |
| 240 | +--- |
| 241 | + |
| 242 | +## 8. CI Workflow Structure |
| 243 | + |
| 244 | +``` |
| 245 | +Go CI Workflow |
| 246 | +│ |
| 247 | +├── Job 1: Test (runs on all triggers) |
| 248 | +│ ├── Checkout code |
| 249 | +│ ├── Set up Go 1.23 (with module cache) |
| 250 | +│ ├── Install dependencies (go mod download) |
| 251 | +│ ├── Run gofmt (formatting check) |
| 252 | +│ ├── Run go vet (static analysis) |
| 253 | +│ └── Run go test (unit tests) |
| 254 | +│ |
| 255 | +└── Job 2: Docker (needs: test, only on push) |
| 256 | + ├── Checkout code |
| 257 | + ├── Set up Docker Buildx |
| 258 | + ├── Log in to Docker Hub |
| 259 | + ├── Extract metadata (tags, labels) |
| 260 | + └── Build and push (multi-stage, cached) |
| 261 | +``` |
| 262 | +
|
| 263 | +--- |
| 264 | +
|
| 265 | +## 9. How to Run Tests Locally |
| 266 | +
|
| 267 | +```bash |
| 268 | +# Navigate to Go app |
| 269 | +cd app_go |
| 270 | +
|
| 271 | +# Run tests |
| 272 | +go test -v ./... |
| 273 | +
|
| 274 | +# Run tests with coverage |
| 275 | +go test -v -cover ./... |
| 276 | +
|
| 277 | +# Generate HTML coverage report |
| 278 | +go test -coverprofile=coverage.out ./... |
| 279 | +go tool cover -html=coverage.out |
| 280 | +
|
| 281 | +# Check formatting |
| 282 | +gofmt -l . |
| 283 | +
|
| 284 | +# Auto-format code |
| 285 | +gofmt -w . |
| 286 | +
|
| 287 | +# Run static analysis |
| 288 | +go vet ./... |
| 289 | +
|
| 290 | +# Run all checks (like CI) |
| 291 | +gofmt -l . && go vet ./... && go test -v ./... |
| 292 | +``` |
| 293 | + |
| 294 | +--- |
| 295 | + |
| 296 | +## 10. Benefits of Multi-App CI |
| 297 | + |
| 298 | +### 1. **Efficiency** |
| 299 | +- **Before path filters:** Every commit triggered both workflows (~5m total) |
| 300 | +- **After path filters:** Only relevant workflow runs (~2m for one app) |
| 301 | +- **Savings:** 60% reduction in CI minutes for typical commits |
| 302 | + |
| 303 | +### 2. **Isolation** |
| 304 | +- Python breaking? Go still deploys |
| 305 | +- Go refactoring? Python CI unaffected |
| 306 | +- Clear separation of concerns |
| 307 | + |
| 308 | +### 3. **Parallel Execution** |
| 309 | +- Both apps can test/build simultaneously |
| 310 | +- Faster feedback on multi-app changes |
| 311 | +- Better resource utilization |
| 312 | + |
| 313 | +### 4. **Scalability** |
| 314 | +- Easy to add Rust/Java/etc. apps |
| 315 | +- Pattern: `app_<lang>/` + `.github/workflows/<lang>-ci.yml` |
| 316 | +- Each app gets its own Docker image |
| 317 | + |
| 318 | +--- |
| 319 | + |
| 320 | +## Summary |
| 321 | + |
| 322 | +✅ **Go CI Pipeline Complete:** |
| 323 | +- Unit tests with Go's built-in testing package |
| 324 | +- gofmt + go vet linting |
| 325 | +- Docker build/push with CalVer versioning |
| 326 | +- Path filters for monorepo efficiency |
| 327 | +- Runs independently from Python CI |
| 328 | + |
| 329 | +✅ **Path Filters Working:** |
| 330 | +- Python changes → Python CI only |
| 331 | +- Go changes → Go CI only |
| 332 | +- Both changes → Both CIs in parallel |
| 333 | + |
| 334 | +🎯 **Bonus Task Achieved:** |
| 335 | +- Multi-app CI with intelligent path-based triggers |
| 336 | +- 60% reduction in CI minutes for single-app commits |
| 337 | +- Scalable pattern for future languages |
| 338 | + |
| 339 | +📊 **Performance:** |
| 340 | +- Go CI faster than Python (45s cached vs 1m 12s) |
| 341 | +- Docker image 6x smaller (15 MB vs 86 MB) |
| 342 | +- Zero external dependencies |
0 commit comments