Created
November 8, 2023 22:22
-
-
Save shelajev/8cac0e71afd782d6ac5bf11d38c84cfa to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package main | |
import ( | |
"context" | |
"fmt" | |
"os" | |
"dagger.io/dagger" | |
) | |
func main() { | |
err := doCi() | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
func doCi() error { | |
ctx := context.Background() | |
// create a Dagger client | |
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout)) | |
if err != nil { | |
return err | |
} | |
defer client.Close() | |
src := client.Host().Directory(".") // get the projects source directory | |
gradle := client.Container().From("maven:3.9-amazoncorretto-21-al2023"). | |
WithDirectory("/src", src).WithWorkdir("/src"). | |
WithExec([]string{"mvn", "test"}) | |
// get gradle output | |
out, err := gradle.Stdout(ctx) | |
if err != nil { | |
return err | |
} | |
// print output to console | |
fmt.Println(out) | |
return nil | |
} |
This file contains hidden or 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
package main | |
import ( | |
"context" | |
"fmt" | |
) | |
type Testcontainers struct{} | |
// optionally, boolean for Testcontainers Cloud, optional TCC_TOKEN (secret). | |
func (t *Testcontainers) Enable(ctx context.Context, c *Container) (*Container, error) { | |
// docker service | |
dockerVersion := "24.0" // extract as a parameter | |
port := 2375 | |
dockerd := dag.Container().From(fmt.Sprintf("docker:%s-dind", dockerVersion)). | |
WithMountedCache("/var/lib/docker", dag.CacheVolume(dockerVersion+"-docker-lib"), ContainerWithMountedCacheOpts{ | |
Sharing: Private, | |
}). | |
WithExposedPort(port). | |
WithExec([]string{ | |
"dockerd", | |
"--host=tcp://0.0.0.0:2375", | |
"--host=unix:///var/run/docker.sock", | |
"--tls=false", | |
}, ContainerWithExecOpts{ | |
InsecureRootCapabilities: true, | |
}). | |
AsService() | |
dockerHost, err := dockerd.Endpoint(ctx, ServiceEndpointOpts{ | |
Scheme: "tcp", | |
}) | |
if err != nil { | |
return nil, err | |
} | |
// --------- | |
c. | |
WithServiceBinding("docker", dockerd). | |
WithEnvVariable("DOCKER_HOST", dockerHost) | |
return c, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment