forked from dagger/dagger
-
Notifications
You must be signed in to change notification settings - Fork 0
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: Alex Suraci <alex@dagger.io>
- Loading branch information
Showing
12 changed files
with
168 additions
and
165 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
134 changes: 67 additions & 67 deletions
134
docs/current/guides/snippets/build-test-publish-java-spring/main.go
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 |
---|---|---|
@@ -1,86 +1,86 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"dagger.io/dagger" | ||
"dagger.io/dagger" | ||
) | ||
|
||
func main() { | ||
|
||
// check for Docker Hub registry credentials in host environment | ||
vars := []string{"DOCKERHUB_USERNAME", "DOCKERHUB_PASSWORD"} | ||
for _, v := range vars { | ||
if os.Getenv(v) == "" { | ||
log.Fatalf("Environment variable %s is not set", v) | ||
} | ||
} | ||
// check for Docker Hub registry credentials in host environment | ||
vars := []string{"DOCKERHUB_USERNAME", "DOCKERHUB_PASSWORD"} | ||
for _, v := range vars { | ||
if os.Getenv(v) == "" { | ||
log.Fatalf("Environment variable %s is not set", v) | ||
} | ||
} | ||
|
||
// initialize Dagger client | ||
ctx := context.Background() | ||
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer client.Close() | ||
// initialize Dagger client | ||
ctx := context.Background() | ||
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer client.Close() | ||
|
||
// set registry password as secret for Dagger pipeline | ||
password := client.SetSecret("password", os.Getenv("DOCKERHUB_PASSWORD")) | ||
username := os.Getenv("DOCKERHUB_USERNAME") | ||
// set registry password as secret for Dagger pipeline | ||
password := client.SetSecret("password", os.Getenv("DOCKERHUB_PASSWORD")) | ||
username := os.Getenv("DOCKERHUB_USERNAME") | ||
|
||
// create a cache volume for Maven downloads | ||
mavenCache := client.CacheVolume("maven-cache") | ||
// create a cache volume for Maven downloads | ||
mavenCache := client.CacheVolume("maven-cache") | ||
|
||
// get reference to source code directory | ||
source := client.Host().Directory(".", dagger.HostDirectoryOpts{ | ||
Exclude: []string{"ci"}, | ||
}) | ||
// get reference to source code directory | ||
source := client.Host().Directory(".", dagger.HostDirectoryOpts{ | ||
Exclude: []string{"ci"}, | ||
}) | ||
|
||
// create database service container | ||
mariadb := client.Container(). | ||
From("mariadb:10.11.2"). | ||
WithEnvVariable("MARIADB_USER", "petclinic"). | ||
WithEnvVariable("MARIADB_PASSWORD", "petclinic"). | ||
WithEnvVariable("MARIADB_DATABASE", "petclinic"). | ||
WithEnvVariable("MARIADB_ROOT_PASSWORD", "root"). | ||
WithExposedPort(3306). | ||
WithExec([]string{}) | ||
// create database service container | ||
mariadb := client.Container(). | ||
From("mariadb:10.11.2"). | ||
WithEnvVariable("MARIADB_USER", "petclinic"). | ||
WithEnvVariable("MARIADB_PASSWORD", "petclinic"). | ||
WithEnvVariable("MARIADB_DATABASE", "petclinic"). | ||
WithEnvVariable("MARIADB_ROOT_PASSWORD", "root"). | ||
WithExposedPort(3306). | ||
WithExec([]string{}) | ||
|
||
// use maven:3.9 container | ||
// mount cache and source code volumes | ||
// set working directory | ||
app := client.Container(). | ||
From("maven:3.9-eclipse-temurin-17"). | ||
WithMountedCache("/root/.m2", mavenCache). | ||
WithMountedDirectory("/app", source). | ||
WithWorkdir("/app") | ||
// use maven:3.9 container | ||
// mount cache and source code volumes | ||
// set working directory | ||
app := client.Container(). | ||
From("maven:3.9-eclipse-temurin-17"). | ||
WithMountedCache("/root/.m2", mavenCache). | ||
WithMountedDirectory("/app", source). | ||
WithWorkdir("/app") | ||
|
||
// define binding between | ||
// application and service containers | ||
// define JDBC URL for tests | ||
// test, build and package application as JAR | ||
build := app.WithServiceBinding("db", mariadb). | ||
WithEnvVariable("MYSQL_URL", "jdbc:mysql://petclinic:petclinic@db/petclinic"). | ||
WithExec([]string{"mvn", "-Dspring.profiles.active=mysql", "clean", "package"}) | ||
// define binding between | ||
// application and service containers | ||
// define JDBC URL for tests | ||
// test, build and package application as JAR | ||
build := app.WithServiceBinding("db", mariadb). | ||
WithEnvVariable("MYSQL_URL", "jdbc:mysql://petclinic:petclinic@db/petclinic"). | ||
WithExec([]string{"mvn", "-Dspring.profiles.active=mysql", "clean", "package"}) | ||
|
||
// use eclipse alpine container as base | ||
// copy JAR files from builder | ||
// set entrypoint and database profile | ||
deploy := client.Container(). | ||
From("eclipse-temurin:17-alpine"). | ||
WithDirectory("/app", build.Directory("./target")). | ||
WithEntrypoint([]string{"java", "-jar", "-Dspring.profiles.active=mysql", "/app/spring-petclinic-3.0.0-SNAPSHOT.jar"}) | ||
// use eclipse alpine container as base | ||
// copy JAR files from builder | ||
// set entrypoint and database profile | ||
deploy := client.Container(). | ||
From("eclipse-temurin:17-alpine"). | ||
WithDirectory("/app", build.Directory("./target")). | ||
WithEntrypoint([]string{"java", "-jar", "-Dspring.profiles.active=mysql", "/app/spring-petclinic-3.0.0-SNAPSHOT.jar"}) | ||
|
||
// publish image to registry | ||
address, err := deploy.WithRegistryAuth("docker.io", username, password). | ||
Publish(ctx, fmt.Sprintf("%s/myapp", username)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
// publish image to registry | ||
address, err := deploy.WithRegistryAuth("docker.io", username, password). | ||
Publish(ctx, fmt.Sprintf("%s/myapp", username)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// print image address | ||
fmt.Println("Image published at:", address) | ||
// print image address | ||
fmt.Println("Image published at:", address) | ||
} |
167 changes: 83 additions & 84 deletions
167
docs/current/guides/snippets/gitlab-google-cloud/main.go
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 |
---|---|---|
@@ -1,96 +1,95 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"dagger.io/dagger" | ||
run "cloud.google.com/go/run/apiv2" | ||
runpb "cloud.google.com/go/run/apiv2/runpb" | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
run "cloud.google.com/go/run/apiv2" | ||
runpb "cloud.google.com/go/run/apiv2/runpb" | ||
"dagger.io/dagger" | ||
) | ||
|
||
const GCR_SERVICE_URL = "projects/PROJECT/locations/us-central1/services/myapp" | ||
const GCR_PUBLISH_ADDRESS = "gcr.io/PROJECT/myapp" | ||
|
||
func main() { | ||
// create Dagger client | ||
ctx := context.Background() | ||
daggerClient, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer daggerClient.Close() | ||
|
||
// get working directory on host | ||
source := daggerClient.Host().Directory(".", dagger.HostDirectoryOpts{ | ||
Exclude: []string{"ci"}, | ||
}) | ||
|
||
// build application | ||
builder := daggerClient.Container(dagger.ContainerOpts{Platform: "linux/amd64"}). | ||
From("golang:1.20"). | ||
WithDirectory("/src", source). | ||
WithWorkdir("/src"). | ||
WithEnvVariable("CGO_ENABLED", "0"). | ||
WithExec([]string{"go", "build", "-o", "myapp"}) | ||
|
||
// add binary to alpine base | ||
prodImage := daggerClient.Container(dagger.ContainerOpts{Platform: "linux/amd64"}). | ||
From("alpine"). | ||
WithFile("/bin/myapp", builder.File("/src/myapp")). | ||
WithEntrypoint([]string{"/bin/myapp"}) | ||
|
||
// publish container to Google Container Registry | ||
addr, err := prodImage.Publish(ctx, GCR_PUBLISH_ADDRESS) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// print ref | ||
fmt.Println("Published at:", addr) | ||
|
||
// create Google Cloud Run client | ||
gcrClient, err := run.NewServicesClient(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer gcrClient.Close() | ||
|
||
// define service request | ||
gcrRequest := &runpb.UpdateServiceRequest{ | ||
Service: &runpb.Service{ | ||
Name: GCR_SERVICE_URL, | ||
Template: &runpb.RevisionTemplate{ | ||
Containers: []*runpb.Container{ | ||
{ | ||
Image: addr, | ||
Ports: []*runpb.ContainerPort{ | ||
{ | ||
Name: "http1", | ||
ContainerPort: 1323, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// update service | ||
gcrOperation, err := gcrClient.UpdateService(ctx, gcrRequest) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// wait for service request completion | ||
gcrResponse, err := gcrOperation.Wait(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// print ref | ||
fmt.Println("Deployment for image", addr, "now available at", gcrResponse.Uri) | ||
// create Dagger client | ||
ctx := context.Background() | ||
daggerClient, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stderr)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer daggerClient.Close() | ||
|
||
// get working directory on host | ||
source := daggerClient.Host().Directory(".", dagger.HostDirectoryOpts{ | ||
Exclude: []string{"ci"}, | ||
}) | ||
|
||
// build application | ||
builder := daggerClient.Container(dagger.ContainerOpts{Platform: "linux/amd64"}). | ||
From("golang:1.20"). | ||
WithDirectory("/src", source). | ||
WithWorkdir("/src"). | ||
WithEnvVariable("CGO_ENABLED", "0"). | ||
WithExec([]string{"go", "build", "-o", "myapp"}) | ||
|
||
// add binary to alpine base | ||
prodImage := daggerClient.Container(dagger.ContainerOpts{Platform: "linux/amd64"}). | ||
From("alpine"). | ||
WithFile("/bin/myapp", builder.File("/src/myapp")). | ||
WithEntrypoint([]string{"/bin/myapp"}) | ||
|
||
// publish container to Google Container Registry | ||
addr, err := prodImage.Publish(ctx, GCR_PUBLISH_ADDRESS) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// print ref | ||
fmt.Println("Published at:", addr) | ||
|
||
// create Google Cloud Run client | ||
gcrClient, err := run.NewServicesClient(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer gcrClient.Close() | ||
|
||
// define service request | ||
gcrRequest := &runpb.UpdateServiceRequest{ | ||
Service: &runpb.Service{ | ||
Name: GCR_SERVICE_URL, | ||
Template: &runpb.RevisionTemplate{ | ||
Containers: []*runpb.Container{ | ||
{ | ||
Image: addr, | ||
Ports: []*runpb.ContainerPort{ | ||
{ | ||
Name: "http1", | ||
ContainerPort: 1323, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// update service | ||
gcrOperation, err := gcrClient.UpdateService(ctx, gcrRequest) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// wait for service request completion | ||
gcrResponse, err := gcrOperation.Wait(ctx) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// print ref | ||
fmt.Println("Deployment for image", addr, "now available at", gcrResponse.Uri) | ||
|
||
} |
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
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
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
Oops, something went wrong.