github.com/joshdk/godel@v0.0.0-20170529232908-862138a45aee/apps/distgo/cmd/docker/publish.go (about) 1 // Copyright 2016 Palantir Technologies, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package docker 16 17 import ( 18 "fmt" 19 "io" 20 "os/exec" 21 "path" 22 23 "github.com/pkg/errors" 24 25 "github.com/palantir/godel/apps/distgo/cmd/build" 26 "github.com/palantir/godel/apps/distgo/params" 27 ) 28 29 func Publish(cfg params.Project, wd string, baseRepo string, stdout io.Writer) error { 30 // find all products with docker images and tag them with correct version and push 31 var products []string 32 for productName, product := range cfg.Products { 33 if len(product.DockerImages) > 0 { 34 products = append(products, productName) 35 } 36 } 37 buildSpecsWithDeps, err := build.SpecsWithDepsForArgs(cfg, products, wd) 38 if err != nil { 39 return err 40 } 41 for _, specWithDeps := range buildSpecsWithDeps { 42 versionTag := specWithDeps.Spec.ProductVersion 43 for _, image := range specWithDeps.Spec.DockerImages { 44 repo := image.Repository 45 if baseRepo != "" { 46 repo = path.Join(baseRepo, repo) 47 } 48 buildTag := fmt.Sprintf("%s:%s", repo, image.Tag) 49 publishTag := fmt.Sprintf("%s:%s", repo, versionTag) 50 if err := tagImage(buildTag, publishTag); err != nil { 51 return err 52 } 53 if err := pushImage(publishTag); err != nil { 54 return err 55 } 56 } 57 } 58 return nil 59 } 60 61 func tagImage(original, new string) error { 62 var args []string 63 args = append(args, "tag") 64 args = append(args, original) 65 args = append(args, new) 66 67 buildCmd := exec.Command("docker", args...) 68 if output, err := buildCmd.CombinedOutput(); err != nil { 69 return errors.Wrap(err, fmt.Sprintf("docker tag failed with error:\n%s\n Make sure to run docker build before docker publish.\n", string(output))) 70 } 71 return nil 72 } 73 74 func pushImage(tag string) error { 75 var args []string 76 args = append(args, "push") 77 args = append(args, tag) 78 79 buildCmd := exec.Command("docker", args...) 80 if output, err := buildCmd.CombinedOutput(); err != nil { 81 return errors.Wrap(err, fmt.Sprintf("docker push failed with error:\n%s\n", string(output))) 82 } 83 return nil 84 }