-
Notifications
You must be signed in to change notification settings - Fork 3
/
build_image_and_spec_flex_template.go
92 lines (79 loc) · 2.67 KB
/
build_image_and_spec_flex_template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"context"
"fmt"
"os"
"dagger.io/dagger"
)
func main() {
projectId := os.Getenv("PROJECT_ID")
location := os.Getenv("LOCATION")
repoName := os.Getenv("REPO_NAME")
imageName := os.Getenv("IMAGE_NAME")
imageTag := os.Getenv("IMAGE_TAG")
metadataTemplateFilePath := os.Getenv("METADATA_TEMPLATE_FILE_PATH")
sdkLanguage := os.Getenv("SDK_LANGUAGE")
metadataFile := os.Getenv("METADATA_FILE")
saEmail := os.Getenv("SA_EMAIL")
ctx := context.Background()
client, err := dagger.Connect(ctx, dagger.WithLogOutput(os.Stdout))
if err != nil {
panic(err)
}
defer client.Close()
hostSourceDir := client.Host().Directory(".", dagger.HostDirectoryOpts{})
activateServiceAccount := []string{
"gcloud",
"auth",
"activate-service-account",
saEmail,
"--key-file=./secrets/sa-dataflow.json",
fmt.Sprintf("--project=%s", projectId),
}
source := client.Container().
From("google/cloud-sdk:420.0.0-slim").
WithMountedDirectory("/src", hostSourceDir).
WithWorkdir("/src").
Directory(".")
buildFlexTemplateImage := client.Container().
From("gcr.io/kaniko-project/executor:v1.9.0-debug").
WithEntrypoint([]string{}).
WithDirectory(".", source).
WithEnvVariable("CI_SERVICE_NAME", "dagger").
WithEnvVariable("PROJECT_ID", projectId).
WithEnvVariable("LOCATION", location).
WithEnvVariable("REPO_NAME", repoName).
WithEnvVariable("IMAGE_NAME", imageName).
WithEnvVariable("IMAGE_TAG", imageTag).
WithEnvVariable("GOOGLE_APPLICATION_CREDENTIALS", "./secrets/sa-dataflow.json").
WithExec([]string{
"sh",
"-c",
"/kaniko/executor --use-new-run --single-snapshot --context=dir://./ --dockerfile Dockerfile --destination $LOCATION-docker.pkg.dev/$PROJECT_ID/$REPO_NAME/$IMAGE_NAME/$CI_SERVICE_NAME:$IMAGE_TAG",
}).
Directory(".")
createFlexTemplateSpecFileGcs := client.Container().
From("google/cloud-sdk:420.0.0-slim").
WithDirectory(".", buildFlexTemplateImage).
WithEnvVariable("CI_SERVICE_NAME", "dagger").
WithEnvVariable("PROJECT_ID", projectId).
WithEnvVariable("LOCATION", location).
WithEnvVariable("REPO_NAME", repoName).
WithEnvVariable("IMAGE_NAME", imageName).
WithEnvVariable("IMAGE_TAG", imageTag).
WithEnvVariable("METADATA_TEMPLATE_FILE_PATH", metadataTemplateFilePath).
WithEnvVariable("SDK_LANGUAGE", sdkLanguage).
WithEnvVariable("METADATA_FILE", metadataFile).
WithEnvVariable("GOOGLE_APPLICATION_CREDENTIALS", "./secrets/sa-dataflow.json").
WithExec(activateServiceAccount).
WithExec([]string{
"sh",
"-c",
"./scripts/create_flex_template_spec_file_gcs.sh",
})
out, err := createFlexTemplateSpecFileGcs.Stdout(ctx)
if err != nil {
panic(err)
}
fmt.Printf("Published image to: %s\n", out)
}