Skip to main content

Tekton

The following code sample demonstrates how to integrate Dagger with Tekton.

note

This Tekton sample assumes that the git-clone Task from Tekton Hub is already installed. This Task adds repository cloning capabilities to the Tekton Pipeline. To install the git-clone Task, use the following command:

tkn hub install task git-clone

The following Tekton Pipeline checks out the project into a Tekton Workspace and runs the dagger Tekton Task:

git-pipeline.yaml
apiVersion: tekton.dev/v1beta1
kind: Pipeline
metadata:
name: dagger-pipeline
spec:
description: |
This pipeline clones a Git repository, then runs the Dagger Function.
params:
- name: repo-url
type: string
description: The Git repository clone URL
- name: dagger-cloud-token
type: string
description: The Dagger Cloud token
workspaces:
- name: shared-data
description: |
This workspace contains the cloned repository files, so they can be read by the
next task.
tasks:
- name: fetch-source
taskRef:
name: git-clone
workspaces:
- name: output
workspace: shared-data
params:
- name: url
value: $(params.repo-url)
- name: dagger
runAfter: ["fetch-source"]
taskRef:
name: dagger
workspaces:
- name: source
workspace: shared-data
params:
- name: dagger-cloud-token
value: $(params.dagger-cloud-token)

The following Tekton Task installs the Dagger CLI and calls a Dagger Function. In this Tekton Task, the Dagger Engine runs as a sidecar and shares a socket with the Task itself. The Task uses dind as its runtime in order to have Docker available.

dagger-task.yaml
apiVersion: tekton.dev/v1beta1
kind: Task
metadata:
name: dagger
spec:
description: Run Dagger Function
workspaces:
- name: source
params:
- name: dagger-cloud-token
type: string
description: Dagger Cloud Token
volumes:
- name: dagger-socket
emptyDir: {}
- name: dagger-storage
emptyDir: {}
sidecars:
- name: dagger-engine
image: registry.dagger.io/engine:v0.10.2
args:
- "--oci-max-parallelism"
- "num-cpu"
securityContext:
privileged: true
capabilities:
add:
- ALL
readinessProbe:
exec:
command: ["buildctl", "debug", "workers"]
volumeMounts:
- mountPath: /var/run/buildkit
name: dagger-socket
- mountPath: /var/lib/dagger
name: dagger-storage
env:
- name: DAGGER_CLOUD_TOKEN
value: $(params.dagger-cloud-token)
steps:
# assumes a Go project
# modify to use different function(s) as needed
- name: read
image: docker:dind
workingDir: $(workspaces.source.path)
script: |
#!/usr/bin/env sh
apk add curl
curl -L https://dl.dagger.io/dagger/install.sh | BIN_DIR=$HOME/.local/bin sh
dagger -m github.com/kpenfound/dagger-modules/golang@v0.1.5 call build --project=. --args=.
volumeMounts:
- mountPath: /var/run/dagger
name: dagger-socket
env:
- name: _EXPERIMENTAL_DAGGER_RUNNER_HOST
value: unix:///var/run/dagger/buildkitd.sock
- name: DAGGER_CLOUD_TOKEN
value: $(params.dagger-cloud-token)

The following Tekton PipelineRun runs the Tekton Pipeline:

git-pipeline-run.yaml
apiVersion: tekton.dev/v1beta1
kind: PipelineRun
metadata:
generateName: clone-read-run-
spec:
pipelineRef:
name: dagger-pipeline
podTemplate:
securityContext:
fsGroup: 65532
workspaces:
- name: shared-data
volumeClaimTemplate:
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
params:
- name: repo-url
value: YOUR_REPOSITORY_URL_HERE
- name: dagger-cloud-token
value: YOUR_DAGGER_CLOUD_TOKEN_HERE

To apply the configuration and run the Tekton Pipeline, use the following commands:

kubectl apply -f dagger-task.yaml
kubectl apply -f git-pipeline-yaml
kubectl create -f git-pipeline-run.yaml

To see the logs from the PipelineRun, obtain the PipelineRun name from the output and run tkn pipelinerun logs clone-read-run-<id> -f.