Dagger Introduction
Dagger is a CI/CD tool co-founded by Solomon Hykes, who is also a co-founder of Docker.
After leaving Docker in 2018, Hykes launched Dagger in 2022 with the aim of revolutionizing CI/CD pipelines by providing a more flexible and programmable solution.
Dagger allows developers to define pipelines using programming languages such as Go, Python, or TypeScript, instead of static configuration files.
This approach simplifies the creation of portable and modular pipelines suited for both local and production environments.
As you might have guessed, no more plugins for GitHub Actions to run locally. Here’s another advantage:
Developers can now write their pipelines in their favorite languages and build in a beta environment identical to what runs on their machines.
Installation
There are several installation methods. You can find the quickstart guide here: Dagger Quickstart.
However, I also recommend Stéphane Robert’s method, which is simple and fast: Stéphane Robert - Dagger.
Here is Stéphane Robert’s method for Linux:
And for macOS:
On Stéphane Robert’s blog, you can find an example in Python. Here, we’ll use TypeScript!
I’ll use this project as an example—a small Express API (GitHub Repository).
Initialization
First, I’ll clone the project:
Now let’s initialize Dagger:
We’ve asked it to initialize with the TypeScript SDK and place all necessary files in the ./dagger
directory.
In the dagger
directory, there’s a src
folder containing index.ts
, which will serve as our CI/CD script.
We’ll replace the content of index.ts
with the following code:
The publish
function temporarily pushes the container to the ttl.sh
registry for testing.
As shown, we’re creating a Node.js container, copying files from our directory to /src
, mounting the Node.js cache, changing the working directory, running npm install
, setting the container’s entry point, and exposing port 8080.
To avoid copying node_modules
, modify the dagger.json
file at the root of the project and add this key:
Important!!!
I wasted an hour on a simple detail…the class
name must match your project name.
No hyphens allowed. For example, if your project is node-api-demo
, the class should be NodeApiDemo
.
Finally, modify the package.json
file in dagger/src
to add your package manager:
Great, everything should now be ready.
Building the Pipeline
Shall we run the pipeline?
During execution, you’ll see a few available commands:
w
: Open the pipeline view in Dagger Cloud.+/-
: Adjust log detail.q
: Quit the pipeline.
If you encounter an issue, try expanding the logs or running:
But here, everything went smoothly, and here’s the result:
And there you have it! The pipeline successfully pushed our Docker image to the ttl.sh
registry, which we can now run directly.
I’ll run it with docker run
:
Our container is responding properly!
We can now integrate this CI pipeline into GitHub Actions or deploy it locally.
Details
A few concepts I forgot to mention:
call
: Thecall
argument is used to invoke a specific function in the pipeline.- Execution order: Dagger automatically organizes pipelines based on their dependencies.
--source
: Specifies the project directory, allowing the function to retrieve values.
Conclusion
I understand that for now, this pipeline isn’t very complex, but the goal was to explore this tool.
Soon, I’ll demonstrate a project using Bun and then move on to more extensive pipelines resembling production environments.