-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matthias Bertschy <[email protected]>
- Loading branch information
Showing
3 changed files
with
136 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"dagger.io/dagger" | ||
) | ||
|
||
// the platforms to build for and push in a multi-platform image | ||
var platforms = []dagger.Platform{ | ||
"linux/amd64", | ||
"linux/arm64", | ||
} | ||
|
||
// the container registry for the multi-platform image | ||
const imageRepo = "quay.io/matthiasb_1/synchronizer:latest" | ||
|
||
func main() { | ||
if err := prMerged(context.Background()); err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func prMerged(ctx context.Context) error { | ||
// initialize Dagger client | ||
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) | ||
if err != nil { | ||
return err | ||
} | ||
defer client.Close() | ||
|
||
// get reference to the local project | ||
src := client.Host().Directory(".") | ||
|
||
imageTagPrerelease, err := dockerBuild(ctx, client, src) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(imageTagPrerelease) | ||
return nil | ||
} | ||
|
||
func dockerBuild(ctx context.Context, client *dagger.Client, src *dagger.Directory) (string, error) { | ||
// run unit tests | ||
if err := unitTest(ctx, client, src); err != nil { | ||
return "", err | ||
} | ||
// build and push the multi-platform image | ||
imageDigest, err := buildPush(ctx, client, src) | ||
if err != nil { | ||
return "", err | ||
} | ||
return imageDigest, nil | ||
} | ||
|
||
func unitTest(ctx context.Context, client *dagger.Client, src *dagger.Directory) error { | ||
fmt.Println("Running unit tests...") | ||
out, err := client.Container(). | ||
From("golang:1.20-bullseye"). | ||
WithDirectory("/src", client.Host().Directory(".")). | ||
WithWorkdir("/src"). | ||
WithExec([]string{"go", "test", "./..."}). | ||
Stderr(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(out) | ||
return nil | ||
} | ||
|
||
func buildPush(ctx context.Context, client *dagger.Client, src *dagger.Directory) (string, error) { | ||
fmt.Println("Building multi-platform image...") | ||
platformVariants := make([]*dagger.Container, 0, len(platforms)) | ||
for _, platform := range platforms { | ||
ctr := src. | ||
DockerBuild(dagger.DirectoryDockerBuildOpts{ | ||
Dockerfile: "build/Dockerfile", | ||
Platform: platform, | ||
}) | ||
platformVariants = append(platformVariants, ctr) | ||
} | ||
|
||
imageDigest, err := client. | ||
Container(). | ||
Publish(ctx, imageRepo, dagger.ContainerPublishOpts{ | ||
PlatformVariants: platformVariants, | ||
// Some registries may require explicit use of docker mediatypes | ||
// rather than the default OCI mediatypes | ||
// MediaTypes: dagger.Dockermediatypes, | ||
}) | ||
if err != nil { | ||
return "", err | ||
} | ||
fmt.Println("Pushed multi-platform image w/ digest: ", imageDigest) | ||
return imageDigest, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters