Skip to content

Commit

Permalink
Merge pull request #37 from coveo/rebuild-image-if-changed
Browse files Browse the repository at this point in the history
Add hash to image tag in order to rebuild when the instructions change
  • Loading branch information
julienduchesne authored Dec 3, 2018
2 parents c0043b9 + 7b5ebb2 commit 0e364fc
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
21 changes: 20 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package main

import (
"crypto/md5"
"errors"
"fmt"
"github.com/hashicorp/go-getter"
"io"
"io/ioutil"
"os"
"os/exec"
Expand Down Expand Up @@ -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 == "" {
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion config_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package main

import (
"crypto/md5"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
}
5 changes: 3 additions & 2 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 0e364fc

Please sign in to comment.