Skip to content

Commit

Permalink
Share the same client when running multiple targets via All
Browse files Browse the repository at this point in the history
Signed-off-by: Gerhard Lazu <[email protected]>
  • Loading branch information
gerhard committed Aug 15, 2023
1 parent 2d39080 commit da6d7de
Showing 1 changed file with 30 additions and 14 deletions.
44 changes: 30 additions & 14 deletions magefiles/dagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ const (

// golangci-lint
func Lint(ctx context.Context) {
c := daggerClient(ctx)
defer c.Close()
c, ok := ctx.Value("daggerClient").(*dagger.Client)
if !ok {
c := newDaggerClient(ctx)
defer c.Close()
}

lint(ctx, c)
}
Expand All @@ -68,8 +71,11 @@ func lint(ctx context.Context, c *dagger.Client) {

// go test
func Test(ctx context.Context) {
c := daggerClient(ctx)
defer c.Close()
c, ok := ctx.Value("daggerClient").(*dagger.Client)
if !ok {
c := newDaggerClient(ctx)
defer c.Close()
}

test(ctx, c)
}
Expand All @@ -91,8 +97,11 @@ func test(ctx context.Context, c *dagger.Client) {

// binary artefact used in container image
func Build(ctx context.Context) {
c := daggerClient(ctx)
defer c.Close()
c, ok := ctx.Value("daggerClient").(*dagger.Client)
if !ok {
c := newDaggerClient(ctx)
defer c.Close()
}

build(ctx, c)
}
Expand Down Expand Up @@ -128,8 +137,11 @@ func sourceCode(c *dagger.Client) *dagger.Directory {

// container image to private registry
func Publish(ctx context.Context) {
c := daggerClient(ctx)
defer c.Close()
c, ok := ctx.Value("daggerClient").(*dagger.Client)
if !ok {
c := newDaggerClient(ctx)
defer c.Close()
}

publish(ctx, c)
}
Expand All @@ -149,8 +161,11 @@ func publish(ctx context.Context, c *dagger.Client) string {

// zero-downtime deploy container image
func Deploy(ctx context.Context) {
c := daggerClient(ctx)
defer c.Close()
c, ok := ctx.Value("daggerClient").(*dagger.Client)
if !ok {
c := newDaggerClient(ctx)
defer c.Close()
}

imageRef := os.Getenv("IMAGE_REF")
if imageRef == "" {
Expand Down Expand Up @@ -194,16 +209,17 @@ func deploy(ctx context.Context, c *dagger.Client, imageRef string) {

// [lints, tests], builds, publishes & deploys a new version of the app
func All(ctx context.Context) {
mg.CtxDeps(ctx, Lint, Test, Build)

c := daggerClient(ctx)
c := newDaggerClient(ctx)
defer c.Close()
ctx = context.WithValue(ctx, "daggerClient", c)

mg.CtxDeps(ctx, Lint, Test, Build)

imageRef := publish(ctx, c)
deploy(ctx, c, imageRef)
}

func daggerClient(ctx context.Context) *dagger.Client {
func newDaggerClient(ctx context.Context) *dagger.Client {
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr))
if err != nil {
panic(err)
Expand Down

0 comments on commit da6d7de

Please sign in to comment.