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/use-cases/1216-go-on-docker-hub.md
Tom Chauveau 6fe49a0294
Improve go use case example according to Gerhard's review
Signed-off-by: Vasek - Tom C <tom.chauveau@epitech.eu>
2022-03-17 17:28:38 +01:00

3.1 KiB

slug displayed_sidebar
/1216/ci-cd-for-go-project europa

Go on Docker Hub

Dagger stand as a powerful CI/CD tool that works on any environment.

For instance, you can use go package to control the whole CI/CD process, from test to push into a remote registry.

:::tip Following examples can be used as a template for any standalone go project. :::

Retrieve Go project

First important step is to make go project accessible in dagger plan.

You can indeed choose which files to include in the filesystem.
Since it's a Golang project, filesystem should contain module and every go source files:

:::tip To make it more accessible in actions, you can set a private field that will act as an alias. :::

Build a Go base image

Dagger go universe provide a base image to build your pipeline but your project may use CGO or any external dependencies.

You can customize that base image to install required dependencies:

Run unit test

Before deliver your application, you certainly want to run unit test.

By using previous steps, you can use the test definition to run your unit test:

:::tip You can also use dagger to write integration tests :::

Build Go binary

To put your go project on docker hub, you first need to compile a binary.

Go universe expose a build definition so you can build a binary:

:::tip You can control the binary platform with os and arch field. :::

Prepare docker image

To make it usable by other user, you must put your binary in an image and set an entrypoint.

For optimisation purpose, you can use alpine as base image to contain your binary:

Push to Docker Hub

To push an image to docker hub, you will need to forward credential to allow dagger push.

To not hard code your docker password in the plan, you can retrieve it as an environment value:

dagger.#Plan & {
    client: {
        // ...

        env: DOCKER_PASSWORD: dagger.#Secret
    }
}

You can now push your image:

Complete CI/CD

After merging all examples, you will have a complete CI/CD to deliver a go binary on Docker Hub.

You can then use dagger do to select which action you want to run.

Push multi-platform

Coming soon...