Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/gitlab-ci/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Minimal GitLab CI pipeline for a Kapitan project.
#
# compile -> run "kapitan compile" and keep compiled/ as an artifact
# validate -> check the generated Kubernetes manifests with kubeconform
#
# Both images ship their own entrypoint, so entrypoint: [""] is set to let
# GitLab run the script through a shell.

stages:
- compile
- validate

compile:
stage: compile
image:
name: kapicorp/kapitan:0.36.1
entrypoint: [""]
script:
- kapitan compile
artifacts:
paths:
- compiled/
expire_in: 1 week

validate:
stage: validate
needs:
- compile
image:
name: ghcr.io/yannh/kubeconform:v0.6.7-alpine
entrypoint: [""]
script:
- /kubeconform -summary -strict compiled/
3 changes: 3 additions & 0 deletions examples/gitlab-ci/.kapitan
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version: 0.36.1
compile:
prune: true
78 changes: 78 additions & 0 deletions examples/gitlab-ci/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Kapitan in GitLab CI

This example runs Kapitan inside GitLab CI: one job compiles the project and
keeps `compiled/` as an artifact, a second job validates the generated
Kubernetes manifests with [kubeconform](https://github.com/yannh/kubeconform).

Tested with Kapitan `0.36.1`.

## Layout

```
gitlab-ci/
.kapitan
.gitlab-ci.yml # compile -> validate pipeline
inventory/
classes/common.yml
targets/echo.yml # one app target
components/
manifests/app.jsonnet # emits a Deployment and a Service
compiled/
echo/manifests/{deployment.yaml,service.yaml}
```

## The pipeline

```yaml
stages:
- compile
- validate

compile:
image:
name: kapicorp/kapitan:0.36.1
entrypoint: [""]
script:
- kapitan compile
artifacts:
paths:
- compiled/

validate:
needs: [compile]
image:
name: ghcr.io/yannh/kubeconform:v0.6.7-alpine
entrypoint: [""]
script:
- /kubeconform -summary -strict compiled/
```

Both images set their own entrypoint, so `entrypoint: [""]` lets GitLab run the
`script` block through a shell. The image tags are pinned on purpose; do not use
`latest` for a pipeline you depend on.

## Run the same steps locally

```bash
# compile
docker run --rm -i -u "$(id -u)" --network host -w /src -v "$PWD:/src" \
kapicorp/kapitan:0.36.1 compile

# validate
docker run --rm -v "$PWD:/src" -w /src --entrypoint /kubeconform \
ghcr.io/yannh/kubeconform:v0.6.7-alpine -summary -strict compiled/
```

Expected validate output:

```
Summary: 2 resources found in 2 files - Valid: 2, Invalid: 0, Errors: 0, Skipped: 0
```

## Optional follow-up: dynamic child pipelines

GitLab can treat a generated file as a
[child pipeline](https://docs.gitlab.com/ci/pipelines/downstream_pipelines/).
Kapitan can render that child `.gitlab-ci.yml` the same way it renders manifests
here, then a `trigger` job runs it. That is a separate step and is not required
for the basic compile-and-validate flow above.
22 changes: 22 additions & 0 deletions examples/gitlab-ci/compiled/echo/manifests/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: echo
name: echo
namespace: demo
spec:
replicas: 2
selector:
matchLabels:
app: echo
template:
metadata:
labels:
app: echo
spec:
containers:
- image: ealen/echo-server:0.9.2
name: echo
ports:
- containerPort: 8080
13 changes: 13 additions & 0 deletions examples/gitlab-ci/compiled/echo/manifests/service.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
apiVersion: v1
kind: Service
metadata:
labels:
app: echo
name: echo
namespace: demo
spec:
ports:
- port: 8080
targetPort: 8080
selector:
app: echo
46 changes: 46 additions & 0 deletions examples/gitlab-ci/components/manifests/app.jsonnet
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local kap = import "lib/kapitan.libjsonnet";
local inv = kap.inventory();
local app = inv.parameters.app;

local labels = { app: app.name };

{
deployment: {
apiVersion: "apps/v1",
kind: "Deployment",
metadata: {
name: app.name,
namespace: app.namespace,
labels: labels,
},
spec: {
replicas: app.replicas,
selector: { matchLabels: labels },
template: {
metadata: { labels: labels },
spec: {
containers: [
{
name: app.name,
image: app.image,
ports: [{ containerPort: app.port }],
},
],
},
},
},
},
service: {
apiVersion: "v1",
kind: "Service",
metadata: {
name: app.name,
namespace: app.namespace,
labels: labels,
},
spec: {
selector: labels,
ports: [{ port: app.port, targetPort: app.port }],
},
},
}
4 changes: 4 additions & 0 deletions examples/gitlab-ci/inventory/classes/common.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
parameters:
kapitan:
vars:
target: ${target_name}
19 changes: 19 additions & 0 deletions examples/gitlab-ci/inventory/targets/echo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
classes:
- common

parameters:
target_name: echo
app:
name: echo
namespace: demo
image: ealen/echo-server:0.9.2
replicas: 2
port: 8080

kapitan:
compile:
- input_type: jsonnet
input_paths:
- components/manifests/app.jsonnet
output_path: manifests
output_type: yaml
Loading