@@ -5,18 +5,15 @@ import (
5
5
"encoding/json"
6
6
"log"
7
7
"os"
8
- "path/filepath"
9
8
"time"
10
9
11
10
"github.com/depot/depot-go/build"
12
11
"github.com/depot/depot-go/machine"
13
12
cliv1 "github.com/depot/depot-go/proto/depot/cli/v1"
14
13
"github.com/moby/buildkit/client"
14
+ "github.com/moby/buildkit/client/llb"
15
15
"github.com/moby/buildkit/session"
16
- "github.com/moby/buildkit/session/auth"
17
- "google.golang.org/grpc"
18
- "google.golang.org/grpc/codes"
19
- "google.golang.org/grpc/status"
16
+ "github.com/moby/buildkit/session/upload/uploadprovider"
20
17
)
21
18
22
19
func main () {
@@ -27,10 +24,17 @@ func main() {
27
24
token := os .Getenv ("DEPOT_TOKEN" )
28
25
project := os .Getenv ("DEPOT_PROJECT_ID" )
29
26
30
- // ... and set these variables.
31
- dockerfilePath := "./Dockerfile"
32
- workingDir := "."
33
- imageTag := "AWS_ACCOUNT_ID_HERE.dkr.ecr.us-east-1.amazonaws.com/REPO_HERE:TAG_HERE"
27
+ /*
28
+ *
29
+ * howdy.tar.gz is a compressed tar archive that contains the Dockerfile and
30
+ * any other files needed to build the image.
31
+ *
32
+ */
33
+ r , err := os .Open ("howdy.tar.gz" )
34
+ if err != nil {
35
+ log .Printf ("unable to open file: %v" , err )
36
+ return
37
+ }
34
38
35
39
// 1. Register a new build. This returns back an id and a temporary build token.
36
40
req := & cliv1.CreateBuildRequest {
@@ -62,28 +66,27 @@ func main() {
62
66
return
63
67
}
64
68
69
+ uploader := uploadprovider .New ()
70
+ // Special buildkit URL for HTTP over gRPC over gRPC.
71
+ contextURL := uploader .Add (r )
72
+
73
+ echo := llb .Scratch ().File (llb .Copy (llb .Local ("." ), "/" , "/" ))
74
+
75
+ // TODO: right context?
76
+ def , err := echo .Marshal (connectCtx )
77
+ if err != nil {
78
+ log .Printf ("unable to marshal LLB definition: %v" , err )
79
+ return
80
+ }
81
+
65
82
solverOptions := client.SolveOpt {
66
83
Frontend : "dockerfile.v0" , // Interpret the build as a Dockerfile.
67
84
FrontendAttrs : map [string ]string {
68
- "filename" : filepath .Base (dockerfilePath ),
69
85
"platform" : "linux/arm64" , // Build for arm64 architecture.
70
- },
71
- LocalDirs : map [string ]string {
72
- "dockerfile" : filepath .Dir (dockerfilePath ),
73
- "context" : workingDir ,
74
- },
75
- Exports : []client.ExportEntry {
76
- {
77
- Type : "image" ,
78
- Attrs : map [string ]string {
79
- "oci-mediatypes" : "true" ,
80
- "push" : "true" , // Push the image to the registry...
81
- "name" : imageTag , // ... with this tag.
82
- },
83
- },
86
+ "context" : contextURL ,
84
87
},
85
88
Session : []session.Attachable {
86
- & EnvAuth {} ,
89
+ uploader ,
87
90
},
88
91
}
89
92
@@ -98,53 +101,8 @@ func main() {
98
101
}()
99
102
100
103
// 4. Build and push the image.
101
- _ , buildErr = buildkitClient .Solve (ctx , nil , solverOptions , buildStatusCh )
104
+ _ , buildErr = buildkitClient .Solve (ctx , def , solverOptions , buildStatusCh )
102
105
if buildErr != nil {
103
106
return
104
107
}
105
108
}
106
-
107
- // EnvAuth is a custom auth provider that uses environment variables to provide registry credentials.
108
- // Uses REGISTRY_USERNAME and REGISTRY_TOKEN environment variables.
109
- type EnvAuth struct {}
110
-
111
- // In BuildKit an Attachable is a client-side gRPC server that the build server can connect to.
112
- // BuildKit tunnels gRPC over gRPC, so the client-side can be dialed by the server-side.
113
- var _ session.Attachable = (* EnvAuth )(nil )
114
-
115
- // Register hosts an AuthServer on the client-side for the build server.
116
- func (ap * EnvAuth ) Register (server * grpc.Server ) {
117
- auth .RegisterAuthServer (server , ap )
118
- }
119
-
120
- // AuthServer is not documented in BuildKit, but these are functions called by the build server.
121
- var _ auth.AuthServer = (* EnvAuth )(nil )
122
-
123
- // For AWS ECR username is `AWS` and for password run `aws ecr get-login-password --region YOUR_REGION`.
124
- func (ap * EnvAuth ) Credentials (ctx context.Context , req * auth.CredentialsRequest ) (* auth.CredentialsResponse , error ) {
125
- // If base image is at docker return empty creds to use public download.
126
- if req .Host == "registry-1.docker.io" {
127
- return & auth.CredentialsResponse {}, nil
128
- }
129
-
130
- username := os .Getenv ("REGISTRY_USERNAME" )
131
- registryPassword := os .Getenv ("REGISTRY_PASSWORD" )
132
-
133
- return & auth.CredentialsResponse {
134
- Username : username ,
135
- Secret : registryPassword ,
136
- }, nil
137
- }
138
-
139
- // GetTokenAuthority needs to return an Unimplemented or a nil public key in order for the Credentials function to be called.
140
- func (ap * EnvAuth ) GetTokenAuthority (ctx context.Context , req * auth.GetTokenAuthorityRequest ) (* auth.GetTokenAuthorityResponse , error ) {
141
- return nil , status .Errorf (codes .Unimplemented , "method Info not implemented" )
142
- }
143
-
144
- func (ap * EnvAuth ) VerifyTokenAuthority (ctx context.Context , req * auth.VerifyTokenAuthorityRequest ) (* auth.VerifyTokenAuthorityResponse , error ) {
145
- return nil , status .Errorf (codes .Unimplemented , "method Info not implemented" )
146
- }
147
-
148
- func (ap * EnvAuth ) FetchToken (ctx context.Context , req * auth.FetchTokenRequest ) (rr * auth.FetchTokenResponse , err error ) {
149
- return nil , status .Errorf (codes .Unimplemented , "method Info not implemented" )
150
- }
0 commit comments