Skip to content

Commit bd591ba

Browse files
authored
Merge pull request #50 from depot/feat/organize-examples
feat: organize examples
2 parents ad22e1f + ec4fccf commit bd591ba

File tree

7 files changed

+130
-2
lines changed

7 files changed

+130
-2
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,7 @@ jobs:
5252
go-version: "1.24"
5353
check-latest: true
5454
cache: true
55-
- run: go build -o bin/example ./examples/build
55+
- name: Build examples
56+
run: |
57+
go build -o bin/llb ./examples/low-level-build-ops
58+
go build -o bin/simple ./examples/simple

examples/go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24
44

55
require (
66
github.com/depot/depot-go v0.0.0-dev
7+
github.com/docker/cli v25.0.3+incompatible
78
github.com/moby/buildkit v0.13.2
89
github.com/opencontainers/image-spec v1.1.0
910
golang.org/x/sync v0.12.0
@@ -24,6 +25,8 @@ require (
2425
github.com/containerd/ttrpc v1.2.7 // indirect
2526
github.com/containerd/typeurl/v2 v2.1.1 // indirect
2627
github.com/distribution/reference v0.6.0 // indirect
28+
github.com/docker/docker v25.0.3+incompatible // indirect
29+
github.com/docker/docker-credential-helpers v0.8.0 // indirect
2730
github.com/felixge/httpsnoop v1.0.4 // indirect
2831
github.com/go-logr/logr v1.4.2 // indirect
2932
github.com/go-logr/stdr v1.2.2 // indirect
@@ -35,6 +38,7 @@ require (
3538
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
3639
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
3740
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 // indirect
41+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
3842
github.com/in-toto/in-toto-golang v0.5.0 // indirect
3943
github.com/klauspost/compress v1.17.4 // indirect
4044
github.com/kr/text v0.2.0 // indirect
@@ -69,6 +73,7 @@ require (
6973
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 // indirect
7074
google.golang.org/grpc v1.63.2 // indirect
7175
google.golang.org/protobuf v1.35.2 // indirect
76+
gotest.tools/v3 v3.5.2 // indirect
7277
)
7378

7479
replace github.com/depot/depot-go => ../

examples/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,12 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
5757
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5858
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
5959
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
60+
github.com/docker/cli v25.0.3+incompatible h1:KLeNs7zws74oFuVhgZQ5ONGZiXUUdgsdy6/EsX/6284=
61+
github.com/docker/cli v25.0.3+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
6062
github.com/docker/docker v25.0.3+incompatible h1:D5fy/lYmY7bvZa0XTZ5/UJPljor41F+vdyJG5luQLfQ=
6163
github.com/docker/docker v25.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
64+
github.com/docker/docker-credential-helpers v0.8.0 h1:YQFtbBQb4VrpoPxhFuzEBPQ9E16qz5SpHLS+uswaCp8=
65+
github.com/docker/docker-credential-helpers v0.8.0/go.mod h1:UGFXcuoQ5TxPiB54nHOZ32AWRqQdECoh/Mg0AlEYb40=
6266
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
6367
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
6468
github.com/docker/go-events v0.0.0-20190806004212-e31b211e4f1c h1:+pKlWGMw7gf6bQ+oDZB4KHQFypsfjYlq/C4rfL7D3g8=
@@ -111,6 +115,8 @@ github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 h1:+9834+KizmvFV7pXQGSXQTsaW
111115
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0/go.mod h1:z0ButlSOZa5vEBq9m2m2hlwIgKw+rp3sdCBRoJY+30Y=
112116
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0 h1:YBftPWNWd4WwGqtY2yeZL2ef8rHAxPBD8KFhJpmcqms=
113117
github.com/grpc-ecosystem/grpc-gateway/v2 v2.16.0/go.mod h1:YN5jB8ie0yfIUg6VvR9Kz84aCaG7AsGZnLjhHbUqwPg=
118+
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
119+
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
114120
github.com/in-toto/in-toto-golang v0.5.0 h1:hb8bgwr0M2hGdDsLjkJ3ZqJ8JFLL/tgYdAxF/XEFBbY=
115121
github.com/in-toto/in-toto-golang v0.5.0/go.mod h1:/Rq0IZHLV7Ku5gielPT4wPHJfH1GdHMCq8+WPxw8/BE=
116122
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
@@ -306,5 +312,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
306312
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
307313
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
308314
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
315+
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
316+
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
309317
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
310318
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Low-Level Build Example
2+
3+
This example shows how to use the BuildKit low-level-build operations to build an image using Depot.

examples/build/main.go renamed to examples/low-level-build-ops/main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ func buildImage(ctx context.Context, buildkitClient *client.Client) error {
7575
ch := make(chan *client.SolveStatus)
7676
eg, ctx := errgroup.WithContext(ctx)
7777

78-
ops := llb.Image("alpine:latest")
78+
ops := llb.
79+
Image("ubuntu:24.04"). // Like FROM
80+
Run(llb.Shlex("apt update")) // Like RUN
7981
def, err := ops.Marshal(ctx, llb.LinuxAmd64)
8082
if err != nil {
8183
return err

examples/simple/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Simple Build with Depot
2+
3+
This example shows how to build and push an image with Depot.

examples/simple/main.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"log"
7+
"os"
8+
"path/filepath"
9+
"time"
10+
11+
"github.com/depot/depot-go/build"
12+
"github.com/depot/depot-go/machine"
13+
cliv1 "github.com/depot/depot-go/proto/depot/cli/v1"
14+
"github.com/docker/cli/cli/config"
15+
"github.com/moby/buildkit/client"
16+
"github.com/moby/buildkit/session"
17+
"github.com/moby/buildkit/session/auth/authprovider"
18+
)
19+
20+
func main() {
21+
// You can use a context with timeout to cancel the build if you would like.
22+
ctx := context.Background()
23+
24+
// Set these environment variables...
25+
token := os.Getenv("DEPOT_TOKEN")
26+
project := os.Getenv("DEPOT_PROJECT_ID")
27+
28+
// ... and set these variables.
29+
dockerfilePath := "./Dockerfile"
30+
workingDir := "."
31+
imageTag := "goller/depot-example:latest"
32+
33+
// 1. Register a new build. This returns back an id and a temporary build token.
34+
req := &cliv1.CreateBuildRequest{
35+
ProjectId: project,
36+
}
37+
build, err := build.NewBuild(ctx, req, token)
38+
if err != nil {
39+
log.Fatal(err)
40+
}
41+
42+
// Set the buildErr to any error that represents the build failing.
43+
var buildErr error
44+
defer build.Finish(buildErr)
45+
46+
// 2. Acquire a buildkit machine.
47+
var buildkit *machine.Machine
48+
buildkit, buildErr = machine.Acquire(ctx, build.ID, build.Token, "arm64" /* or "amd64" */)
49+
if buildErr != nil {
50+
return
51+
}
52+
defer buildkit.Release()
53+
54+
connectCtx, cancelConnect := context.WithTimeout(ctx, 5*time.Minute)
55+
defer cancelConnect()
56+
57+
var buildkitClient *client.Client
58+
buildkitClient, buildErr = buildkit.Connect(connectCtx)
59+
if buildErr != nil {
60+
return
61+
}
62+
63+
solverOptions := client.SolveOpt{
64+
Frontend: "dockerfile.v0", // Interpret the build as a Dockerfile.
65+
FrontendAttrs: map[string]string{
66+
"filename": filepath.Base(dockerfilePath),
67+
"platform": "linux/arm64", // Build for arm64 architecture.
68+
},
69+
LocalDirs: map[string]string{
70+
"dockerfile": filepath.Dir(dockerfilePath),
71+
"context": workingDir,
72+
},
73+
Exports: []client.ExportEntry{
74+
{
75+
Type: "image",
76+
Attrs: map[string]string{
77+
"oci-mediatypes": "true",
78+
"push": "true", // Push the image to the registry...
79+
"name": imageTag, // ... with this tag.
80+
},
81+
},
82+
},
83+
Session: []session.Attachable{
84+
// Use credentials sorted by `docker login` command.
85+
authprovider.NewDockerAuthProvider(config.LoadDefaultConfigFile(os.Stderr), nil),
86+
},
87+
}
88+
89+
// 3. Print all build status updates as JSON to stdout.
90+
buildStatusCh := make(chan *client.SolveStatus, 10)
91+
go func() {
92+
enc := json.NewEncoder(os.Stdout)
93+
enc.SetIndent("", " ")
94+
for status := range buildStatusCh {
95+
_ = enc.Encode(status)
96+
}
97+
}()
98+
99+
// 4. Build and push the image.
100+
_, buildErr = buildkitClient.Solve(ctx, nil, solverOptions, buildStatusCh)
101+
if buildErr != nil {
102+
return
103+
}
104+
}

0 commit comments

Comments
 (0)