2021-02-19 10:25:41 +01:00
|
|
|
package go
|
|
|
|
|
|
|
|
import (
|
|
|
|
"dagger.io/dagger"
|
2021-04-06 02:27:51 +02:00
|
|
|
"dagger.io/dagger/op"
|
2021-05-25 02:54:04 +02:00
|
|
|
"dagger.io/docker"
|
2021-05-01 03:04:49 +02:00
|
|
|
|
2021-05-06 08:53:04 +02:00
|
|
|
"dagger.io/os"
|
2021-02-19 10:25:41 +01:00
|
|
|
)
|
|
|
|
|
2021-05-01 03:04:49 +02:00
|
|
|
// A standalone go environment
|
|
|
|
#Container: {
|
|
|
|
// Go version to use
|
|
|
|
version: *"1.16" | string
|
|
|
|
source: dagger.#Artifact
|
|
|
|
|
2021-05-06 08:53:04 +02:00
|
|
|
os.#Container & {
|
2021-05-25 02:54:04 +02:00
|
|
|
env: CGO_ENABLED: "0"
|
|
|
|
|
|
|
|
image: docker.#Pull & {
|
|
|
|
from: "docker.io/golang:\(version)-alpine"
|
2021-05-01 03:04:49 +02:00
|
|
|
}
|
|
|
|
|
2021-05-25 02:54:04 +02:00
|
|
|
// Setup source dir
|
|
|
|
let srcPath = "/src"
|
|
|
|
mount: "\(srcPath)": from: source
|
|
|
|
dir: srcPath
|
2021-05-06 08:53:04 +02:00
|
|
|
|
2021-05-25 02:54:04 +02:00
|
|
|
// Setup go cache
|
|
|
|
let cachePath = "/root/.cache/gocache"
|
|
|
|
cache: "\(cachePath)": true
|
|
|
|
env: GOMODCACHE: cachePath
|
2021-05-01 03:04:49 +02:00
|
|
|
|
|
|
|
// Add go to search path (FIXME: should be inherited from image metadata)
|
|
|
|
shell: search: "/usr/local/go/bin": true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-19 10:25:41 +01:00
|
|
|
#Go: {
|
|
|
|
// Go version to use
|
|
|
|
version: *"1.16" | string
|
|
|
|
|
|
|
|
// Arguments to the Go binary
|
|
|
|
args: [...string]
|
|
|
|
|
|
|
|
// Source Directory to build
|
2021-03-13 07:29:37 +01:00
|
|
|
source: dagger.#Artifact
|
2021-02-19 10:25:41 +01:00
|
|
|
|
|
|
|
// Environment variables
|
|
|
|
env: [string]: string
|
|
|
|
|
2021-04-03 00:00:30 +02:00
|
|
|
#up: [
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#FetchContainer & {
|
2021-02-19 10:25:41 +01:00
|
|
|
ref: "docker.io/golang:\(version)-alpine"
|
|
|
|
},
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#Exec & {
|
2021-02-19 10:25:41 +01:00
|
|
|
"args": ["go"] + args
|
|
|
|
|
2021-02-25 23:17:01 +01:00
|
|
|
"env": env
|
|
|
|
"env": CGO_ENABLED: "0"
|
2021-04-05 23:31:24 +02:00
|
|
|
"env": GOMODCACHE: "/root/.cache/gocache"
|
2021-02-19 10:25:41 +01:00
|
|
|
|
2021-02-19 23:35:01 +01:00
|
|
|
dir: "/src"
|
|
|
|
mount: "/src": from: source
|
|
|
|
|
2021-02-19 10:25:41 +01:00
|
|
|
mount: "/root/.cache": "cache"
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
#Build: {
|
|
|
|
// Go version to use
|
|
|
|
version: *#Go.version | string
|
|
|
|
|
|
|
|
// Source Directory to build
|
2021-03-13 07:29:37 +01:00
|
|
|
source: dagger.#Artifact
|
2021-02-19 10:25:41 +01:00
|
|
|
|
|
|
|
// Packages to build
|
|
|
|
packages: *"." | string
|
|
|
|
|
|
|
|
// Target architecture
|
|
|
|
arch: *"amd64" | string
|
|
|
|
|
|
|
|
// Target OS
|
|
|
|
os: *"linux" | string
|
|
|
|
|
|
|
|
// Build tags to use for building
|
|
|
|
tags: *"" | string
|
|
|
|
|
|
|
|
// LDFLAGS to use for linking
|
|
|
|
ldflags: *"" | string
|
|
|
|
|
|
|
|
// Specify the targeted binary name
|
|
|
|
output: string
|
|
|
|
|
|
|
|
env: [string]: string
|
|
|
|
|
2021-04-03 00:00:30 +02:00
|
|
|
#up: [
|
2021-04-06 02:27:51 +02:00
|
|
|
op.#Copy & {
|
2021-02-19 10:25:41 +01:00
|
|
|
from: #Go & {
|
|
|
|
"version": version
|
|
|
|
"source": source
|
|
|
|
"env": env
|
|
|
|
args: ["build", "-v", "-tags", tags, "-ldflags", ldflags, "-o", output, packages]
|
|
|
|
}
|
|
|
|
src: output
|
|
|
|
dest: output
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
#Test: {
|
|
|
|
// Go version to use
|
|
|
|
version: *#Go.version | string
|
|
|
|
|
|
|
|
// Source Directory to build
|
2021-03-13 07:29:37 +01:00
|
|
|
source: dagger.#Artifact
|
2021-02-19 10:25:41 +01:00
|
|
|
|
|
|
|
// Packages to test
|
|
|
|
packages: *"." | string
|
|
|
|
|
|
|
|
#Go & {
|
|
|
|
"version": version
|
|
|
|
"source": source
|
|
|
|
args: ["test", "-v", packages]
|
|
|
|
}
|
|
|
|
}
|