├── .circleci └── config.yml ├── .gitignore ├── 0-base ├── Tiltfile ├── build.bat ├── deployments │ ├── Dockerfile │ └── kubernetes.yaml ├── go.mod ├── go.sum ├── main.go └── web │ ├── index.html │ └── pets.png ├── 1-measured ├── Tiltfile ├── deployments │ ├── Dockerfile │ └── kubernetes.yaml ├── go.mod ├── go.sum ├── main.go ├── record-start-time.py ├── start.go └── web │ ├── pets.png │ └── templates │ └── index.tmpl ├── 2-optimized ├── Tiltfile ├── build.bat ├── deployments │ ├── Dockerfile │ └── kubernetes.yaml ├── go.mod ├── go.sum ├── main.go ├── record-start-time.py ├── start.go └── web │ ├── pets.png │ └── templates │ └── index.tmpl ├── 3-recommended ├── Tiltfile ├── build.bat ├── deployments │ ├── Dockerfile │ └── kubernetes.yaml ├── go.mod ├── go.sum ├── main.go ├── record-start-time.py ├── start.go ├── tilt_modules │ ├── extensions.json │ └── restart_process │ │ └── Tiltfile └── web │ ├── pets.png │ └── templates │ └── index.tmpl ├── LICENSE ├── NOTICE ├── README.md ├── test ├── test.ps1 └── test.sh └── tests-example ├── Tiltfile ├── Tiltfile.tests ├── build.bat ├── deployments ├── Dockerfile └── kubernetes.yaml ├── go.mod ├── go.sum ├── integration └── integration_test.go ├── main.go ├── main_test.go ├── record-start-time.py ├── start.go ├── subpackage └── subpackage_test.go └── web ├── pets.png └── templates └── index.tmpl /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | version: 2.1 2 | jobs: 3 | build: 4 | docker: 5 | - image: docker/tilt:latest 6 | 7 | steps: 8 | - checkout 9 | - setup_remote_docker 10 | - run: apt update && apt install -y golang git python3 11 | - run: ctlptl create cluster kind --registry=ctlptl-registry && test/test.sh 12 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | 8 | # Test binary, built with `go test -c` 9 | *.test 10 | 11 | # Output of the go coverage tool, specifically when used with LiteIDE 12 | *.out 13 | 14 | # Dependency directories (remove the comment below to include it) 15 | # vendor/ 16 | 17 | */build/ 18 | 19 | **/tilt_modules 20 | -------------------------------------------------------------------------------- /0-base/Tiltfile: -------------------------------------------------------------------------------- 1 | # -*- mode: Python -*- 2 | 3 | docker_build('example-go-image', '.', dockerfile='deployments/Dockerfile') 4 | k8s_yaml('deployments/kubernetes.yaml') 5 | k8s_resource('example-go', port_forwards=8000) 6 | -------------------------------------------------------------------------------- /0-base/build.bat: -------------------------------------------------------------------------------- 1 | set CGO_ENABLED=0 2 | set GOOS=linux 3 | set GOARCH=amd64 4 | go build -o build/tilt-example-go ./ -------------------------------------------------------------------------------- /0-base/deployments/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.21 2 | WORKDIR /app 3 | ADD . . 4 | RUN go install ./ 5 | ENTRYPOINT tilt-example-go 6 | -------------------------------------------------------------------------------- /0-base/deployments/kubernetes.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: example-go 5 | labels: 6 | app: example-go 7 | spec: 8 | selector: 9 | matchLabels: 10 | app: example-go 11 | template: 12 | metadata: 13 | labels: 14 | app: example-go 15 | spec: 16 | containers: 17 | - name: example-go 18 | image: example-go-image 19 | ports: 20 | - containerPort: 8000 21 | -------------------------------------------------------------------------------- /0-base/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tilt-dev/tilt-example-go 2 | 3 | go 1.21 4 | 5 | require github.com/gorilla/mux v1.8.0 6 | -------------------------------------------------------------------------------- /0-base/go.sum: -------------------------------------------------------------------------------- 1 | github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= 2 | github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 3 | -------------------------------------------------------------------------------- /0-base/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "log" 5 | "net/http" 6 | 7 | "github.com/gorilla/mux" 8 | ) 9 | 10 | type ExampleRouter struct { 11 | *mux.Router 12 | } 13 | 14 | func NewExampleRouter() *ExampleRouter { 15 | r := mux.NewRouter() 16 | 17 | fs := http.FileServer(http.Dir("./web")) 18 | r.PathPrefix("/").Handler(fs) 19 | 20 | return &ExampleRouter{ 21 | Router: r, 22 | } 23 | } 24 | 25 | func main() { 26 | http.Handle("/", NewExampleRouter()) 27 | 28 | log.Println("Serving on port 8000") 29 | err := http.ListenAndServe(":8000", nil) 30 | if err != nil { 31 | log.Fatalf("Server exited with: %v", err) 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /0-base/web/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 |
Hello cats!
7 |
8 | 9 | 10 | -------------------------------------------------------------------------------- /0-base/web/pets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tilt-dev/tilt-example-go/ec079fd2a4df431064579687bd49e03dcaa3de69/0-base/web/pets.png -------------------------------------------------------------------------------- /1-measured/Tiltfile: -------------------------------------------------------------------------------- 1 | # -*- mode: Python -*- 2 | 3 | # Records the current time, then kicks off a server update. 4 | # Normally, you would let Tilt do deploys automatically, but this 5 | # shows you how to set up a custom workflow that measures it. 6 | local_resource( 7 | 'deploy', 8 | 'python3 record-start-time.py', 9 | ) 10 | 11 | docker_build('example-go-image', '.', dockerfile='deployments/Dockerfile') 12 | k8s_yaml('deployments/kubernetes.yaml') 13 | k8s_resource('example-go', port_forwards=8000, resource_deps=['deploy']) 14 | -------------------------------------------------------------------------------- /1-measured/deployments/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM golang:1.21 2 | WORKDIR /app 3 | ADD . . 4 | RUN go install ./ 5 | ENTRYPOINT tilt-example-go 6 | -------------------------------------------------------------------------------- /1-measured/deployments/kubernetes.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: example-go 5 | labels: 6 | app: example-go 7 | spec: 8 | selector: 9 | matchLabels: 10 | app: example-go 11 | template: 12 | metadata: 13 | labels: 14 | app: example-go 15 | spec: 16 | containers: 17 | - name: example-go 18 | image: example-go-image 19 | ports: 20 | - containerPort: 8000 21 | -------------------------------------------------------------------------------- /1-measured/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tilt-dev/tilt-example-go 2 | 3 | go 1.21 4 | 5 | require github.com/gorilla/mux v1.8.0 6 | -------------------------------------------------------------------------------- /1-measured/go.sum: -------------------------------------------------------------------------------- 1 | github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= 2 | github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 3 | -------------------------------------------------------------------------------- /1-measured/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "html/template" 5 | "log" 6 | "net/http" 7 | "time" 8 | 9 | "github.com/gorilla/mux" 10 | ) 11 | 12 | func calcUpdateDuration() time.Duration { 13 | if StartTime.IsZero() { 14 | return 0 15 | } 16 | return time.Since(StartTime) 17 | } 18 | 19 | type ExampleRouter struct { 20 | *mux.Router 21 | tmpl *template.Template 22 | updateDuration time.Duration 23 | } 24 | 25 | func NewExampleRouter() (*ExampleRouter, error) { 26 | r := mux.NewRouter() 27 | 28 | tmpl, err := template.ParseGlob("./web/templates/*.tmpl") 29 | if err != nil { 30 | return nil, err 31 | } 32 | 33 | updateDuration := calcUpdateDuration() 34 | router := &ExampleRouter{ 35 | Router: r, 36 | tmpl: tmpl, 37 | updateDuration: updateDuration, 38 | } 39 | 40 | fs := http.FileServer(http.Dir("./web")) 41 | r.HandleFunc("/", router.index) 42 | r.PathPrefix("/").Handler(fs) 43 | 44 | return router, nil 45 | } 46 | 47 | func (r *ExampleRouter) updateTimeDisplay() string { 48 | if r.updateDuration != 0 { 49 | return r.updateDuration.Truncate(100 * time.Millisecond).String() 50 | } 51 | return "N/A" 52 | } 53 | 54 | func (r *ExampleRouter) index(w http.ResponseWriter, req *http.Request) { 55 | err := r.tmpl.ExecuteTemplate(w, "index.tmpl", map[string]string{ 56 | "Duration": r.updateTimeDisplay(), 57 | }) 58 | if err != nil { 59 | log.Printf("index: %v") 60 | } 61 | } 62 | 63 | func main() { 64 | router, err := NewExampleRouter() 65 | if err != nil { 66 | log.Fatalf("Router creation failed: %v", err) 67 | } 68 | http.Handle("/", router) 69 | 70 | log.Println("Serving on port 8000") 71 | log.Printf("Deploy time: %s\n", router.updateTimeDisplay()) 72 | err = http.ListenAndServe(":8000", nil) 73 | if err != nil { 74 | log.Fatalf("Server exited with: %v", err) 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /1-measured/record-start-time.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | contents = """package main 4 | 5 | import "time" 6 | 7 | var StartTime = time.Unix(0, %d) 8 | """ % int(time.time() * 1000 * 1000 * 1000) 9 | 10 | f = open("start.go", "w") 11 | f.write(contents) 12 | f.close() 13 | -------------------------------------------------------------------------------- /1-measured/start.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "time" 4 | 5 | var StartTime = time.Unix(0, 1589419929834305792) 6 | -------------------------------------------------------------------------------- /1-measured/web/pets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tilt-dev/tilt-example-go/ec079fd2a4df431064579687bd49e03dcaa3de69/1-measured/web/pets.png -------------------------------------------------------------------------------- /1-measured/web/templates/index.tmpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 |
Hello cats!
7 |
Time from "deploy" button pressed → server up: {{.Duration}}
8 |
9 | 10 | 11 | -------------------------------------------------------------------------------- /2-optimized/Tiltfile: -------------------------------------------------------------------------------- 1 | # -*- mode: Python -*- 2 | 3 | # Records the current time, then kicks off a server update. 4 | # Normally, you would let Tilt do deploys automatically, but this 5 | # shows you how to set up a custom workflow that measures it. 6 | local_resource( 7 | 'deploy', 8 | 'python3 record-start-time.py', 9 | ) 10 | 11 | compile_cmd = 'CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/tilt-example-go ./' 12 | if os.name == 'nt': 13 | compile_cmd = 'build.bat' 14 | 15 | local_resource( 16 | 'example-go-compile', 17 | compile_cmd, 18 | deps=['./main.go', './start.go'], 19 | resource_deps = ['deploy']) 20 | 21 | docker_build( 22 | 'example-go-image', 23 | '.', 24 | dockerfile='deployments/Dockerfile', 25 | only=[ 26 | './build', 27 | './web', 28 | ]) 29 | 30 | k8s_yaml('deployments/kubernetes.yaml') 31 | k8s_resource('example-go', port_forwards=8000, 32 | resource_deps=['deploy', 'example-go-compile']) 33 | -------------------------------------------------------------------------------- /2-optimized/build.bat: -------------------------------------------------------------------------------- 1 | set CGO_ENABLED=0 2 | set GOOS=linux 3 | set GOARCH=amd64 4 | go build -o build/tilt-example-go ./ -------------------------------------------------------------------------------- /2-optimized/deployments/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine 2 | WORKDIR /app 3 | ADD web web 4 | ADD build build 5 | ENTRYPOINT build/tilt-example-go 6 | -------------------------------------------------------------------------------- /2-optimized/deployments/kubernetes.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: example-go 5 | labels: 6 | app: example-go 7 | spec: 8 | selector: 9 | matchLabels: 10 | app: example-go 11 | template: 12 | metadata: 13 | labels: 14 | app: example-go 15 | spec: 16 | containers: 17 | - name: example-go 18 | image: example-go-image 19 | ports: 20 | - containerPort: 8000 21 | -------------------------------------------------------------------------------- /2-optimized/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tilt-dev/tilt-example-go 2 | 3 | go 1.21 4 | 5 | require github.com/gorilla/mux v1.8.0 6 | -------------------------------------------------------------------------------- /2-optimized/go.sum: -------------------------------------------------------------------------------- 1 | github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= 2 | github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 3 | -------------------------------------------------------------------------------- /2-optimized/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "html/template" 5 | "log" 6 | "net/http" 7 | "time" 8 | 9 | "github.com/gorilla/mux" 10 | ) 11 | 12 | func calcUpdateDuration() time.Duration { 13 | if StartTime.IsZero() { 14 | return 0 15 | } 16 | return time.Since(StartTime) 17 | } 18 | 19 | type ExampleRouter struct { 20 | *mux.Router 21 | tmpl *template.Template 22 | updateDuration time.Duration 23 | } 24 | 25 | func NewExampleRouter() (*ExampleRouter, error) { 26 | r := mux.NewRouter() 27 | 28 | tmpl, err := template.ParseGlob("./web/templates/*.tmpl") 29 | if err != nil { 30 | return nil, err 31 | } 32 | 33 | updateDuration := calcUpdateDuration() 34 | router := &ExampleRouter{ 35 | Router: r, 36 | tmpl: tmpl, 37 | updateDuration: updateDuration, 38 | } 39 | 40 | fs := http.FileServer(http.Dir("./web")) 41 | r.HandleFunc("/", router.index) 42 | r.PathPrefix("/").Handler(fs) 43 | 44 | return router, nil 45 | } 46 | 47 | func (r *ExampleRouter) updateTimeDisplay() string { 48 | if r.updateDuration != 0 { 49 | return r.updateDuration.Truncate(100 * time.Millisecond).String() 50 | } 51 | return "N/A" 52 | } 53 | 54 | func (r *ExampleRouter) index(w http.ResponseWriter, req *http.Request) { 55 | err := r.tmpl.ExecuteTemplate(w, "index.tmpl", map[string]string{ 56 | "Duration": r.updateTimeDisplay(), 57 | }) 58 | if err != nil { 59 | log.Printf("index: %v") 60 | } 61 | } 62 | 63 | func main() { 64 | router, err := NewExampleRouter() 65 | if err != nil { 66 | log.Fatalf("Router creation failed: %v", err) 67 | } 68 | http.Handle("/", router) 69 | 70 | log.Println("Serving on port 8000") 71 | log.Printf("Deploy time: %s\n", router.updateTimeDisplay()) 72 | err = http.ListenAndServe(":8000", nil) 73 | if err != nil { 74 | log.Fatalf("Server exited with: %v", err) 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /2-optimized/record-start-time.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | contents = """package main 4 | 5 | import "time" 6 | 7 | var StartTime = time.Unix(0, %d) 8 | """ % int(time.time() * 1000 * 1000 * 1000) 9 | 10 | f = open("start.go", "w") 11 | f.write(contents) 12 | f.close() 13 | -------------------------------------------------------------------------------- /2-optimized/start.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "time" 4 | 5 | var StartTime = time.Unix(0, 1589419933917529856) 6 | -------------------------------------------------------------------------------- /2-optimized/web/pets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tilt-dev/tilt-example-go/ec079fd2a4df431064579687bd49e03dcaa3de69/2-optimized/web/pets.png -------------------------------------------------------------------------------- /2-optimized/web/templates/index.tmpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 |
Hello cats!
7 |
Time from "deploy" button pressed → server up: {{.Duration}}
8 |
9 | 10 | 11 | -------------------------------------------------------------------------------- /3-recommended/Tiltfile: -------------------------------------------------------------------------------- 1 | # -*- mode: Python -*- 2 | 3 | # For more on Extensions, see: https://docs.tilt.dev/extensions.html 4 | load('ext://restart_process', 'docker_build_with_restart') 5 | 6 | # Records the current time, then kicks off a server update. 7 | # Normally, you would let Tilt do deploys automatically, but this 8 | # shows you how to set up a custom workflow that measures it. 9 | local_resource( 10 | 'deploy', 11 | 'python3 record-start-time.py', 12 | ) 13 | 14 | compile_cmd = 'CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/tilt-example-go ./' 15 | if os.name == 'nt': 16 | compile_cmd = 'build.bat' 17 | 18 | local_resource( 19 | 'example-go-compile', 20 | compile_cmd, 21 | deps=['./main.go', './start.go'], 22 | resource_deps=['deploy']) 23 | 24 | docker_build_with_restart( 25 | 'example-go-image', 26 | '.', 27 | entrypoint=['/app/build/tilt-example-go'], 28 | dockerfile='deployments/Dockerfile', 29 | only=[ 30 | './build', 31 | './web', 32 | ], 33 | live_update=[ 34 | sync('./build', '/app/build'), 35 | sync('./web', '/app/web'), 36 | ], 37 | ) 38 | 39 | k8s_yaml('deployments/kubernetes.yaml') 40 | k8s_resource('example-go', port_forwards=8000, 41 | resource_deps=['deploy', 'example-go-compile']) 42 | -------------------------------------------------------------------------------- /3-recommended/build.bat: -------------------------------------------------------------------------------- 1 | set CGO_ENABLED=0 2 | set GOOS=linux 3 | set GOARCH=amd64 4 | go build -o build/tilt-example-go ./ -------------------------------------------------------------------------------- /3-recommended/deployments/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine 2 | WORKDIR /app 3 | ADD web web 4 | ADD build build 5 | ENTRYPOINT build/tilt-example-go 6 | -------------------------------------------------------------------------------- /3-recommended/deployments/kubernetes.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: example-go 5 | labels: 6 | app: example-go 7 | spec: 8 | selector: 9 | matchLabels: 10 | app: example-go 11 | template: 12 | metadata: 13 | labels: 14 | app: example-go 15 | spec: 16 | containers: 17 | - name: example-go 18 | image: example-go-image 19 | ports: 20 | - containerPort: 8000 21 | -------------------------------------------------------------------------------- /3-recommended/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tilt-dev/tilt-example-go 2 | 3 | go 1.21 4 | 5 | require github.com/gorilla/mux v1.8.0 6 | -------------------------------------------------------------------------------- /3-recommended/go.sum: -------------------------------------------------------------------------------- 1 | github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= 2 | github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 3 | -------------------------------------------------------------------------------- /3-recommended/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "html/template" 5 | "log" 6 | "net/http" 7 | "time" 8 | 9 | "github.com/gorilla/mux" 10 | ) 11 | 12 | func calcUpdateDuration() time.Duration { 13 | if StartTime.IsZero() { 14 | return 0 15 | } 16 | return time.Since(StartTime) 17 | } 18 | 19 | type ExampleRouter struct { 20 | *mux.Router 21 | tmpl *template.Template 22 | updateDuration time.Duration 23 | } 24 | 25 | func NewExampleRouter() (*ExampleRouter, error) { 26 | r := mux.NewRouter() 27 | 28 | tmpl, err := template.ParseGlob("./web/templates/*.tmpl") 29 | if err != nil { 30 | return nil, err 31 | } 32 | 33 | updateDuration := calcUpdateDuration() 34 | router := &ExampleRouter{ 35 | Router: r, 36 | tmpl: tmpl, 37 | updateDuration: updateDuration, 38 | } 39 | 40 | fs := http.FileServer(http.Dir("./web")) 41 | r.HandleFunc("/", router.index) 42 | r.PathPrefix("/").Handler(fs) 43 | 44 | return router, nil 45 | } 46 | 47 | func (r *ExampleRouter) updateTimeDisplay() string { 48 | if r.updateDuration != 0 { 49 | return r.updateDuration.Truncate(100 * time.Millisecond).String() 50 | } 51 | return "N/A" 52 | } 53 | 54 | func (r *ExampleRouter) index(w http.ResponseWriter, req *http.Request) { 55 | congrats := r.updateDuration != 0 && r.updateDuration < 2*time.Second 56 | err := r.tmpl.ExecuteTemplate(w, "index.tmpl", map[string]interface{}{ 57 | "Duration": r.updateTimeDisplay(), 58 | "Congrats": congrats, 59 | }) 60 | if err != nil { 61 | log.Printf("index: %v", err) 62 | } 63 | } 64 | 65 | func main() { 66 | router, err := NewExampleRouter() 67 | if err != nil { 68 | log.Fatalf("Router creation failed: %v", err) 69 | } 70 | http.Handle("/", router) 71 | 72 | log.Println("Serving on port 8000") 73 | log.Printf("Deploy time: %s\n", router.updateTimeDisplay()) 74 | err = http.ListenAndServe(":8000", nil) 75 | if err != nil { 76 | log.Fatalf("Server exited with: %v", err) 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /3-recommended/record-start-time.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | contents = """package main 4 | 5 | import "time" 6 | 7 | var StartTime = time.Unix(0, %d) 8 | """ % int(time.time() * 1000 * 1000 * 1000) 9 | 10 | f = open("start.go", "w") 11 | f.write(contents) 12 | f.close() 13 | -------------------------------------------------------------------------------- /3-recommended/start.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "time" 4 | 5 | var StartTime = time.Unix(0, 1619813649804410880) 6 | -------------------------------------------------------------------------------- /3-recommended/tilt_modules/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "Extensions": [ 3 | { 4 | "Name": "restart_process", 5 | "ExtensionRegistry": "https://github.com/tilt-dev/tilt-extensions", 6 | "TimeFetched": "2021-04-30T16:14:09.519926-04:00" 7 | } 8 | ] 9 | } -------------------------------------------------------------------------------- /3-recommended/tilt_modules/restart_process/Tiltfile: -------------------------------------------------------------------------------- 1 | RESTART_FILE = '/tmp/.restart-proc' 2 | TYPE_RESTART_CONTAINER_STEP = 'live_update_restart_container_step' 3 | 4 | KWARGS_BLACKLIST = [ 5 | # since we'll be passing `dockerfile_contents` when building the 6 | # child image, remove any kwargs that might conflict 7 | 'dockerfile', 'dockerfile_contents', 8 | 9 | # 'target' isn't relevant to our child build--if we pass this arg, 10 | # Docker will just fail to find the specified stage and error out 11 | 'target', 12 | ] 13 | 14 | # Arguments to custom_build that don't apply to the docker_build. 15 | _CUSTOM_BUILD_KWARGS_BLACKLIST = [ 16 | 'tag', 17 | 'command_bat', 18 | 'outputs_image_ref_to', 19 | 'disable_push', 20 | ] 21 | 22 | _ext_dir = os.getcwd() 23 | 24 | # shared code between the two restart functions 25 | def _helper(base_ref, ref, entrypoint, live_update, restart_file=RESTART_FILE, trigger=None, **kwargs): 26 | if not trigger: 27 | trigger = [] 28 | 29 | # declare a new docker build that adds a static binary of tilt-restart-wrapper 30 | # (which makes use of `entr` to watch files and restart processes) to the user's image 31 | df = ''' 32 | FROM tiltdev/restart-helper:2020-10-16 as restart-helper 33 | 34 | FROM {} 35 | RUN ["touch", "{}"] 36 | COPY --from=restart-helper /tilt-restart-wrapper / 37 | COPY --from=restart-helper /entr / 38 | '''.format(base_ref, restart_file) 39 | 40 | # Change the entrypoint to use `tilt-restart-wrapper`. 41 | # `tilt-restart-wrapper` makes use of `entr` (https://github.com/eradman/entr/) to 42 | # re-execute $entrypoint whenever $restart_file changes 43 | if type(entrypoint) == type(""): 44 | entrypoint_with_entr = ["/tilt-restart-wrapper", "--watch_file={}".format(restart_file), "sh", "-c", entrypoint] 45 | elif type(entrypoint) == type([]): 46 | entrypoint_with_entr = ["/tilt-restart-wrapper", "--watch_file={}".format(restart_file)] + entrypoint 47 | else: 48 | fail("`entrypoint` must be a string or list of strings: got {}".format(type(entrypoint))) 49 | 50 | # last live_update step should always be to modify $restart_file, which 51 | # triggers the process wrapper to rerun $entrypoint 52 | # NB: write `date` instead of just `touch`ing because `entr` doesn't respond 53 | # to timestamp changes, only writes (see https://github.com/eradman/entr/issues/32) 54 | live_update = live_update + [run('date > {}'.format(restart_file), trigger=trigger)] 55 | 56 | # We don't need a real context. See: 57 | # https://github.com/tilt-dev/tilt/issues/3897 58 | context = _ext_dir 59 | 60 | docker_build(ref, context, entrypoint=entrypoint_with_entr, dockerfile_contents=df, 61 | live_update=live_update, **kwargs) 62 | 63 | def docker_build_with_restart(ref, context, entrypoint, live_update, 64 | base_suffix='-tilt_docker_build_with_restart_base', restart_file=RESTART_FILE, 65 | trigger=None, **kwargs): 66 | """Wrap a docker_build call and its associated live_update steps so that the last step 67 | of any live update is to rerun the given entrypoint. 68 | 69 | Args: 70 | ref: name for this image (e.g. 'myproj/backend' or 'myregistry/myproj/backend'); as the parameter of the same name in docker_build 71 | context: path to use as the Docker build context; as the parameter of the same name in docker_build 72 | entrypoint: the command to be (re-)executed when the container starts or when a live_update is run 73 | live_update: set of steps for updating a running container; as the parameter of the same name in docker_build 74 | base_suffix: suffix for naming the base image, applied as {ref}{base_suffix} 75 | restart_file: file that Tilt will update during a live_update to signal the entrypoint to rerun 76 | trigger: (optional) list of local paths. If specified, the process will ONLY be restarted when there are changes 77 | to the given file(s); as the parameter of the same name in the LiveUpdate `run` step. 78 | **kwargs: will be passed to the underlying `docker_build` call 79 | """ 80 | 81 | # first, validate the given live_update steps 82 | if len(live_update) == 0: 83 | fail("`docker_build_with_restart` requires at least one live_update step") 84 | for step in live_update: 85 | if type(step) == TYPE_RESTART_CONTAINER_STEP: 86 | fail("`docker_build_with_restart` is not compatible with live_update step: " + 87 | "`restart_container()` (this extension is meant to REPLACE restart_container() )") 88 | 89 | # rename the original image to make it a base image and declare a docker_build for it 90 | base_ref = '{}{}'.format(ref, base_suffix) 91 | docker_build(base_ref, context, **kwargs) 92 | 93 | # Clean kwargs for building the child image (which builds on user's specified 94 | # image and copies in Tilt's restart wrapper). In practice, this means removing 95 | # kwargs that were relevant to building the user's specified image but are NOT 96 | # relevant to building the child image / may conflict with args we specifically 97 | # pass for the child image. 98 | cleaned_kwargs = {k: v for k, v in kwargs.items() if k not in KWARGS_BLACKLIST} 99 | _helper(base_ref, ref, entrypoint, live_update, restart_file, trigger, **cleaned_kwargs) 100 | 101 | 102 | def custom_build_with_restart(ref, command, deps, entrypoint, live_update, 103 | base_suffix='-tilt_docker_build_with_restart_base', restart_file=RESTART_FILE, 104 | trigger=None, **kwargs): 105 | """Wrap a custom_build call and its associated live_update steps so that the last step 106 | of any live update is to rerun the given entrypoint. 107 | 108 | Args: 109 | ref: name for this image (e.g. 'myproj/backend' or 'myregistry/myproj/backend'); as the parameter of the same name in custom_build 110 | command: build command for building your image 111 | deps: source dependencies of the custom build 112 | entrypoint: the command to be (re-)executed when the container starts or when a live_update is run 113 | live_update: set of steps for updating a running container; as the parameter of the same name in custom_build 114 | base_suffix: suffix for naming the base image, applied as {ref}{base_suffix} 115 | restart_file: file that Tilt will update during a live_update to signal the entrypoint to rerun 116 | trigger: (optional) list of local paths. If specified, the process will ONLY be restarted when there are changes 117 | to the given file(s); as the parameter of the same name in the LiveUpdate `run` step. 118 | **kwargs: will be passed to the underlying `custom_build` call 119 | """ 120 | 121 | # first, validate the given live_update steps 122 | if len(live_update) == 0: 123 | fail("`custom_build_with_restart` requires at least one live_update step") 124 | for step in live_update: 125 | if type(step) == TYPE_RESTART_CONTAINER_STEP: 126 | fail("`custom_build_with_restart` is not compatible with live_update step: "+ 127 | "`restart_container()` (this extension is meant to REPLACE restart_container() )") 128 | 129 | for k, v in kwargs.items(): 130 | if k == 'skips_local_docker': 131 | fail("`custom_build_with_restart` is not compatible with `skips_local_docker`, because it needs access to the image") 132 | if k == 'disable_push': 133 | fail("`custom_build_with_restart` is not compatible with `disable_push`") 134 | if k == 'tag': 135 | fail("`custom_build_with_restart` renames your base image, so is not compatible with `tag`") 136 | 137 | # rename the original image to make it a base image and declare a custom_build for it 138 | base_ref = '{}{}'.format(ref, base_suffix) 139 | custom_build(base_ref, command=command, deps=deps, **kwargs) 140 | 141 | # A few arguments aren't applicable to the docker_build, so remove them. 142 | cleaned_kwargs = {k: v for k, v in kwargs.items() if k not in _CUSTOM_BUILD_KWARGS_BLACKLIST} 143 | _helper(base_ref, ref, entrypoint, live_update, restart_file, trigger, **cleaned_kwargs) 144 | -------------------------------------------------------------------------------- /3-recommended/web/pets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tilt-dev/tilt-example-go/ec079fd2a4df431064579687bd49e03dcaa3de69/3-recommended/web/pets.png -------------------------------------------------------------------------------- /3-recommended/web/templates/index.tmpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | {{if .Congrats}} 7 |
Congratulations, you set up live_update!
8 | {{else}} 9 |
Hello cats!
10 | {{end}} 11 | 12 |
Time from "deploy" button pressed → server up: {{.Duration}}
13 |
14 | 15 | 16 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /NOTICE: -------------------------------------------------------------------------------- 1 | Tilt Example Go 2 | Copyright 2022 Docker, Inc. 3 | 4 | This product includes software developed at Docker, Inc. (https://www.docker.com). 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tilt-example-go 2 | 3 | [![Build Status](https://circleci.com/gh/tilt-dev/tilt-example-go/tree/master.svg?style=shield)](https://circleci.com/gh/tilt-dev/tilt-example-go) 4 | 5 | An example project that demonstrates a live-updating Go server in Kubernetes. Read [doc](https://docs.tilt.dev/example_go.html). 6 | 7 | - [0-base](0-base): The simplest way to start 8 | - [1-measured](1-measured): Use `local_resource` to measure your deployment time 9 | - [2-optimized](2-optimized): Compile Go binaries and copy them into Docker 10 | - [3-recommended](3-recommended): Live-update compiled binaries 11 | 12 | ## Other Examples 13 | - [tests-example](tests-example): an example of how to use Tilt to run your tests for you as you iterate 14 | 15 | ## License 16 | 17 | Copyright 2022 Docker, Inc. 18 | 19 | Licensed under [the Apache License, Version 2.0](LICENSE) 20 | -------------------------------------------------------------------------------- /test/test.ps1: -------------------------------------------------------------------------------- 1 | 2 | echo "Testing 0-base" 3 | tilt ci --file 0-base/Tiltfile 4 | if (!$?) { 5 | throw "failed" 6 | } 7 | tilt down --file 0-base/Tiltfile 8 | 9 | echo "Testing 1-measured" 10 | tilt ci --file 1-measured/Tiltfile 11 | if (!$?) { 12 | throw "failed" 13 | } 14 | tilt down --file 1-measured/Tiltfile 15 | 16 | echo "Testing 2-optimized" 17 | tilt ci --file 2-optimized/Tiltfile 18 | if (!$?) { 19 | throw "failed" 20 | } 21 | tilt down --file 2-optimized/Tiltfile 22 | 23 | echo "Testing 3-recommended" 24 | tilt ci --file 3-recommended/Tiltfile 25 | if (!$?) { 26 | throw "failed" 27 | } 28 | tilt down --file 3-recommended/Tiltfile 29 | -------------------------------------------------------------------------------- /test/test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -ex 4 | 5 | # cd to the root of the repo. 6 | cd $(dirname $(dirname $0)) 7 | 8 | echo "Testing 0-base" 9 | tilt ci --file 0-base/Tiltfile 10 | tilt down --file 0-base/Tiltfile 11 | 12 | echo "Testing 1-measured" 13 | tilt ci --file 1-measured/Tiltfile 14 | tilt down --file 1-measured/Tiltfile 15 | 16 | echo "Testing 2-optimized" 17 | tilt ci --file 2-optimized/Tiltfile 18 | tilt down --file 2-optimized/Tiltfile 19 | 20 | echo "Testing 3-recommended" 21 | tilt ci --file 3-recommended/Tiltfile 22 | tilt down --file 3-recommended/Tiltfile 23 | 24 | echo "Testing tests-example" 25 | tilt ci --file tests-example/Tiltfile 26 | tilt down --file tests-example/Tiltfile 27 | -------------------------------------------------------------------------------- /tests-example/Tiltfile: -------------------------------------------------------------------------------- 1 | # -*- mode: Python -*- 2 | 3 | # For more on Extensions, see: https://docs.tilt.dev/extensions.html 4 | load('ext://restart_process', 'docker_build_with_restart') 5 | 6 | # Tilt can run your tests for you, so you get fast feedback while iterating on your app 7 | include('Tiltfile.tests') 8 | 9 | # Records the current time, then kicks off a server update. 10 | # Normally, you would let Tilt do deploys automatically, but this 11 | # shows you how to set up a custom workflow that measures it. 12 | local_resource( 13 | 'deploy', 14 | 'python3 record-start-time.py', 15 | ) 16 | 17 | compile_cmd = 'CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o build/tilt-example-go ./' 18 | if os.name == 'nt': 19 | compile_cmd = 'build.bat' 20 | 21 | local_resource( 22 | 'example-go-compile', 23 | compile_cmd, 24 | deps=['./main.go', './start.go'], 25 | resource_deps=['deploy']) 26 | 27 | docker_build_with_restart( 28 | 'example-go-image', 29 | '.', 30 | entrypoint=['/app/build/tilt-example-go'], 31 | dockerfile='deployments/Dockerfile', 32 | only=[ 33 | './build', 34 | './web', 35 | ], 36 | live_update=[ 37 | sync('./build', '/app/build'), 38 | sync('./web', '/app/web'), 39 | ], 40 | ) 41 | 42 | k8s_yaml('deployments/kubernetes.yaml') 43 | k8s_resource('example-go', port_forwards=8000, 44 | resource_deps=['deploy', 'example-go-compile']) 45 | -------------------------------------------------------------------------------- /tests-example/Tiltfile.tests: -------------------------------------------------------------------------------- 1 | # -*- mode: Python -*- 2 | 3 | # For more on the `test_go` extension: 4 | # https://github.com/tilt-dev/tilt-extensions/tree/master/tests/golang 5 | # For more on tests in Tilt: https://docs.tilt.dev/tests_in_tilt.html 6 | load('ext://tests/golang', 'test_go') 7 | 8 | # these unit tests are fast/well cached, so it's easy to run them all whenever 9 | # a file changes and get fast feedback 10 | # (The `skipintegration` tag prevents this test resource from running our very 11 | # slow integration test suite) 12 | test_go('unit-tests', '.', '.', 13 | recursive=True, tags=['skipintegration']) 14 | 15 | # the integration tests are slow and clunky, so make them available from the sidebar 16 | # and let the developer run them manually whenever it's necessary 17 | test_go('integration-tests', './integration', '.', 18 | extra_args=["-v"], 19 | trigger_mode=TRIGGER_MODE_MANUAL, 20 | 21 | # run this test automatically in CI mode; otherwise, only on manual trigger 22 | auto_init=config.tilt_subcommand == 'ci') 23 | -------------------------------------------------------------------------------- /tests-example/build.bat: -------------------------------------------------------------------------------- 1 | set CGO_ENABLED=0 2 | set GOOS=linux 3 | set GOARCH=amd64 4 | go build -o build/tilt-example-go ./ -------------------------------------------------------------------------------- /tests-example/deployments/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine 2 | WORKDIR /app 3 | ADD web web 4 | ADD build build 5 | ENTRYPOINT build/tilt-example-go 6 | -------------------------------------------------------------------------------- /tests-example/deployments/kubernetes.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: example-go 5 | labels: 6 | app: example-go 7 | spec: 8 | selector: 9 | matchLabels: 10 | app: example-go 11 | template: 12 | metadata: 13 | labels: 14 | app: example-go 15 | spec: 16 | containers: 17 | - name: example-go 18 | image: example-go-image 19 | ports: 20 | - containerPort: 8000 21 | -------------------------------------------------------------------------------- /tests-example/go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tilt-dev/tilt-example-go 2 | 3 | go 1.21 4 | 5 | require github.com/gorilla/mux v1.8.0 6 | -------------------------------------------------------------------------------- /tests-example/go.sum: -------------------------------------------------------------------------------- 1 | github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= 2 | github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= 3 | -------------------------------------------------------------------------------- /tests-example/integration/integration_test.go: -------------------------------------------------------------------------------- 1 | // +build !skipintegration 2 | 3 | package integration 4 | 5 | import ( 6 | "fmt" 7 | "testing" 8 | "time" 9 | ) 10 | 11 | func TestSomethingSlow(t *testing.T) { 12 | fmt.Println("Transponder log partitioned") 13 | time.Sleep(time.Second) 14 | fmt.Println("Cascading generator transistorized") 15 | time.Sleep(time.Second) 16 | fmt.Println("Checking the harmonix proxy processor") 17 | time.Sleep(time.Second) 18 | fmt.Println("Is network capacity inverted?") 19 | time.Sleep(time.Second) 20 | fmt.Println("\t- transmission technician acquired") 21 | time.Sleep(time.Second) 22 | fmt.Println("\t- transmission technician acquired") 23 | time.Sleep(time.Second) 24 | fmt.Println("All is well!") 25 | } 26 | 27 | func TestOtherSlowThing(t *testing.T) { 28 | fmt.Println("Verifying scan bus frequency") 29 | time.Sleep(time.Second) 30 | fmt.Println("Logisticating data") 31 | time.Sleep(time.Second) 32 | fmt.Println("Broadband plasma may be dangerous, performing safety check") 33 | time.Sleep(time.Second) 34 | fmt.Println("Watch out for geese") 35 | time.Sleep(time.Second) 36 | fmt.Println("All is well!") 37 | } 38 | -------------------------------------------------------------------------------- /tests-example/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "html/template" 5 | "log" 6 | "net/http" 7 | "time" 8 | 9 | "github.com/gorilla/mux" 10 | ) 11 | 12 | func calcUpdateDuration() time.Duration { 13 | if StartTime.IsZero() { 14 | return 0 15 | } 16 | return time.Since(StartTime) 17 | } 18 | 19 | type ExampleRouter struct { 20 | *mux.Router 21 | tmpl *template.Template 22 | updateDuration time.Duration 23 | } 24 | 25 | func NewExampleRouter() (*ExampleRouter, error) { 26 | r := mux.NewRouter() 27 | 28 | tmpl, err := template.ParseGlob("./web/templates/*.tmpl") 29 | if err != nil { 30 | return nil, err 31 | } 32 | 33 | updateDuration := calcUpdateDuration() 34 | router := &ExampleRouter{ 35 | Router: r, 36 | tmpl: tmpl, 37 | updateDuration: updateDuration, 38 | } 39 | 40 | fs := http.FileServer(http.Dir("./web")) 41 | r.HandleFunc("/", router.index) 42 | r.PathPrefix("/").Handler(fs) 43 | 44 | return router, nil 45 | } 46 | 47 | func (r *ExampleRouter) updateTimeDisplay() string { 48 | if r.updateDuration != 0 { 49 | return r.updateDuration.Truncate(100 * time.Millisecond).String() 50 | } 51 | return "N/A" 52 | } 53 | 54 | func (r *ExampleRouter) index(w http.ResponseWriter, req *http.Request) { 55 | congrats := r.updateDuration != 0 && r.updateDuration < 2*time.Second 56 | err := r.tmpl.ExecuteTemplate(w, "index.tmpl", map[string]interface{}{ 57 | "Duration": r.updateTimeDisplay(), 58 | "Congrats": congrats, 59 | }) 60 | if err != nil { 61 | log.Printf("index: %v", err) 62 | } 63 | } 64 | 65 | func main() { 66 | router, err := NewExampleRouter() 67 | if err != nil { 68 | log.Fatalf("Router creation failed: %v", err) 69 | } 70 | http.Handle("/", router) 71 | 72 | log.Println("Serving on port 8000") 73 | log.Printf("Deploy time: %s\n", router.updateTimeDisplay()) 74 | err = http.ListenAndServe(":8000", nil) 75 | if err != nil { 76 | log.Fatalf("Server exited with: %v", err) 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /tests-example/main_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "testing" 5 | "time" 6 | ) 7 | 8 | func TestUpdateTimeDisplayNonzero(t *testing.T) { 9 | r, err := NewExampleRouter() 10 | if err != nil { 11 | t.Fatalf("error making new router: %v", err) 12 | } 13 | 14 | r.updateDuration = time.Second 15 | actual := r.updateTimeDisplay() 16 | expected := "1s" 17 | 18 | if expected != actual { 19 | t.Fatalf("expected update duration %s but got %s", expected, actual) 20 | } 21 | } 22 | 23 | func TestUpdateTimeNonzero(t *testing.T) { 24 | r, err := NewExampleRouter() 25 | if err != nil { 26 | t.Fatalf("error making new router: %v", err) 27 | } 28 | 29 | r.updateDuration = 0 30 | actual := r.updateTimeDisplay() 31 | expected := "N/A" 32 | 33 | if expected != actual { 34 | t.Fatalf("expected update duration %s but got %s", expected, actual) 35 | } 36 | } 37 | 38 | func TestUpdateTimeTruncate(t *testing.T) { 39 | r, err := NewExampleRouter() 40 | if err != nil { 41 | t.Fatalf("error making new router: %v", err) 42 | } 43 | 44 | r.updateDuration = time.Millisecond * 123 45 | actual := r.updateTimeDisplay() 46 | expected := "100ms" // truncate to nearest 100ms 47 | 48 | if expected != actual { 49 | t.Fatalf("expected update duration %s but got %s", expected, actual) 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /tests-example/record-start-time.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | contents = """package main 4 | 5 | import "time" 6 | 7 | var StartTime = time.Unix(0, %d) 8 | """ % int(time.time() * 1000 * 1000 * 1000) 9 | 10 | f = open("start.go", "w") 11 | f.write(contents) 12 | f.close() 13 | -------------------------------------------------------------------------------- /tests-example/start.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "time" 4 | 5 | var StartTime = time.Unix(0, 1617121029400116992) 6 | -------------------------------------------------------------------------------- /tests-example/subpackage/subpackage_test.go: -------------------------------------------------------------------------------- 1 | package subpackage 2 | 3 | import ( 4 | "fmt" 5 | "testing" 6 | ) 7 | 8 | func TestFoo(t *testing.T) { 9 | fmt.Println("foo is working") 10 | } 11 | 12 | func TestBar(t *testing.T) { 13 | fmt.Println("bar is working") 14 | } 15 | 16 | func TestBaz(t *testing.T) { 17 | fmt.Println("baz is working") 18 | } 19 | -------------------------------------------------------------------------------- /tests-example/web/pets.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tilt-dev/tilt-example-go/ec079fd2a4df431064579687bd49e03dcaa3de69/tests-example/web/pets.png -------------------------------------------------------------------------------- /tests-example/web/templates/index.tmpl: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 | 6 | {{if .Congrats}} 7 |
Congratulations, you set up live_update!
8 | {{else}} 9 |
Hello cats!
10 | {{end}} 11 | 12 |
Time from "deploy" button pressed → server up: {{.Duration}}
13 |
14 | 15 | 16 | --------------------------------------------------------------------------------