From a47d0d37c3294b831d9bd0aea4f5aa6827df724f Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Wed, 28 Nov 2018 07:58:31 -0500 Subject: [PATCH 1/3] Add hash to image tag in order to rebuild when the instructions change --- config.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/config.go b/config.go index 3a1dc445..2c679e6b 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" @@ -87,7 +89,9 @@ func (cb TGFConfigBuild) GetTag() string { if cb.Tag != "" { return cb.Tag } - return filepath.Base(filepath.Dir(cb.source)) + h := md5.New() + io.WriteString(h, cb.Instructions) + return fmt.Sprintf("%s-%x", filepath.Base(filepath.Dir(cb.source)), h.Sum(nil)) } // InitConfig returns a properly initialized TGF configuration struct From 07ee1a1f6a7a1f6cb9a802e69f1d5caf31e42441 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Wed, 28 Nov 2018 08:12:39 -0500 Subject: [PATCH 2/3] Fix test. Test value should contain the hash --- config_test.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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)) +} From 7b5ebb2027473abbcf5689791f013c8008edb5f8 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Fri, 30 Nov 2018 15:39:28 -0500 Subject: [PATCH 3/3] Calculate the md5 checksum for the files in the build folder --- config.go | 21 ++++++++++++++++++--- docker.go | 5 +++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index 2c679e6b..5e0e4d2d 100644 --- a/config.go +++ b/config.go @@ -73,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 == "" { @@ -89,9 +106,7 @@ func (cb TGFConfigBuild) GetTag() string { if cb.Tag != "" { return cb.Tag } - h := md5.New() - io.WriteString(h, cb.Instructions) - return fmt.Sprintf("%s-%x", filepath.Base(filepath.Dir(cb.source)), h.Sum(nil)) + return fmt.Sprintf("%s-%s", filepath.Base(filepath.Dir(cb.source)), cb.hash()) } // InitConfig returns a properly initialized TGF configuration struct diff --git a/docker.go b/docker.go index 1b6bd5a0..5a27dd67 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 }