2022-03-15 14:32:26 +01:00
|
|
|
---
|
2022-03-30 01:27:33 +02:00
|
|
|
slug: /1219/go-docker-hub
|
2022-04-13 15:58:51 +02:00
|
|
|
displayed_sidebar: '0.2'
|
2022-03-15 14:32:26 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
# Go on Docker Hub
|
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
Dagger stands as a powerful CI/CD tool that works on any environment.
|
2022-03-15 14:32:26 +01:00
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
For instance, you can use the [Dagger Go package](https://github.com/dagger/dagger/tree/main/pkg/universe.dagger.io/go)
|
|
|
|
to control the whole CI/CD process, from testing to pushing into a remote registry.
|
2022-03-15 14:32:26 +01:00
|
|
|
|
|
|
|
:::tip
|
2022-03-30 14:10:14 +02:00
|
|
|
The following examples can be used as a template for any standalone Go project.
|
2022-03-15 14:32:26 +01:00
|
|
|
:::
|
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
## Retrieve Go project
|
2022-03-15 14:32:26 +01:00
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
The first step is to make your Go project accessible to the Dagger plan.
|
2022-03-17 14:55:34 +01:00
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
You can indeed choose which files to include. Since it's a Golang project
|
|
|
|
it should contain the module and all Go source files:
|
2022-03-17 14:55:34 +01:00
|
|
|
|
|
|
|
```cue file=../tests/use-cases/ci-cd-for-go-project/retrieve-go-project/dagger.cue
|
2022-04-13 15:58:51 +02:00
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
:::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
|
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
The [universe.dagger.io/go](https://github.com/dagger/dagger/tree/main/pkg/universe.dagger.io/go)
|
|
|
|
package provides a [base image](https://github.com/dagger/dagger/blob/main/pkg/universe.dagger.io/go/image.cue)
|
|
|
|
to build your pipeline, but your project may use `CGO` or any external dependencies.
|
2022-03-17 14:55:34 +01:00
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
You can customize the base image to install required dependencies:
|
2022-03-17 14:55:34 +01:00
|
|
|
|
|
|
|
```cue file=../tests/use-cases/ci-cd-for-go-project/base.cue.fragment
|
2022-04-13 15:58:51 +02:00
|
|
|
|
2022-03-15 14:32:26 +01:00
|
|
|
```
|
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
## Run unit tests
|
2022-03-17 14:55:34 +01:00
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
Before delivering your application, you certainly want to run unit tests.
|
2022-03-17 14:55:34 +01:00
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
Use the [#Test](https://github.com/dagger/dagger/blob/main/pkg/universe.dagger.io/go/test.cue)
|
|
|
|
definition:
|
2022-03-15 14:32:26 +01:00
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
```cue file=../tests/use-cases/ci-cd-for-go-project/test.cue.fragment
|
2022-04-13 15:58:51 +02:00
|
|
|
|
2022-03-15 14:32:26 +01:00
|
|
|
```
|
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
<!-- FIXME(TomChv): we should write a bunch of documentation about TDD with dagger -->
|
2022-04-13 15:58:51 +02:00
|
|
|
|
2022-03-15 14:32:26 +01:00
|
|
|
:::tip
|
2022-03-30 14:10:14 +02:00
|
|
|
You can also use Dagger to write integration tests.
|
2022-03-15 14:32:26 +01:00
|
|
|
:::
|
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
## Build Go binary
|
2022-03-15 14:32:26 +01:00
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
To put your Go project on Docker Hub, you first need to compile a binary.
|
2022-03-17 14:55:34 +01:00
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
Use the [#Build](https://github.com/dagger/dagger/blob/main/pkg/universe.dagger.io/go/build.cue)
|
|
|
|
definition to do that:
|
2022-03-17 14:55:34 +01:00
|
|
|
|
|
|
|
```cue file=../tests/use-cases/ci-cd-for-go-project/build.cue.fragment
|
2022-04-13 15:58:51 +02:00
|
|
|
|
2022-03-15 14:32:26 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
:::tip
|
2022-03-30 14:10:14 +02:00
|
|
|
You can control the binary platform with `os` and `arch` fields.
|
2022-03-15 14:32:26 +01:00
|
|
|
:::
|
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
## Prepare docker image
|
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
To make it usable for other users, you must put your binary in an image and set an entrypoint.
|
2022-03-17 14:55:34 +01:00
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
For optimization purposes, you can use alpine as the base image to contain your binary:
|
2022-03-17 14:55:34 +01:00
|
|
|
|
|
|
|
```cue file=../tests/use-cases/ci-cd-for-go-project/image.cue.fragment
|
2022-04-13 15:58:51 +02:00
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
## Push to Docker Hub
|
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
To push an image to Docker Hub, you will need your private credentials.
|
2022-03-17 14:55:34 +01:00
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
To not hard code your docker password in the plan, you can retrieve it
|
|
|
|
from your environment:
|
2022-03-15 14:32:26 +01:00
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
```cue
|
|
|
|
dagger.#Plan & {
|
|
|
|
client: {
|
|
|
|
// ...
|
|
|
|
|
|
|
|
env: DOCKER_PASSWORD: dagger.#Secret
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 14:32:26 +01:00
|
|
|
```
|
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
You can now push your image:
|
2022-03-15 14:32:26 +01:00
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
```cue file=../tests/use-cases/ci-cd-for-go-project/push.cue.fragment
|
2022-04-13 15:58:51 +02:00
|
|
|
|
2022-03-15 14:32:26 +01:00
|
|
|
```
|
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
## Complete CI/CD
|
|
|
|
|
2022-03-30 14:10:14 +02:00
|
|
|
After merging all examples, you will have a complete CI/CD to deliver a Go
|
2022-03-17 14:55:34 +01:00
|
|
|
binary on Docker Hub.
|
2022-03-15 14:32:26 +01:00
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
```cue file=../tests/use-cases/ci-cd-for-go-project/complete-ci-cd/dagger.cue
|
2022-04-13 15:58:51 +02:00
|
|
|
|
2022-03-15 14:32:26 +01:00
|
|
|
```
|
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
You can then use `dagger do` to select which action you want to run.
|
|
|
|
|
2022-03-15 14:32:26 +01:00
|
|
|
## Push multi-platform
|
|
|
|
|
2022-03-17 14:55:34 +01:00
|
|
|
Coming soon...
|