diff --git a/docs/guides/1228-go-ci.md b/docs/guides/1228-go-ci.md new file mode 100644 index 00000000..2ee0d624 --- /dev/null +++ b/docs/guides/1228-go-ci.md @@ -0,0 +1,33 @@ +--- +slug: /1227/go-ci +displayed_sidebar: europa +--- + +# Go project CI + +We want to run some tests and build a Go project with the `go` cue package. + +## Plan + +The plan consist of 2 actions: + +- `test` for `go test` +- `build` for `go build` + +both use the [Go dagger package](https://github.com/dagger/dagger/tree/main/pkg/universe.dagger.io/go) + +```cue file=../plans/go-ci/plan.cue +``` + +## Go project + +The project is a simple program that takes `$NAME` environment variable, use the [Go greeting package](https://github.com/dagger/dagger/tree/main/docs/plans/go-ci/hello/greeting) to print the `greeting.Greet()` out. + +```go file=../plans/go-ci/hello/main.go +``` + +```go file=../plans/go-ci/hello/greeting/greeting.go +``` + +```go file=../plans/go-ci/hello/greeting/greeting_test.go +``` diff --git a/docs/plans/go-ci/hello/go.mod b/docs/plans/go-ci/hello/go.mod new file mode 100644 index 00000000..58103bd2 --- /dev/null +++ b/docs/plans/go-ci/hello/go.mod @@ -0,0 +1,3 @@ +module dagger.io/testgreetci + +go 1.17 diff --git a/docs/plans/go-ci/hello/greeting/greeting.go b/docs/plans/go-ci/hello/greeting/greeting.go new file mode 100644 index 00000000..b830aabf --- /dev/null +++ b/docs/plans/go-ci/hello/greeting/greeting.go @@ -0,0 +1,7 @@ +package greeting + +import "fmt" + +func Greeting(name string) string { + return fmt.Sprintf("Hi %s!", name) +} diff --git a/docs/plans/go-ci/hello/greeting/greeting_test.go b/docs/plans/go-ci/hello/greeting/greeting_test.go new file mode 100644 index 00000000..f2f2d812 --- /dev/null +++ b/docs/plans/go-ci/hello/greeting/greeting_test.go @@ -0,0 +1,13 @@ +package greeting + +import "testing" + +func TestGreeting(t *testing.T) { + name := "Dagger Test" + expect := "Hi Dagger Test!" + value := Greeting(name) + + if expect != value { + t.Fatalf("Hello(%s) = '%s', expected '%s'", name, value, expect) + } +} diff --git a/docs/plans/go-ci/hello/main.go b/docs/plans/go-ci/hello/main.go new file mode 100644 index 00000000..a18f5487 --- /dev/null +++ b/docs/plans/go-ci/hello/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "os" + + "dagger.io/testgreetci/greeting" +) + +func main() { + name := os.Getenv("NAME") + if name == "" { + name = "John Doe" + } + fmt.Printf(greeting.Greeting(name)) +} diff --git a/docs/plans/go-ci/plan.cue b/docs/plans/go-ci/plan.cue new file mode 100644 index 00000000..25c394ce --- /dev/null +++ b/docs/plans/go-ci/plan.cue @@ -0,0 +1,24 @@ +package main + +import ( + "dagger.io/dagger" + + "universe.dagger.io/go" + //"universe.dagger.io/bash" +) + +dagger.#Plan & { + client: filesystem: "./hello": read: contents: dagger.#FS + + actions: { + test: go.#Test & { + source: client.filesystem."./hello".read.contents + package: "./..." + } + + build: go.#Build & { + source: client.filesystem."./hello".read.contents + } + } + +}