Skip to content

Commit

Permalink
Merge pull request #19 from VaibhavMalik4187/dagger-transition
Browse files Browse the repository at this point in the history
Created separate files for different workflows
  • Loading branch information
matthyx authored Jan 5, 2024
2 parents c537538 + 5db2ceb commit 59cd7fa
Show file tree
Hide file tree
Showing 5 changed files with 272 additions and 147 deletions.
107 changes: 107 additions & 0 deletions ci/lib/basic_tests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package lib

import (
"context"
"fmt"

"dagger.io/dagger"
)

type BasicTestsInputs struct {
GOVERSION string
GO111MODULE string
CGOENABLED bool
BUILDPATH string
UNITTESTSPATH string
FAILED_THRESHOLD int
}

type BasicTestsSecrets struct {
SNYK_TOKEN string
GITGUARDIAN_API_KEY string
}

// Check if secrets are set
func CheckSecrets(secrets BasicTestsSecrets) bool {
if secrets.GITGUARDIAN_API_KEY == "" || secrets.SNYK_TOKEN == "" {
return false
}
return true
}

func UnitTest(ctx context.Context, client *dagger.Client, src *dagger.Directory) error {
fmt.Println("Running unit tests...")
// create a cache volume
goBuildCache := client.CacheVolume("goBuild")
goPkgCache := client.CacheVolume("goPkg")
// run tests
out, err := client.Container().
From("golang:1.20-bullseye").
WithDirectory("/src", src).
WithMountedCache("/go/pkg", goPkgCache).
WithMountedCache("/root/.cache/go-build", goBuildCache).
WithWorkdir("/src").
WithExec([]string{"go", "test", "-v", "./..."}).
Stderr(ctx)
if err != nil {
return err
}
fmt.Println(out)
return nil
}

func EnvironmentTest(ctx context.Context, client *dagger.Client, src *dagger.Directory, inputs BasicTestsInputs) error {
testCmd := []string{"go", "test", "-v"}
if inputs.CGOENABLED {
testCmd = append(testCmd, "-race", "./...")
} else {
testCmd = append(testCmd, "./...")
}

fmt.Println("Running environment tests...")

// Create a cache volume
goBuildCache := client.CacheVolume("goBuild")
goPkgCache := client.CacheVolume("goPkg")

// Run tests
out, err := client.Container().
From("golang:1.20-bullseye").
WithDirectory("/src", src).
WithMountedCache("/go/pkg", goPkgCache).
WithMountedCache("/root/.cache/go-build", goBuildCache).
WithWorkdir("/src").
WithExec(testCmd).
Stderr(ctx)

if err != nil {
return err
}

fmt.Println(out)
return nil
}

func BasicTest(ctx context.Context, client *dagger.Client, src *dagger.Directory) error {
// TODO: Write the code for the Basic-Test job
return nil
}

func BasicTests(ctx context.Context, client *dagger.Client, src *dagger.Directory, inputs BasicTestsInputs, secrets BasicTestsSecrets) error {
// Step 1: Check if the secrets are set
secretsSet := CheckSecrets(secrets)
if !secretsSet {
fmt.Println("Secrets are not set")
fmt.Printf("SNYK_TOKEN: %s\n", secrets.SNYK_TOKEN)
fmt.Printf("GITGUARDIAN_API_KEY: %s\n", secrets.GITGUARDIAN_API_KEY)

// return errors.New("secrets are not set")
}

err := EnvironmentTest(ctx, client, src, inputs)
if err != nil {
fmt.Println("Environment test failed")
}

return nil
}
35 changes: 35 additions & 0 deletions ci/lib/prcreated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package lib

import (
"context"
"fmt"
"os"

"dagger.io/dagger"
)

// Run basic tests on PR
func PrCreated(ctx context.Context, client *dagger.Client, src *dagger.Directory) error {
basicTestsInputs := BasicTestsInputs{
GOVERSION: os.Getenv("GO_VERSION"),
CGOENABLED: os.Getenv("CGO_ENABLED") == "1",
BUILDPATH: os.Getenv("BUILD_PATH"),
UNITTESTSPATH: os.Getenv("UNITTESTS_PATH"),
}

basicTestsSecrets := BasicTestsSecrets{
SNYK_TOKEN: os.Getenv("SNYK_TOKEN"),
GITGUARDIAN_API_KEY: os.Getenv("GITGUARDIAN_API_KEY"),
}

fmt.Printf("GOVERSION: %s\n", basicTestsInputs.GOVERSION)
fmt.Printf("CGOENABLED: %t\n", basicTestsInputs.CGOENABLED)
fmt.Printf("BUILDPATH: %s\n", basicTestsInputs.BUILDPATH)
fmt.Printf("UNITTESTSPATH: %s\n", basicTestsInputs.UNITTESTSPATH)

if err := BasicTests(ctx, client, src, basicTestsInputs, basicTestsSecrets); err != nil {
return err
}

return nil
}
83 changes: 83 additions & 0 deletions ci/lib/prmerged.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package lib

import (
"context"
"fmt"

"dagger.io/dagger"
)

func ReleaseRetag(ctx context.Context, client *dagger.Client, prerelease string) (string, error) {
// TODO implement
return "", nil
}

func SystemTest(ctx context.Context, client *dagger.Client, imageDigest string) error {
// TODO implement
return nil
}

func DockerBuild(ctx context.Context, client *dagger.Client, src *dagger.Directory, platforms []dagger.Platform, imageRepo string) (string, error) {
// TODO prerelease image tag
// 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, platforms, imageRepo)
if err != nil {
return "", err
}
// TODO sign image with cosign
return imageDigest, nil
}

func BuildPush(ctx context.Context, client *dagger.Client, src *dagger.Directory, platforms []dagger.Platform, imageRepo string) (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
}

func PrMerged(ctx context.Context, client *dagger.Client, src *dagger.Directory) error {
// Build image
imagePrerelease, err := DockerBuild(ctx, client, src, platforms, imageRepo)
if err != nil {
return err
}
fmt.Println(imagePrerelease)

// run system tests
if err := SystemTest(ctx, client, imagePrerelease); err != nil {
return err
}

// create release and retag image
imageRelease, err := ReleaseRetag(ctx, client, imagePrerelease)
if err != nil {
return err
}
fmt.Println(imageRelease)

return nil
}
34 changes: 34 additions & 0 deletions ci/lib/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package lib

import (
"context"
"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 system tests to run on the newly built image
var TestNames = []string{
"unit",
}

// the container registry for the multi-platform image
const imageRepo = "quay.io/matthiasb_1/synchronizer"

func InitDagger(ctx context.Context) (*dagger.Client, *dagger.Directory, error) {
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
return nil, nil, err
}

// get reference to the local project
// git checkout is performed by the calling CI system (e.g. GitHub Actions)
src := client.Host().Directory(".")
return client, src, nil
}
Loading

0 comments on commit 59cd7fa

Please sign in to comment.