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

69
main.go
View File

@ -12,19 +12,18 @@ import (
) )
type Docker struct { type Docker struct {
Storage string `json:"storage_driver"` Storage string `json:"storage_driver"`
Registry string `json:"registry"` Registry string `json:"registry"`
Insecure bool `json:"insecure"` Insecure bool `json:"insecure"`
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
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"`
} }
func main() { func main() {
@ -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,16 +137,34 @@ 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:] {
cmd.Dir = workspace.Path name_ := fmt.Sprintf("%s:%s", vargs.Repo, tag)
cmd.Stdout = os.Stdout cmd = exec.Command("/usr/bin/docker", "tag", name, name_)
cmd.Stderr = os.Stderr cmd.Dir = workspace.Path
trace(cmd) cmd.Stdout = os.Stdout
err = cmd.Run() cmd.Stderr = os.Stderr
if err != nil { trace(cmd)
os.Exit(1) err = cmd.Run()
if err != nil {
os.Exit(1)
}
} }
// 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

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
}