Using Dagger with Jenkins in a simple CI pipeline

Reading Time: 4 minutes

Introduction

Dagger is an opensource devkit for continuous integration, deployment and delivery. It uses the power of BuildKit from docker and CUE to create platform independent deployment and integrations. And Jenkins is a automation server like teamcity, concourse, circle ci, github actions that creates CI/CD pipelines.

Prerequisites

  • Knowledge of Jenkins
  • Know to use basic dagger
  • understand CUE

Requirements

  • Jenkins running either locally or on cloud
  • GitHub Account
  • Docker daemon on Jenkins system or server
  • Dagger installed on system

Jenkins and Dagger Project

In this article, we would build and publish an image on my Docker Hub repository using dagger in a Jenkins pipeline. If you want to know way to publish docker image directly on Docker Hub only using Jenkins, then you can refer to this article on Knoldus blog.

Adding Credentials in Jenkins for Docker Hub

  1. Go to Credentials → Global → Add credentials .
  2. Fill out the form with your username and password.
  3. Give it an ID like DockerHub .
  4. Save .
Crediantials on Jenkins

Creating Dagger Script

In Dagger, we need a ‘.cue’ script file for performing the work . So we create a main.cue file with following script:

package daggerapp

import (
  "dagger.io/dagger"
  "universe.dagger.io/docker"
)

dagger.#Plan & {
  client: {
    env: {
      DH_CREDS_USR: string
      DH_CREDS_PSW: dagger.#Secret
    }
  }
  actions: {
    build: docker.#Build & {
      steps: [
        docker.#Pull & {
          source: "alpine"
        },
      ]      
    }
    push: docker.#Push & {
      auth: {
        username: client.env.DH_CREDS_USR
        secret: client.env.DH_CREDS_PSW
      }
      image: build.output
      dest: "vaibhavkuma779/jenkins-dagger:latest"
    }
  }
}

In above code, first we define the package name as done in many languages like Java or Scala . Next is the importing predefined packages or actions. we have imported dagger and docker. Dagger to perform dagger actions like dagger do as seen in Jenkinsfile. Docker to perform pull and push actions as seen in later part of script. Plan defines workflow of the dagger script. First we define the environment variables for credentials to push on Docker Hub repository. Username is directly defined a string while password is under #Secret of dagger.

Note: Since the Credentials are in Jenkins Server we are using the environment variables defined in the Jenkinsfile to extract them here to be used in push action.

Next we start the actions of the app under actions. We need to create an image so build action. We are not using Dockerfile but we are pulling an image from docker directly. #Pull and #Push defines dagger buildKit action or commands. like a Dockerfile we ca write every step under build action here. Next is push, first we provide authentication from the env variables. Now #Push requires image which sourced as output of build action. Similar to using tag in docker we are using “dest” to define repository name as image name to push on Docker Hub.

Creating Jenkinsfile

Now we will creating a Jenkinsfile :

pipeline {
  agent any
  environment {
    DH_CREDS=credentials('DockerHub')
  }
  stages {
    stage("setup") {
      steps {
        sh '''
          dagger project init
          dagger project update
        '''
      }
    }
    stage("do") {
      steps {
        sh 'dagger do push'
      }
    }    
  }
}

The Jenkinsfile is in Declarative form. We define agent as any. So stages run on any agent added to Jenkins server. Next we define the environment variable for Docker Hub . The variable holds two values: Username and Password. Dagger extracts them individually from Jenkins. Then use them in dagger script. Now we start with the stages . The first stage is setup. Every time you start a dagger workflow , there is requirement of dagger project is initialised. Then it is updated. This creates a cue.mod directory in the project with all predefined packages and actions that can be reused. After Setup is completed. Then is main stage for image creation and push. Notice, we have written only “dagger do push”. This is beauty of dagger that it would connect with previous required step of Build. And then do the Push action. It runs build action first the push.

Creating Jenkins Job

Now on Jenkins Server, we create a normal pipeline job in following way:

job creation

Then adding the github repository here in following way:

Github Repo

The APPLY and SAVE .

Now click on Build and go to console output. We may see similar output.

Note: if you get error of permission denied for docker. You can run following command on Terminal or Command Line on System of Jenkins.

sudo chmod 666 /var/run/docker.sock

Checkout and stage setup output:

stage1 output

Stage do output and SUCCESS

dagger do output

Review on DockerHub

We will go and see if it is pushed on dockerhub or not:

dockerhub

Conclusion

Using Dagger we able to build an image. So we were also able to push it on Docker Hub in a pipeline crated by Jenkins. Dagger is an automation tool for CI/CD and other DevOps or SRE related development. There is more to explore in uses of Dagger like doing same things with Github Actions or Circle CI.

References

  1. dagger.io
  2. jenkins.io
  3. https://github.com/vaibhavkumar779/jenkinsDagger
  4. https://docs.dagger.io/1201/ci-environment
knoldus

Written by 

Explorer of new technologies: from DevOps to Web 3

Leave a Reply