enables multi-tag build and publish

This commit is contained in:
Brad Rydzewski 2015-10-27 17:53:51 -07:00
parent c17725f269
commit 54b0c1d9e0
2 changed files with 88 additions and 26 deletions

33
main.go
View File

@ -20,9 +20,8 @@ type Docker struct {
Email string `json:"email"` Email string `json:"email"`
Auth string `json:"auth"` Auth string `json:"auth"`
Repo string `json:"repo"` Repo string `json:"repo"`
Tag string `json:"tag"` Tag StrSlice `json:"tag"`
File string `json:"file"` File string `json:"file"`
// see more here https://docs.docker.com/reference/commandline/build/
Context string `json:"context"` Context string `json:"context"`
Dns []string `json:"dns"` Dns []string `json:"dns"`
} }
@ -57,10 +56,9 @@ func main() {
vargs.Context = "." vargs.Context = "."
} }
// Set the Tag value // Set the Tag value
if len(vargs.Tag) == 0 { if vargs.Tag.Len() == 0 {
vargs.Tag = "latest" vargs.Tag = StrSlice{[]string{"latest"}}
} }
vargs.Repo = fmt.Sprintf("%s:%s", vargs.Repo, vargs.Tag)
go func() { go func() {
args := []string{"-d"} args := []string{"-d"}
@ -128,7 +126,8 @@ func main() {
cmd.Run() cmd.Run()
// Build the container // Build the container
cmd = exec.Command("/usr/bin/docker", "build", "--pull=true", "--rm=true", "-f", vargs.File, "-t", vargs.Repo, vargs.Context) name := fmt.Sprintf("%s:%s", vargs.Repo, vargs.Tag.Slice()[0])
cmd = exec.Command("/usr/bin/docker", "build", "--pull=true", "--rm=true", "-f", vargs.File, "-t", name, vargs.Context)
cmd.Dir = workspace.Path cmd.Dir = workspace.Path
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -138,8 +137,10 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// Push the container // Creates image tags
cmd = exec.Command("/usr/bin/docker", "push", vargs.Repo) for _, tag := range vargs.Tag.Slice()[1:] {
name_ := fmt.Sprintf("%s:%s", vargs.Repo, tag)
cmd = exec.Command("/usr/bin/docker", "tag", name, name_)
cmd.Dir = workspace.Path cmd.Dir = workspace.Path
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -150,6 +151,22 @@ func main() {
} }
} }
// Push the image and tags to the registry
for _, tag := range vargs.Tag.Slice() {
name_ := fmt.Sprintf("%s:%s", vargs.Repo, tag)
cmd = exec.Command("/usr/bin/docker", "push", name_)
cmd.Dir = workspace.Path
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
trace(cmd)
err = cmd.Run()
if err != nil {
os.Exit(1)
}
}
}
// Trace writes each command to standard error (preceded by a $ ) before it // Trace writes each command to standard error (preceded by a $ ) before it
// is executed. Used for debugging your build. // is executed. Used for debugging your build.
func trace(cmd *exec.Cmd) { func trace(cmd *exec.Cmd) {

45
types.go Normal file
View File

@ -0,0 +1,45 @@
package main
import "encoding/json"
// StrSlice representes a string or an array of strings.
// We need to override the json decoder to accept both options.
type StrSlice struct {
parts []string
}
// UnmarshalJSON decodes the byte slice whether it's a string or an array of strings.
// This method is needed to implement json.Unmarshaler.
func (e *StrSlice) UnmarshalJSON(b []byte) error {
if len(b) == 0 {
return nil
}
p := make([]string, 0, 1)
if err := json.Unmarshal(b, &p); err != nil {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
p = append(p, s)
}
e.parts = p
return nil
}
// Len returns the number of parts of the StrSlice.
func (e *StrSlice) Len() int {
if e == nil {
return 0
}
return len(e.parts)
}
// Slice gets the parts of the StrSlice as a Slice of string.
func (e *StrSlice) Slice() []string {
if e == nil {
return nil
}
return e.parts
}