|
1 | | -# 🛠️ Harbor CLI Dagger Pipeline |
| 1 | +# 🛠️ Harbor CLI — Dagger Pipeline |
2 | 2 |
|
3 | | -We use [Dagger](https://dagger.io) to define a CI/CD pipeline for building, linting, and publishing the [Harbor CLI](https://github.com/goharbor/harbor-cli). |
4 | | -This README will help beginners understand how to use Dagger in local development and CI workflows. |
| 3 | +We use [Dagger](https://dagger.io) to define a **modular and reproducible CI/CD pipeline** for building, linting, testing, and publishing the [Harbor CLI](https://github.com/goharbor/harbor-cli). |
| 4 | +This README provides a clear reference for contributors and maintainers to understand, run, and extend the pipeline locally or in CI. |
5 | 5 |
|
6 | | -## Prerequisites |
7 | | - |
8 | | -Before you start, ensure you have the following: |
9 | | - |
10 | | -1. Dagger: Install the latest version of Dagger. You can check the official documentation for installation steps: [Dagger Installation Guide](https://docs.dagger.io/install). |
11 | | - |
12 | | -## Dagger Setup and Development Mode |
| 6 | +--- |
13 | 7 |
|
14 | | -### Run Dagger Develop |
| 8 | +## 🚧 Prerequisites |
15 | 9 |
|
16 | | -```bash |
17 | | -dagger develop |
18 | | -``` |
| 10 | +Before using the pipeline, make sure you have: |
19 | 11 |
|
20 | | -This command will generate the necessary files and configuration for building and running Dagger. |
| 12 | +1. **Dagger CLI** — Install the latest version from the official docs: |
| 13 | + 👉 [Dagger Installation Guide](https://docs.dagger.io/install) |
| 14 | +2. **Go** — Installed according to the version specified in the project’s `go.mod`. |
| 15 | +3. **Docker** — Required if you’re publishing images. |
21 | 16 |
|
| 17 | +--- |
22 | 18 |
|
23 | | -## 📦 Dagger Functions Explained |
| 19 | +## ⚙️ Setup and Development Mode |
24 | 20 |
|
25 | | -### 🔧 `BuildDev(platform)` |
| 21 | +### Run Dagger in Development Mode |
26 | 22 |
|
27 | | -Builds a development binary for your target platform. |
| 23 | +To start the Dagger session and enable live code reloads: |
28 | 24 |
|
29 | 25 | ```bash |
30 | | -dagger call build-dev --platform="linux/amd64" export --path=bin/harbor-dev |
| 26 | +dagger develop |
31 | 27 | ``` |
32 | 28 |
|
33 | | -### 🧼 `LintReport()` |
| 29 | +This command prepares the environment for pipeline development and local testing. |
34 | 30 |
|
35 | | -Runs `golangci-lint` on your code and saves the report to a file. |
| 31 | +## 📦 Dagger Functions Overview |
36 | 32 |
|
37 | | -```bash |
38 | | -dagger call lint-report export --path=./LintReport.json |
39 | | -``` |
| 33 | +| **Name** | **Description** | |
| 34 | +|--------------------------------|-------------------------------------------------------------------------------------------------| |
| 35 | +| `lint` | Runs `golangci-lint` and prints the report as a string to stdout. | |
| 36 | +| `lint-report` | Runs `golangci-lint` and writes the lint report to a file. | |
| 37 | +| `pipeline` | Executes the **full CI/CD pipeline** including build, test, lint, and publish stages. | |
| 38 | +| `run-doc` | Generates CLI documentation and returns the directory containing generated files. | |
| 39 | +| `test` | Runs all Go tests in the repository. | |
| 40 | +| `test-report` | Executes Go tests and outputs a structured JSON test report. | |
| 41 | +| `test-coverage` | Runs Go tests with coverage tracking. | |
| 42 | +| `test-coverage-report` | Processes coverage data and returns a formatted Markdown report. | |
| 43 | +| `vulnerability-check` | Runs `govulncheck` to detect known vulnerabilities in dependencies. | |
| 44 | +| `vulnerability-check-report` | Runs `govulncheck` and saves results to a file (`vulnerability-check.report`). | |
| 45 | +| `build-dev` | Create build of Harbor CLI for local testing and development| |
40 | 46 |
|
41 | | -### 📝 `TestCoverageReport()` |
| 47 | +--- |
42 | 48 |
|
43 | | -Runs go test coverage tools and creates a report. |
44 | | -```bash |
45 | | -dagger call test-coverage-report export --path=coverage-report.md |
46 | | -``` |
| 49 | +## 🧩 Example Usage |
47 | 50 |
|
48 | | -### ✅ `CheckCoverageThreshold(context, threshold)` |
| 51 | +Below are some common commands to run specific Dagger functions locally: |
49 | 52 |
|
50 | | -Runs go test coverage tools and creates a report. The total coverage is compared to a threshold that can be set to e.g. 80%. |
51 | 53 | ```bash |
52 | | -dagger call check-coverage-threshold --threshold 80.0 |
53 | | -``` |
| 54 | +# Development build for binaries |
54 | 55 |
|
55 | | -### 🚀 `PublishImage(registry, imageTags)` |
| 56 | +dagger call build-dev --source=. --platform="linux/amd64" export --path=bin/harbor-dev |
56 | 57 |
|
57 | | -Builds and publishes the Harbor CLI image to the given container registry with proper OCI metadata labels. |
| 58 | +# Print report to stdout |
| 59 | +dagger call lint |
58 | 60 |
|
59 | | -Before running the command you have to export you registry password |
| 61 | +# Save report to a file |
| 62 | +dagger call lint-report export --path=LintReport.json |
60 | 63 |
|
61 | | -```shell |
62 | | -export REGPASS=Harbor12345 |
63 | | -``` |
| 64 | +# Run Tests |
| 65 | +dagger call test |
64 | 66 |
|
65 | | -```bash |
66 | | -dagger call publish-image \ |
67 | | - --registry=demo.goharbor.io \ |
68 | | - --registry-username=harbor-cli \ |
69 | | - --registry-password=env:REGPASS \ |
70 | | - --imageTags=v0.1.0,latest |
71 | | -``` |
| 67 | +# Generate a JSON Report |
| 68 | +dagger call test-report export --path=TestReport.json |
72 | 69 |
|
73 | | ---- |
| 70 | +# Test Coverage |
| 71 | +dagger call test-coverage |
74 | 72 |
|
75 | | -## ⚙️ Configuration Constants |
| 73 | +# Generate a Markdown Report |
| 74 | +dagger call test-coverage-report export --path=coverage-report.md |
76 | 75 |
|
77 | | -Dagger uses these constant versions (you can modify them as needed): |
| 76 | +# Vulnerability Check |
| 77 | +dagger call vulnerability-check |
78 | 78 |
|
79 | | -```go |
80 | | -const ( |
81 | | - GO_VERSION = "1.24.2" |
82 | | - GOLANGCILINT_VERSION = "v2.1.2" |
83 | | - SYFT_VERSION = "v1.9.0" |
84 | | - GORELEASER_VERSION = "v2.3.2" |
85 | | -) |
| 79 | +# Generate a Report |
| 80 | +dagger call vulnerability-check-report export --path=vuln.report |
| 81 | + |
| 82 | +# Generate CLI docs |
| 83 | +dagger call run-doc export --path=docs/cli |
86 | 84 | ``` |
87 | 85 |
|
88 | | ---- |
89 | 86 |
|
90 | | -## 💡 Tips for Beginners |
| 87 | +## 💡 Tips for Contributors |
91 | 88 |
|
92 | | -- Every container step is **reproducible** you can build locally or in GitHub Actions without changes. |
93 | | -- Use Dagger to cache Go builds and lint output, speeding up re-runs. |
| 89 | +- Every step in Dagger is **deterministic and reproducible** — what you run locally is identical to CI. |
| 90 | +- Use Dagger’s built-in caching to accelerate Go builds, lint runs, and dependency installs. |
| 91 | +- Modular functions let you run only what you need, improving iteration speed and debugging efficiency. |
| 92 | +- Prefer using `dagger develop` for fast iteration and testing new steps before committing. |
| 93 | +- Store output reports (lint, test, coverage) under a consistent `/reports` directory for easier CI integration. |
| 94 | +- If you modify or add new pipeline steps, document them under **Dagger Functions Overview** to maintain clarity. |
| 95 | +- Always validate pipelines with `dagger call pipeline` locally before merging into main. |
94 | 96 |
|
95 | 97 | --- |
96 | 98 |
|
97 | 99 | ## 📚 References |
98 | 100 |
|
99 | | -- [Dagger Go SDK Docs](https://pkg.go.dev/dagger.io/dagger) |
100 | | -- [golangci-lint](https://golangci-lint.run/) |
101 | | -- [Goreleaser](https://goreleaser.com/) |
| 101 | +- [Dagger Go SDK](https://pkg.go.dev/dagger.io/dagger) |
| 102 | +- [golangci-lint Docs](https://golangci-lint.run/) |
| 103 | +- [govulncheck](https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck) |
0 commit comments