Created
November 16, 2018 07:25
-
-
Save kunalkushwaha/bae5fdd062107a9379d476a034ffaa62 to your computer and use it in GitHub Desktop.
buildkit example which prints output of intermediate steps
This file contains hidden or 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
| package main | |
| import ( | |
| "bufio" | |
| "flag" | |
| "log" | |
| "os" | |
| "strings" | |
| "github.com/moby/buildkit/client/llb" | |
| ) | |
| const ( | |
| defaultImage = "docker.io/library/alpine:latest" | |
| ) | |
| func main() { | |
| scriptFile := flag.String("f", "", "input file") | |
| flag.Parse() | |
| if *scriptFile == "" { | |
| log.Fatal("requires input file", *scriptFile) | |
| os.Exit(1) | |
| } | |
| file, err := os.Open(*scriptFile) | |
| if err != nil { | |
| log.Fatal("cannot open file : ", *scriptFile) | |
| os.Exit(1) | |
| } | |
| defer file.Close() | |
| scanner := bufio.NewScanner(file) | |
| scanner.Scan() | |
| baseImage := scanner.Text() | |
| if baseImage == "" { | |
| baseImage = defaultImage | |
| } | |
| bk := buildkit(baseImage) | |
| for scanner.Scan() { | |
| cmd := scanner.Text() | |
| if strings.HasPrefix(cmd, "#") { | |
| continue | |
| } | |
| log.Println(cmd) | |
| out := bk.Run(llb.Shlex(cmd)).Root() // debug output | |
| dt, err := out.Marshal() | |
| if err != nil { | |
| panic(err) | |
| } | |
| llb.WriteTo(dt, os.Stdout) | |
| bk = out | |
| } | |
| } | |
| func goBuildBase(baseImage string) llb.State { | |
| goBase := llb.Image(baseImage) | |
| return goBase. | |
| AddEnv("http_proxy", os.Getenv("http_proxy")). | |
| AddEnv("https_proxy", os.Getenv("https_proxy")). | |
| Run(llb.Shlex("echo \"Buildkit Shell\"")).Root() | |
| } | |
| func buildkit(baseImage string) llb.State { | |
| return goBuildBase(baseImage) | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
input example