Skip to content

Instantly share code, notes, and snippets.

@sheldonhull
Last active May 17, 2023 17:14
Show Gist options
  • Save sheldonhull/436136240c417d7c5c01117f75d0469a to your computer and use it in GitHub Desktop.
Save sheldonhull/436136240c417d7c5c01117f75d0469a to your computer and use it in GitHub Desktop.
use goreleaser-cross with dagger
package main
import (
"context"
"os"
"path/filepath"
"runtime"
"dsv-engine/magefiles/constants"
"dagger.io/dagger"
"github.com/pterm/pterm"
)
// For Dagger & Goreleaser Setup
const (
// GoCrossCompileVersion is the version of go to use for cross compiling.
GoCrossCompileVersion = "v1.19.5"
// GoreleaserDockerImage is the docker image to use for goreleaser.
GoreleaserDockerImage = "ghcr.io/goreleaser/goreleaser-cross"
// ArtifactDirectory is a directory containing artifacts for the project and shouldn't be committed to source.
ArtifactDirectory = ".artifacts"
// PermissionUserReadWriteExecute is the permissions for the artifact directory.
PermissionUserReadWriteExecute = 0o0700
// CacheDirectory is where the cache for the project is placed, ie artifacts that don't need to be rebuilt often.
CacheDirectory = ".cache"
)
// TargetBuildDirectory is the path to the build directory that dagger will output to.
var TargetBuildDirectory = filepath.Join(ArtifactDirectory, "goreleaser")
// ๐Ÿ”จ invokeGoreleaserViaDagger builds the service using Dagger and goreleaser-cross for support for CGO_ENABLED based builds.
//
// Development notes: This is a fully containerized build, using Dagger. Requires Docker.
func invokeGoreleaserViaDagger(envvars map[string]string, args ...string) error {
if len(args) == 0 {
return nil
}
workingDirectory, err := os.Getwd()
if err != nil {
return err
}
homedir, err := os.UserHomeDir()
if err != nil {
return err
}
ctx := context.Background()
pterm.DefaultHeader.Println("Building with Dagger")
dockerConfigDirectory := filepath.Join(homedir, ".docker")
dockerSecret := &dagger.Secret{}
if _, err := os.Stat(dockerConfigDirectory); err == nil {
pterm.Info.Printfln("found docker config file: %s", dockerConfigDirectory)
} else if os.IsNotExist(err) {
pterm.Warning.Printfln("docker config file not found: %s", dockerConfigDirectory)
} else {
pterm.Error.Printfln("error checking for docker config file: %s", dockerConfigDirectory)
return err
}
// make the target directory, to ensure docker doesn't, otherwise will be under root
if err := os.MkdirAll(constants.TargetBuildDirectory, constants.PermissionUserReadWriteExecute); err != nil {
return err
}
// initialize Dagger client
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
return err
}
defer client.Close()
dockerSecret = client.Host().Directory(dockerConfigDirectory).File("config.json").Secret()
// get reference to the local project
src := client.Host().Directory(workingDirectory)
cachedBuild := client.CacheVolume("go-build-cache")
cachedMod := client.CacheVolume("go-mod-cache")
modcache := "/nonroot/.cache/go-mod-cache"
buildcache := "/nonroot/.cache/go-build-cache"
golang := client.Container().From(constants.GoreleaserDockerImage).
// WithEnvVariable("CGO_ENABLED", "0").
WithEnvVariable("GOOS", runtime.GOOS).
WithEnvVariable("GOARCH", runtime.GOARCH).
WithEnvVariable("GOMODCACHE", modcache).
WithEnvVariable("GOCACHE", buildcache)
golang = golang.WithMountedDirectory("/go/src/", src).
WithMountedSecret("/root/.docker/config.json", dockerSecret). // using dagger secrets api
WithWorkdir("/src").
WithMountedCache(modcache, cachedMod).
WithMountedCache(buildcache, cachedBuild).
WithUnixSocket("/var/run/docker.sock", client.Host().UnixSocket("/var/run/docker.sock")).
WithWorkdir("/go/src/")
// run goreleaser with the args passed in
golang.WithExec(args)
// get reference to build output directory in container
output := golang.Directory(TargetBuildDirectory) // ๐Ÿ‘ˆ๐Ÿ‘ˆ๐Ÿ‘ˆ๐Ÿ‘ˆ NOT WORKING YET: /go/src/.artifacts/goreleaser: no such file or directory
// write contents of container build/ directory to the host
_, err = output.Export(ctx, constants.TargetBuildDirectory)
if err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment