diff --git a/config.go b/config.go index b4ad313d..f515b4d1 100644 --- a/config.go +++ b/config.go @@ -1,9 +1,11 @@ package main import ( + "crypto/md5" "errors" "fmt" "github.com/hashicorp/go-getter" + "io" "io/ioutil" "os" "os/exec" @@ -71,6 +73,23 @@ type TGFConfigBuild struct { source string } +func (cb TGFConfigBuild) hash() string { + h := md5.New() + io.WriteString(h, cb.Instructions) + if cb.Folder != "" { + filepath.Walk(cb.Dir(), func(path string, info os.FileInfo, err error) error { + if info == nil || info.IsDir() || err != nil { + return nil + } + if !strings.Contains(path, dockerfilePattern) { + io.WriteString(h, fmt.Sprintf("%v", info.ModTime())) + } + return nil + }) + } + return fmt.Sprintf("%x", h.Sum(nil)) +} + // Dir returns the folder name relative to the source func (cb TGFConfigBuild) Dir() string { if cb.Folder == "" { @@ -87,7 +106,7 @@ func (cb TGFConfigBuild) GetTag() string { if cb.Tag != "" { return cb.Tag } - return filepath.Base(filepath.Dir(cb.source)) + return fmt.Sprintf("%s-%s", filepath.Base(filepath.Dir(cb.source)), cb.hash()) } // InitConfig returns a properly initialized TGF configuration struct diff --git a/config_test.go b/config_test.go index f0826eae..e56e57c1 100644 --- a/config_test.go +++ b/config_test.go @@ -1,7 +1,9 @@ package main import ( + "crypto/md5" "fmt" + "io" "io/ioutil" "math/rand" "os" @@ -108,7 +110,7 @@ func TestSetConfigDefaultValues(t *testing.T) { assert.Equal(t, "RUN ls test", config.imageBuildConfigs[1].Instructions) assert.Equal(t, "/abspath/my-folder", config.imageBuildConfigs[1].Folder) assert.Equal(t, "/abspath/my-folder", config.imageBuildConfigs[1].Dir()) - assert.Equal(t, "AWS", config.imageBuildConfigs[1].GetTag()) + assert.Equal(t, "AWS-"+getHash(config.imageBuildConfigs[1].Instructions), config.imageBuildConfigs[1].GetTag()) assert.Equal(t, "coveo/stuff", config.Image) assert.Equal(t, "test", *config.ImageTag) @@ -191,3 +193,9 @@ func randInt() int { random := rand.New(source) return random.Int() } + +func getHash(value string) string { + h := md5.New() + io.WriteString(h, value) + return fmt.Sprintf("%x", h.Sum(nil)) +} diff --git a/docker.go b/docker.go index db43170d..49481067 100644 --- a/docker.go +++ b/docker.go @@ -32,6 +32,7 @@ const ( minimumDockerVersion = "1.25" tgfImageVersion = "TGF_IMAGE_VERSION" dockerSocketFile = "/var/run/docker.sock" + dockerfilePattern = "TGF_dockerfile" ) func callDocker(withDockerMount bool, args ...string) int { @@ -209,11 +210,11 @@ func getImage() (name string) { if ib.Folder == "" { // There is no explicit folder, so we create a temporary folder to store the docker file temp = must(ioutil.TempDir("", "tgf-dockerbuild")).(string) - out = must(os.Create(filepath.Join(temp, "Dockerfile"))).(*os.File) + out = must(os.Create(filepath.Join(temp, dockerfilePattern))).(*os.File) folder = temp } else { if ib.Instructions != "" { - out = must(ioutil.TempFile(ib.Dir(), "DockerFile")).(*os.File) + out = must(ioutil.TempFile(ib.Dir(), dockerfilePattern)).(*os.File) temp = out.Name() dockerFile = temp }