ci: add gitlab pipeline for tests, images and helm chart; document deployment
Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qu4xuNCkFLDByYWUrjanny
This commit is contained in:
parent
951ea9ccb4
commit
b480d4fe66
4 changed files with 129 additions and 1 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -6,3 +6,4 @@
|
|||
/frontend/dist/
|
||||
.env
|
||||
GIT_SHA
|
||||
/dist/
|
||||
|
|
|
|||
97
.gitlab-ci.yml
Normal file
97
.gitlab-ci.yml
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
stages:
|
||||
- test
|
||||
- build
|
||||
- bundle
|
||||
- deploy
|
||||
|
||||
|
||||
BackendUnitTests:
|
||||
image: golang:1.27.3-alpine
|
||||
stage: test
|
||||
needs: []
|
||||
variables:
|
||||
CGO_ENABLED: "0"
|
||||
script:
|
||||
- cd backend
|
||||
- go test ./...
|
||||
|
||||
FrontendUnitTests:
|
||||
image: node:24.22-alpine
|
||||
stage: test
|
||||
needs: []
|
||||
before_script:
|
||||
- corepack enable
|
||||
script:
|
||||
- cd frontend
|
||||
- pnpm install --frozen-lockfile
|
||||
- pnpm test
|
||||
- pnpm build
|
||||
|
||||
BackendDocker:
|
||||
image: jonasled.dev/infra/docker-build:master@sha256:af92638195740083c2de64db6b9807da647e0b01dfec335a72618c5d00e96cce
|
||||
stage: build
|
||||
needs: []
|
||||
script:
|
||||
- export IMAGE_NAME=$CI_REGISTRY_IMAGE/backend
|
||||
- cd backend
|
||||
- build-image
|
||||
- mv image-hash.txt ../image-hash-backend.txt
|
||||
- mv image-scan-report.bom.json ../backend.bom.json
|
||||
artifacts:
|
||||
paths:
|
||||
- image-hash-backend.txt
|
||||
- backend.bom.json
|
||||
|
||||
FrontendDocker:
|
||||
image: jonasled.dev/infra/docker-build:master@sha256:af92638195740083c2de64db6b9807da647e0b01dfec335a72618c5d00e96cce
|
||||
stage: build
|
||||
needs: []
|
||||
script:
|
||||
- export IMAGE_NAME=$CI_REGISTRY_IMAGE/frontend
|
||||
- cd frontend
|
||||
# The image build context has no .git, so the commit goes into a file that vite.config.ts reads.
|
||||
- echo "$CI_COMMIT_SHA" > GIT_SHA
|
||||
- build-image
|
||||
- mv image-hash.txt ../image-hash-frontend.txt
|
||||
- mv image-scan-report.bom.json ../frontend.bom.json
|
||||
artifacts:
|
||||
paths:
|
||||
- image-hash-frontend.txt
|
||||
- frontend.bom.json
|
||||
|
||||
HelmChart:
|
||||
# Renders the chart, packages it and pushes it to the project's OCI registry:
|
||||
# helm install timetracker oci://$CI_REGISTRY/$CI_PROJECT_PATH/charts/timetracker --version <version>
|
||||
# The job needs both image builds, so it runs only if they succeeded. The chart's image
|
||||
# repositories and tags are set to this project's registry path and this commit's SHA.
|
||||
image:
|
||||
name: alpine/helm:4.3.1
|
||||
entrypoint: [""]
|
||||
stage: bundle
|
||||
needs:
|
||||
- BackendDocker
|
||||
- FrontendDocker
|
||||
variables:
|
||||
CHART_DIR: deploy/helm/timetracker
|
||||
CHART_REPO: oci://$CI_REGISTRY/$CI_PROJECT_PATH/charts
|
||||
script:
|
||||
- sed -i "s|__GIT_COMMIT_SHA__|$CI_COMMIT_SHA|g; s|__CI_REGISTRY_IMAGE__|$CI_REGISTRY_IMAGE|g" "$CHART_DIR/values.yaml"
|
||||
- if grep -Eq "__GIT_COMMIT_SHA__|__CI_REGISTRY_IMAGE__" "$CHART_DIR/values.yaml"; then echo "placeholder left in values.yaml"; exit 1; fi
|
||||
- helm lint "$CHART_DIR" --strict
|
||||
# Render with the common configurations so template failures show up in CI.
|
||||
- helm template ci "$CHART_DIR" --set secrets.inline.jwtSecret=ci > /dev/null
|
||||
- helm template ci "$CHART_DIR" --set config.database.driver=postgres --set config.database.host=db --set config.database.user=timetracker --set secrets.inline.dbPassword=ci --set secrets.inline.jwtSecret=ci --set replicaCount=2 --set ingress.enabled=true --set 'ingress.hosts={timetracker.example.com}' > /dev/null
|
||||
- |
|
||||
# Flux picks the highest version, so versions must only grow. The patch number is the pipeline IID,
|
||||
# which increases with every pipeline of the project. The major.minor part comes from Chart.yaml.
|
||||
BASE_VERSION=$(sed -n 's/^version: *//p' "$CHART_DIR/Chart.yaml" | cut -d. -f1,2)
|
||||
CHART_VERSION="${BASE_VERSION}.${CI_PIPELINE_IID}"
|
||||
helm package "$CHART_DIR" --version "$CHART_VERSION" --app-version "$CI_COMMIT_SHA" --destination dist
|
||||
- helm registry login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
|
||||
- helm push dist/timetracker-*.tgz "$CHART_REPO"
|
||||
rules:
|
||||
- if: $CI_COMMIT_TAG
|
||||
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
|
||||
artifacts:
|
||||
paths:
|
||||
- dist/
|
||||
6
Makefile
6
Makefile
|
|
@ -1,4 +1,4 @@
|
|||
.PHONY: test test-backend test-frontend swagger run build
|
||||
.PHONY: test test-backend test-frontend lint-chart swagger run build
|
||||
|
||||
test: test-backend test-frontend
|
||||
|
||||
|
|
@ -8,6 +8,10 @@ test-backend:
|
|||
test-frontend:
|
||||
cd frontend && pnpm test
|
||||
|
||||
lint-chart:
|
||||
helm lint deploy/helm/timetracker --strict --set secrets.inline.jwtSecret=ci
|
||||
helm template ci deploy/helm/timetracker --set secrets.inline.jwtSecret=ci > /dev/null
|
||||
|
||||
swagger:
|
||||
cd backend && go run github.com/swaggo/swag/cmd/swag@v1.16.6 init -g cmd/server/main.go -o docs --parseDependency --parseInternal
|
||||
|
||||
|
|
|
|||
26
README.md
26
README.md
|
|
@ -29,6 +29,31 @@ docker compose up --build
|
|||
|
||||
Frontend: http://localhost:8080 · Swagger UI: http://localhost:8080/swagger/index.html
|
||||
|
||||
Die Images werden aus ihrem Unterverzeichnis gebaut (`backend/Dockerfile`, `frontend/Dockerfile`), das ist auch der Build-Kontext in der CI.
|
||||
Das Frontend-Image schreibt den Commit aus der Datei `frontend/GIT_SHA` in die Anzeige; lokal steht dort `dev`.
|
||||
|
||||
## Kubernetes mit Helm
|
||||
|
||||
Das Chart liegt unter `deploy/helm/timetracker`. Es startet ein Backend-Pod und ein Frontend-Pod. Das Frontend leitet `/api` an den Service `backend` weiter, daher gilt: **ein Release pro Namespace**.
|
||||
|
||||
```bash
|
||||
helm install timetracker deploy/helm/timetracker \
|
||||
--set image.backend.repository=registry.example.com/timetracker/backend \
|
||||
--set image.backend.tag=<tag> \
|
||||
--set image.frontend.repository=registry.example.com/timetracker/frontend \
|
||||
--set image.frontend.tag=<tag> \
|
||||
--set secrets.inline.jwtSecret="$(openssl rand -hex 32)" \
|
||||
--set ingress.enabled=true --set 'ingress.hosts={timetracker.example.com}'
|
||||
```
|
||||
|
||||
- **SQLite (Standard):** eine Datei in einem PersistentVolumeClaim unter `/data`. Nur ein Replica möglich, das Chart verweigert mehr.
|
||||
- **PostgreSQL / MariaDB:** `config.database.driver`, `config.database.host`, `config.database.user`, `config.database.name` setzen und `secrets.inline.dbPassword` übergeben. Für Produktion besser `secrets.existingSecret` mit den Schlüsseln `APP_JWT_SECRET` und `DB_DSN` verwenden.
|
||||
- **Sicherheit:** Beide Pods laufen als Nutzer 65532 ohne Root-Rechte, mit read-only Root-Dateisystem, ohne Capabilities, ohne Privilege Escalation, mit `RuntimeDefault`-Seccomp und ohne ServiceAccount-Token. NetworkPolicies lassen nur Frontend-Pods zum Backend. Die Abschnitte `securityContext` in `values.yaml` bzw. den Templates sind die Stelle für Anpassungen.
|
||||
|
||||
## CI
|
||||
|
||||
`.gitlab-ci.yml` führt Go- und Frontend-Tests aus, baut beide Images (`build-image`), rendert und prüft das Chart (`helm lint --strict`, `helm template`) und veröffentlicht es als OCI-Chart. Der Chart-Tag ist die Pipeline-IID als Patch-Version; Image-Tags und Registry-Pfad werden in der Pipeline in `values.yaml` eingesetzt.
|
||||
|
||||
## Lokale Entwicklung
|
||||
|
||||
Voraussetzungen: Go (siehe `backend/go.mod`), Node.js mit pnpm.
|
||||
|
|
@ -63,6 +88,7 @@ Für MariaDB und PostgreSQL in Docker: `docker compose --profile postgres up` bz
|
|||
|
||||
```bash
|
||||
make test # Go- und Frontend-Tests
|
||||
make lint-chart # Helm-Chart prüfen
|
||||
make swagger # Swagger-Dateien neu erzeugen
|
||||
```
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue