-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
109 lines (96 loc) · 2.71 KB
/
main.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"flag"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/moby/buildkit/frontend/dockerfile/instructions"
"github.com/moby/buildkit/frontend/dockerfile/parser"
)
func main() {
log.SetFlags(0)
log.SetPrefix("castage: ")
if err := run(); err != nil {
log.Fatal(err)
}
}
func run() error {
dockerfilePath := flag.String("f", "Dockerfile", "Dockerfile path")
imageName := flag.String("i", "", "image name (required)")
buildContext := flag.String("p", ".", "build context")
push := flag.Bool("push", false, "push")
buildKit := flag.Bool("buildkit", false, "BuildKit")
flag.Parse()
if *imageName == "" {
log.Print("-i <image name> is must be required")
invalidArgs()
}
stageNames, err := readStageNames(*dockerfilePath)
if err != nil {
return fmt.Errorf("read stage names: %w", err)
}
cachedStages := make([]string, 0, len(stageNames))
fmt.Println("set -ex")
if *buildKit {
fmt.Printf("docker build -t %s --cache-from=%s --build-arg BUILDKIT_INLINE_CACHE=1 %s\n", fmt.Sprintf("%s:%s", *imageName, stageNames[len(stageNames)-1]), strings.Join(stageNames, ","), *buildContext)
for i, stageName := range stageNames {
if i == len(stageNames)-1 {
break
}
fmt.Printf("docker build -t %s --target=%s --build-arg BUILDKIT_INLINE_CACHE=1 %s &\n", fmt.Sprintf("%s:%s", *imageName, stageName), stageName, *buildContext)
}
fmt.Println("wait")
if *push {
for _, stageName := range stageNames {
fmt.Printf("docker push %s &\n", fmt.Sprintf("%s:%s", *imageName, stageName))
}
fmt.Println("wait")
}
} else {
for _, stageName := range stageNames {
cachedStage := fmt.Sprintf("%s:%s", *imageName, stageName)
cachedStages = append(cachedStages, cachedStage)
fmt.Printf("docker pull %s || true\n", cachedStage)
fmt.Printf("docker build -t %s --target=%s --cache-from=%s %s\n", cachedStage, stageName, strings.Join(cachedStages, ","), *buildContext)
if *push {
fmt.Printf("docker push %s\n", cachedStage)
}
}
}
return nil
}
func readStageNames(dockerfilePath string) ([]string, error) {
var r io.Reader
if dockerfilePath == "-" {
r = os.Stdin
} else {
f, err := os.Open(dockerfilePath)
if err != nil {
return nil, fmt.Errorf("open Dockerfile: %w", err)
}
defer f.Close()
r = f
}
result, err := parser.Parse(r)
if err != nil {
return nil, fmt.Errorf("parse Dockerfile: %w", err)
}
stages, _, err := instructions.Parse(result.AST)
if err != nil {
return nil, fmt.Errorf("parse instructions: %w", err)
}
stageNames := make([]string, 0, len(stages))
for _, stage := range stages {
if stage.Name == "" {
continue
}
stageNames = append(stageNames, stage.Name)
}
return stageNames, nil
}
func invalidArgs() {
flag.Usage()
os.Exit(2)
}