From 2d3acc61e0292fef4e9b706a52fb47e700f3bd55 Mon Sep 17 00:00:00 2001 From: Tom Chauveau Date: Tue, 15 Mar 2022 14:32:26 +0100 Subject: [PATCH] Add dagger golang use case Signed-off-by: Vasek - Tom C --- .../ci-cd-for-go-project/build/dagger.cue | 32 +++++++++ .../ci-cd-for-go-project/push/dagger.cue | 69 +++++++++++++++++++ .../ci-cd-for-go-project/test/dagger.cue | 34 +++++++++ docs/use-cases/1216-go-on-docker-hub.md | 61 ++++++++++++++++ website/sidebars.js | 9 +++ 5 files changed, 205 insertions(+) create mode 100644 docs/tests/use-cases/ci-cd-for-go-project/build/dagger.cue create mode 100644 docs/tests/use-cases/ci-cd-for-go-project/push/dagger.cue create mode 100644 docs/tests/use-cases/ci-cd-for-go-project/test/dagger.cue create mode 100644 docs/use-cases/1216-go-on-docker-hub.md 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 new file mode 100644 index 00000000..a105818a --- /dev/null +++ b/docs/tests/use-cases/ci-cd-for-go-project/build/dagger.cue @@ -0,0 +1,32 @@ +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/push/dagger.cue new file mode 100644 index 00000000..97cf80ad --- /dev/null +++ b/docs/tests/use-cases/ci-cd-for-go-project/push/dagger.cue @@ -0,0 +1,69 @@ +package main + +import ( + "dagger.io/dagger" + "universe.dagger.io/go" + "universe.dagger.io/docker" + "universe.dagger.io/alpine" +) + +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 + } + + actions: { + // Alias to code + _code: client.filesystem.".".read.contents + + // Improved go base image with useful tool + _base: go.#Image & { + packages: "build-base": version: _ + } + + // Build go project + build: go.#Build & { + source: _code + } + + // 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: ["////"] + }, + ] + } + } + + // 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 + } + } + } + } +} 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 new file mode 100644 index 00000000..0a8525a0 --- /dev/null +++ b/docs/tests/use-cases/ci-cd-for-go-project/test/dagger.cue @@ -0,0 +1,34 @@ +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 new file mode 100644 index 00000000..ad4e29c2 --- /dev/null +++ b/docs/use-cases/1216-go-on-docker-hub.md @@ -0,0 +1,61 @@ +--- +slug: /1216/ci-cd-for-go-project +displayed_sidebar: 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](https://github.com/dagger/dagger/tree/main/pkg/universe.dagger.io/go) +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. +::: + +## Test + +```cue file=../tests/use-cases/ci-cd-for-go-project/test/dagger.cue +``` + +You can then run unit test in your go project with + +```shell +dagger do unit-test +``` + + +:::tip +You can also use dagger to write integration tests +::: + +## Build + +```cue file=../tests/use-cases/ci-cd-for-go-project/build/dagger.cue +``` + +:::tip +You can control the binary platform with `os` and `arch` field. +::: + +You can then build your binary with + +```shell +dagger do build +``` + +## Push + +```cue file=../tests/use-cases/ci-cd-for-go-project/push/dagger.cue +``` + +You can then build and push your go project with + +```shell +dagger do push +``` + +## Push multi-platform + +Coming soon diff --git a/website/sidebars.js b/website/sidebars.js index 3cbc97c4..1a0be921 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -101,6 +101,15 @@ module.exports = { "use-cases/go-docker-swarm", ], }, + { + type: "category", + label: "Use Cases", + collapsible: false, + collapsed: false, + items: [ + "use-cases/go-on-docker-hub" + ], + }, { type: "link", label: "⬅️ Dagger 0.1",