This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
dagger/docs/getting-started/1200-local-dev.md
Gerhard Lazu d693742477
Europa docs: get started with CI/CD in local dev
The idea is to start simple and get users a good feel for how this works
within 5 minutes or less. We should cover the three popular OSes, and
ensure that everything works as expected.

At the end of this, users will have Dagger set up for local CI/CD, and
know how to make a change to the example app and re-run the build, test
& deploy loop.

This is part of https://github.com/dagger/dagger/issues/1327

Signed-off-by: Gerhard Lazu <gerhard@lazu.co.uk>
2022-03-05 23:58:49 +00:00

7.7 KiB

slug displayed_sidebar
/1200/local-dev europa

CI/CD in your local dev

Everyone should be able to run their CI/CD pipeline locally. Having to commit & push in order to test a change is a slow way to iterate on a pipeline. This guide shows you the Dagger way. Within 5 minutes, you will have a local CI/CD loop and run your first build, test & deploy pipeline.

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

<Tabs defaultValue="macos" groupId="os" values={[ {label: 'macOS', value: 'macos'}, {label: 'Linux', value: 'linux'}, {label: 'Windows', value: 'windows'}, ]}>

We assume that you have Homebrew installed. If you do, you can install dagger with a single command:

brew install dagger/tap/dagger

Check that dagger installed correctly by verifying its execution path:

type dagger
dagger is /opt/homebrew/bin/dagger

Before we can build, test & deploy our example app with dagger, we need to have Docker running. You most likely already have Docker set up. If not, Docker Desktop makes this easy. With Docker running, we are ready to download our example app and use its dev CI/CD pipeline:

git clone https://github.com/dagger/examples
cd examples/todoapp

With everything in place, we run the CI/CD pipeline locally:

dagger up dev.cue

With an empty cache, installing all dependencies, then building, testing & deploying this example app completes in just under 3 minutes:

[] inputs.directories.app                                        0.3s
[] actions.test                                                125.6s
[] actions.build                                               163.0s
[+] actions.deploy                                              167.5s
#18 INFO: System: Ran is running on HTTP port 8020
#18 INFO: System: Listening on http://0.0.0.0:8020

:::caution localhost:8020 is not accessible on macOS 12 & Docker 20.10.12. Works fine on Linux 🤷 :::

We can now access the application on localhost:8020 and get a preview of what the app would look like if the same thing ran in a CI environment.

While this is a good first step, it gets better when we run this again - the cache makes it quicker. Type ^C to exit the deployment, and run dagger up dev.cue again:

[] inputs.directories.app                                        0.1s
[] actions.build                                                 0.6s
[] actions.test                                                  0.6s
[+] actions.deploy                                                1.1s
#18 INFO: System: Ran is running on HTTP port 8020
#18 INFO: System: Listening on http://0.0.0.0:8020

Now that we have everything running locally, let us make a change and get a feel for our local CI/CD loop. The quicker we can close this loop, the quicker we can learn what actually works.

In the todoapp dir, edit line 25 of src/components/Form.js and save the file.

I change this line to What must be done today? and run build, test & deploy again:

dagger up dev.cue

[] inputs.directories.app                                        7.5s
[] actions.build                                                94.7s
[] actions.test                                                 57.3s
[+] actions.deploy                                               96.1s
#18 INFO: System: Ran is running on HTTP port 8020
#18 INFO: System: Listening on http://0.0.0.0:8020

The total 94.7s time is macOS specific, since the Linux alternative is more than 5x quicker. Either way, this local build, test & deploy loop is likely to change the approach to iterating on changes. It becomes even more obvious when the change is not as straightforward as knowing exactly which line to edit.

Download the latest 64bit Linux binary release. We support both x86 & ARM architectures.

We install dagger in /usr/local/bin, but anywhere in your PATH works:

type dagger
dagger is /usr/local/bin/dagger

Before we can build, test & deploy our example app with dagger, we need to have Docker Engine running. You most likely already have Docker Engine set up. If not, Docker on Linux install makes this easy. With Docker Engine running, we are ready to download our example app and use its dev CI/CD pipeline:

git clone https://github.com/dagger/examples
cd examples/todoapp

With everything in place, we run the CI/CD pipeline locally:

dagger up dev.cue

With an empty cache, installing all dependencies, then building, testing & deploying this example app completes in just under 1 minute:

[] inputs.directories.app                                        0.3s
[] actions.test                                                 45.1s
[] actions.build                                                53.8s
[+] actions.deploy                                               57.5s
#18 INFO: System: Ran is running on HTTP port 8020
#18 INFO: System: Listening on http://0.0.0.0:8020

We can now access the application on localhost:8020 and get a preview of what the app would look like if the same thing ran in a CI environment.

While this is a good first step, it gets better when we run this again - the cache makes it quicker. Type ^C to exit the deployment, and run dagger up dev.cue again:

[] inputs.directories.app                                        0.1s
[] actions.build                                                 1.7s
[] actions.test                                                  1.8s
[+] actions.deploy                                                2.1s
#18 INFO: System: Ran is running on HTTP port 8020
#18 INFO: System: Listening on http://0.0.0.0:8020

Now that we have everything running locally, let us make a change and get a feel for our local CI/CD loop. The quicker we can close this loop, the quicker we can learn what actually works.

In the todoapp dir, edit line 25 of src/components/Form.js and save the file.

I change this line to What must be done today? and run build, test & deploy again:

dagger up dev.cue

[] inputs.directories.app                                        0.1s
[] actions.build                                                24.7s
[] actions.test                                                 16.2s
[+] actions.deploy                                               17.1s
#18 INFO: System: Ran is running on HTTP port 8020
#18 INFO: System: Listening on http://0.0.0.0:8020

Being able to re-run a build, test & deploy loop locally in 17.1s is likely to change the approach to iterating on changes. It becomes even more obvious when the change is not as straightforward as knowing exactly which line to edit.

We assume that you have curl installed. If you do, you can install dagger with a single command. From a powershell terminal, run the following command:

curl https://releases.dagger.io/dagger/install.ps1 -OutFile install.ps1 ; ./install.ps1; rm install.ps1

We try to move the dagger binary under C:\Windows\System32 but in case we miss the necessary permissions, we'll save everything under <your home folder>/dagger

Check that dagger is installed correctly by opening a command prompt and run:

where dagger
C:\<your home folder>\dagger.exe

:::tip Now that we are comfortable with our local CI/CD loop, let us configure a remote CI environment in the second part. Dagger makes this easy. :::