From 6fe49a02946f58f622cf891d9a6c6fd1f46c756a Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Thu, 17 Mar 2022 14:55:34 +0100 Subject: [PATCH] Improve go use case example according to Gerhard's review Signed-off-by: Vasek - Tom C --- .../ci-cd-for-go-project/base.cue.fragment | 14 +++ .../ci-cd-for-go-project/build.cue.fragment | 4 + .../ci-cd-for-go-project/build/dagger.cue | 32 ------- .../{push => complete-ci-cd}/dagger.cue | 23 +++-- .../ci-cd-for-go-project/image.cue.fragment | 22 +++++ .../ci-cd-for-go-project/push.cue.fragment | 14 +++ .../retrieve-go-project/dagger.cue | 18 ++++ .../ci-cd-for-go-project/test.cue.fragment | 6 ++ .../ci-cd-for-go-project/test/dagger.cue | 34 ------- docs/use-cases/1216-go-on-docker-hub.md | 90 +++++++++++++++---- 10 files changed, 167 insertions(+), 90 deletions(-) create mode 100644 docs/tests/use-cases/ci-cd-for-go-project/base.cue.fragment create mode 100644 docs/tests/use-cases/ci-cd-for-go-project/build.cue.fragment delete mode 100644 docs/tests/use-cases/ci-cd-for-go-project/build/dagger.cue rename docs/tests/use-cases/ci-cd-for-go-project/{push => complete-ci-cd}/dagger.cue (70%) create mode 100644 docs/tests/use-cases/ci-cd-for-go-project/image.cue.fragment create mode 100644 docs/tests/use-cases/ci-cd-for-go-project/push.cue.fragment create mode 100644 docs/tests/use-cases/ci-cd-for-go-project/retrieve-go-project/dagger.cue create mode 100644 docs/tests/use-cases/ci-cd-for-go-project/test.cue.fragment delete mode 100644 docs/tests/use-cases/ci-cd-for-go-project/test/dagger.cue diff --git a/docs/tests/use-cases/ci-cd-for-go-project/base.cue.fragment b/docs/tests/use-cases/ci-cd-for-go-project/base.cue.fragment new file mode 100644 index 00000000..1f722bb3 --- /dev/null +++ b/docs/tests/use-cases/ci-cd-for-go-project/base.cue.fragment @@ -0,0 +1,14 @@ +import ( + "dagger.io/dagger" + "universe.dagger.io/go" +) + +dagger.#Plan & { + actions: { + // Improve go base image with useful tool + // Enable cgo by installing build-base + _base: go.#Image & { + packages: "build-base": version: _ + } + } +} \ No newline at end of file diff --git a/docs/tests/use-cases/ci-cd-for-go-project/build.cue.fragment b/docs/tests/use-cases/ci-cd-for-go-project/build.cue.fragment new file mode 100644 index 00000000..7c69e1c5 --- /dev/null +++ b/docs/tests/use-cases/ci-cd-for-go-project/build.cue.fragment @@ -0,0 +1,4 @@ +// Build go project +build: go.#Build & { + source: _code +} diff --git a/docs/tests/use-cases/ci-cd-for-go-project/build/dagger.cue b/docs/tests/use-cases/ci-cd-for-go-project/build/dagger.cue deleted file mode 100644 index a105818a..00000000 --- a/docs/tests/use-cases/ci-cd-for-go-project/build/dagger.cue +++ /dev/null @@ -1,32 +0,0 @@ -package main - -import ( - "dagger.io/dagger" - "universe.dagger.io/go" -) - -dagger.#Plan & { - client: { - // Retrieve go source code - filesystem: ".": read: { - contents: dagger.#FS - include: ["go.mod", "go.sum", "**/*.go"] - } - } - - actions: { - // Alias to code directory - _code: client.filesystem.".".read.contents - - // Improved go base image with useful tool - // Enable cgo by installing build-base - _base: go.#Image & { - packages: "build-base": version: _ - } - - // Build go project - build: go.#Build & { - source: _code - } - } -} diff --git a/docs/tests/use-cases/ci-cd-for-go-project/push/dagger.cue b/docs/tests/use-cases/ci-cd-for-go-project/complete-ci-cd/dagger.cue similarity index 70% rename from docs/tests/use-cases/ci-cd-for-go-project/push/dagger.cue rename to docs/tests/use-cases/ci-cd-for-go-project/complete-ci-cd/dagger.cue index 97cf80ad..64867b47 100644 --- a/docs/tests/use-cases/ci-cd-for-go-project/push/dagger.cue +++ b/docs/tests/use-cases/ci-cd-for-go-project/complete-ci-cd/dagger.cue @@ -9,13 +9,11 @@ import ( dagger.#Plan & { client: { - // Retrieve go source code filesystem: ".": read: { contents: dagger.#FS include: ["go.mod", "go.sum", "**/*.go"] } - // Retrieve docker password from environment env: DOCKER_PASSWORD: dagger.#Secret } @@ -23,9 +21,20 @@ dagger.#Plan & { // Alias to code _code: client.filesystem.".".read.contents - // Improved go base image with useful tool + // Improve go base image with useful tool + // Enable cgo by installing build-base _base: go.#Image & { - packages: "build-base": version: _ + packages: { + "build-base": version: _ + bash: version: _ + } + } + + // Run go unit test + "unit-test": go.#Test & { + source: _code + package: "./..." + input: _base.output } // Build go project @@ -45,7 +54,7 @@ dagger.#Plan & { dest: "/usr/bin" }, docker.#Set & { - config: cmd: ["////"] + config: cmd: [""] }, ] } @@ -54,11 +63,11 @@ dagger.#Plan & { // Push image to remote registry (depends on image) push: { // Docker username - _dockerUsername: "" + _dockerUsername: "" docker.#Push & { "image": image.output - dest: "\(_dockerUsername)/:" + dest: "\(_dockerUsername)/" auth: { username: "\(_dockerUsername)" secret: client.env.DOCKER_PASSWORD diff --git a/docs/tests/use-cases/ci-cd-for-go-project/image.cue.fragment b/docs/tests/use-cases/ci-cd-for-go-project/image.cue.fragment new file mode 100644 index 00000000..b554eef3 --- /dev/null +++ b/docs/tests/use-cases/ci-cd-for-go-project/image.cue.fragment @@ -0,0 +1,22 @@ +import ( + "universe.dagger.io/alpine" + "universe.dagger.io/docker" +) + +// Build docker image (depends on build) +image: { + _base: alpine.#Build & {} + + docker.#Build & { + steps: [ + docker.#Copy & { + input: _base.output + contents: build.output + dest: "/usr/bin" + }, + docker.#Set & { + config: cmd: [""] + }, + ] + } +} \ No newline at end of file diff --git a/docs/tests/use-cases/ci-cd-for-go-project/push.cue.fragment b/docs/tests/use-cases/ci-cd-for-go-project/push.cue.fragment new file mode 100644 index 00000000..b742f0de --- /dev/null +++ b/docs/tests/use-cases/ci-cd-for-go-project/push.cue.fragment @@ -0,0 +1,14 @@ +// Push image to remote registry (depends on image) +push: { + // Docker username + _dockerUsername: "" + + docker.#Push & { + "image": image.output + dest: "\(_dockerUsername)/" + auth: { + username: "\(_dockerUsername)" + secret: client.env.DOCKER_PASSWORD + } + } +} \ No newline at end of file diff --git a/docs/tests/use-cases/ci-cd-for-go-project/retrieve-go-project/dagger.cue b/docs/tests/use-cases/ci-cd-for-go-project/retrieve-go-project/dagger.cue new file mode 100644 index 00000000..13483d79 --- /dev/null +++ b/docs/tests/use-cases/ci-cd-for-go-project/retrieve-go-project/dagger.cue @@ -0,0 +1,18 @@ +package main + +import ( + "dagger.io/dagger" +) + +dagger.#Plan & { + // Retrieve go source code + client: filesystem: ".": read: { + contents: dagger.#FS + include: ["go.mod", "go.sum", "**/*.go"] + } + + actions: { + // Alias to code directory + _code: client.filesystem.".".read.contents + } +} diff --git a/docs/tests/use-cases/ci-cd-for-go-project/test.cue.fragment b/docs/tests/use-cases/ci-cd-for-go-project/test.cue.fragment new file mode 100644 index 00000000..d1518765 --- /dev/null +++ b/docs/tests/use-cases/ci-cd-for-go-project/test.cue.fragment @@ -0,0 +1,6 @@ +// Run go unit test +"unit-test": go.#Test & { + source: _code + package: "./..." + input: _base.output +} diff --git a/docs/tests/use-cases/ci-cd-for-go-project/test/dagger.cue b/docs/tests/use-cases/ci-cd-for-go-project/test/dagger.cue deleted file mode 100644 index 0a8525a0..00000000 --- a/docs/tests/use-cases/ci-cd-for-go-project/test/dagger.cue +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "dagger.io/dagger" - "universe.dagger.io/go" -) - -dagger.#Plan & { - client: { - // Retrieve go source code - filesystem: ".": read: { - contents: dagger.#FS - include: ["go.mod", "go.sum", "**/*.go"] - } - } - - actions: { - // Alias to code directory - _code: client.filesystem.".".read.contents - - // Improved go base image with useful tool - // Enable cgo by installing build-base - _base: go.#Image & { - packages: "build-base": version: _ - } - - // Run go unit test - "unit-test": go.#Test & { - source: _code - package: "./..." - input: _base.output - } - } -} diff --git a/docs/use-cases/1216-go-on-docker-hub.md b/docs/use-cases/1216-go-on-docker-hub.md index ad4e29c2..44590cd2 100644 --- a/docs/use-cases/1216-go-on-docker-hub.md +++ b/docs/use-cases/1216-go-on-docker-hub.md @@ -14,48 +14,104 @@ to control the whole CI/CD process, from test to push into a remote registry. Following examples can be used as a template for any standalone go project. ::: -## Test +## Retrieve Go project -```cue file=../tests/use-cases/ci-cd-for-go-project/test/dagger.cue +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: + +```cue file=../tests/use-cases/ci-cd-for-go-project/retrieve-go-project/dagger.cue ``` -You can then run unit test in your go project with +:::tip +To make it more accessible in actions, you can set a private field that will +act as an alias. +::: -```shell -dagger do unit-test +## Build a Go base image + +[Dagger go universe](https://github.com/dagger/dagger/tree/main/pkg/universe.dagger.io/go) +provide 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. + +You can customize that base image to install required dependencies: + +```cue file=../tests/use-cases/ci-cd-for-go-project/base.cue.fragment ``` - +## Run unit test + +Before deliver your application, you certainly want to run unit test. + +By using previous steps, you can use the [test](https://github.com/dagger/dagger/blob/main/pkg/universe.dagger.io/go/test.cue) +definition to run your unit test: + +```cue file=../tests/use-cases/ci-cd-for-go-project/test.cue.fragment +``` + + :::tip You can also use dagger to write integration tests ::: -## Build +## Build Go binary -```cue file=../tests/use-cases/ci-cd-for-go-project/build/dagger.cue +To put your go project on docker hub, you first need to compile a binary. + +Go universe expose a [build](https://github.com/dagger/dagger/blob/main/pkg/universe.dagger.io/go/build.cue) +definition so you can build a binary: + +```cue file=../tests/use-cases/ci-cd-for-go-project/build.cue.fragment ``` :::tip You can control the binary platform with `os` and `arch` field. ::: -You can then build your binary with +## Prepare docker image -```shell -dagger do build +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: + +```cue file=../tests/use-cases/ci-cd-for-go-project/image.cue.fragment ``` -## Push +## Push to Docker Hub -```cue file=../tests/use-cases/ci-cd-for-go-project/push/dagger.cue +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: + +```cue +dagger.#Plan & { + client: { + // ... + + env: DOCKER_PASSWORD: dagger.#Secret + } +} ``` -You can then build and push your go project with +You can now push your image: -```shell -dagger do push +```cue file=../tests/use-cases/ci-cd-for-go-project/push.cue.fragment ``` +## Complete CI/CD + +After merging all examples, you will have a complete CI/CD to deliver a go +binary on Docker Hub. + +```cue file=../tests/use-cases/ci-cd-for-go-project/complete-ci-cd/dagger.cue +``` + +You can then use `dagger do` to select which action you want to run. + ## Push multi-platform -Coming soon +Coming soon...