---
slug: /213240/tekton
displayed_sidebar: "current"
category: "guides"
tags: ["tekton", "kubernetes"]
authors: ["Kyle Penfound"]
date: "2023-10-26"
---

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

# Use Dagger with Tekton

## Introduction

[Tekton](https://tekton.dev/) is an open source framework for CI/CD implementation, with built-in support for Kubernetes and other CI/CD tools. This guide explains how to run Dagger pipelines using Tekton.

## Requirements

This guide assumes that:

- You have a basic understanding of Kubernetes and Tekton.
- You have a project with a Dagger pipeline in a Git repository. If not, follow the steps in Appendix A to [create and populate a GitHub repository with a simple project and Dagger pipeline](#appendix-a-create-a-github-repository-with-an-example-project).

## Step 1: Install Tekton

The first step is to install [Tekton](https://tekton.dev/) in the Kubernetes cluster and the [Tekton CLI](https://tekton.dev/docs/cli/).

Follow the [Tekton installation instructions](https://tekton.dev/docs/getting-started/) and the [Tekton CLI installation steps](https://tekton.dev/docs/cli/), adjusting them as needed to your own requirements. Once you've successfully installed Tekton and the Tekton CLI, continue to the next step.

## Step 2: Install the `git-clone` Task

Next, install the `git-clone` Task from Tekton Hub. This Task adds repository cloning capabilities to your Tekton Pipeline, and is used by the Tekton Pipeline defined later in this guide.

```shell
tkn hub install task git-clone
```

## Step 3: Create a Tekton Pipeline, Task and PipelineRun

Follow the steps below:

1. Define a new Tekton Pipeline as follows, in a file named `git-pipeline.yaml`.

    ```yaml file=./snippets/tekton/git-pipeline.yaml
    ```

    This Pipeline references two Tasks:
      - The `git-clone` Task, to check out the Git repository for the project into a Tekton Workspace;
      - A custom `dagger` Task, to run the Dagger pipeline for the project (defined below).

1. Define a new Tekton Task as follows, in a file named `dagger-task.yaml`.

    ```yaml file=./snippets/tekton/dagger-task.yaml
    ```

    This Task installs the dependencies needed to execute the Dagger pipeline for the project (which was checked out in the previous Tekton Pipeline) and then executes the pipeline using the `npm run ci` command.

1. Define a new Tekton PipelineRun as follows, in a file named `git-pipeline-run.yaml`.

    ```yaml file=./snippets/tekton/git-pipeline-run.yaml
    ```

    This PipelineRun corresponds to the Tekton Pipeline created previously. It executes the Tekton Pipeline with a given set of input parameters: the Git repository URL and an optional Dagger Cloud token. Update the `YOUR_REPOSITORY_URL` placeholder with the correct repository URL and, if you have a Dagger Cloud token, replace the `YOUR_DAGGER_CLOUD_TOKEN` placeholder with the token or a Kubernetes secret holding the token.

A few important points to note:

- In the 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.
- Setting the `DAGGER_CLOUD_TOKEN` environment variable in the Task and the `dagger-cloud-token` parameter in the Pipeline and PipelineRun is only necessary if integrating with [Dagger Cloud](https://dagger.cloud/).

## Step 4: Run the Pipeline

1. Apply the configuration:

    ```shell
    kubectl apply -f dagger-task.yaml
    kubectl apply -f git-pipeline-yaml
    ```

1. Run the Tekton Pipeline:

    ```shell
    kubectl create -f git-pipeline-run.yaml
    ```

    The output will look something like `pipelinerun.tekton.dev/clone-read-run-mwvkm created`
    To see the logs from the PipelineRun, obtain the PipelineRun name from the output and run `tkn pipelinerun logs clone-read-run-<id> -f`.

Once the PipelineRun has successfully completed, run it again. Dagger's caching should result in a significantly faster second execution.

## Conclusion

This example demonstrated how to integrate Dagger into a Tekton Pipeline using a custom Task.

To learn more about Dagger, use the [API Key Concepts](../api/975146-concepts.mdx) page and the [Go](https://pkg.go.dev/dagger.io/dagger), [Node.js](../sdk/nodejs/reference/modules.md) and [Python](https://dagger-io.readthedocs.org/) SDK References. For more information on Tekton, refer to the [official documentation](https://tekton.dev/docs/).

## Appendix A: Create a GitHub repository with an example project

This guide assumes that you have a Git repository with a project and a Dagger pipeline. If you don't, follow the steps below to create a GitHub repository and add a simple project and Dagger pipeline to it.

1. Clone the [Dagger starter application](https://github.com/dagger/hello-dagger):

  ```shell
  git clone git@github.com:dagger/hello-dagger.git
  ```

1. Create a new `ci` subdirectory:

  ```shell
  cd hello-dagger
  mkdir ci && cd ci
  ```

1. Add a simple Dagger pipeline as `index.mjs`:

  ```javascript title="index.mjs" file=./snippets/tekton/index.mjs
  ```

1. Commit the changes:

  ```shell
  cd ..
  git add .
  git commit -m "Initial commit"
  ```

1. Log in to GitHub using the GitHub CLI:

  ```shell
  gh auth login
  ```

1. Create a repository in your GitHub account and push the changes to it:

  ```shell
  gh repo create myapp --push --remote upstream --source . --public
  ```
