Skip to content
Permalink
Browse files

feat(source_version): add SOURCE_VERSION env var

heroku exposes the git commit hash of the revision being built in the
SOURCE_VERSION environment variable. this commit implements this in the
deis builder for git pushes.

see https://devcenter.heroku.com/changelog-items/630 for more info on
heroku's implementation
  • Loading branch information...
aboyett committed Aug 30, 2016
1 parent 9c54cdd commit 05522a05a66420f1e0c0ad7a4e616f2a5dcd9470
Showing with 27 additions and 14 deletions.
  1. +2 −0 pkg/gitreceive/build.go
  2. +5 −0 pkg/gitreceive/k8s_util.go
  3. +20 −14 pkg/gitreceive/k8s_util_test.go
@@ -167,6 +167,7 @@ func build(
conf.PodNamespace,
appConf.Values,
slugBuilderInfo.TarKey(),
gitSha.Short(),
slugName,
conf.StorageType,
conf.DockerBuilderImage,
@@ -185,6 +186,7 @@ func build(
slugBuilderInfo.TarKey(),
slugBuilderInfo.PushKey(),
slugBuilderInfo.CacheKey(),
gitSha.Short(),
buildPackURL,
conf.StorageType,
conf.SlugBuilderImage,
@@ -19,6 +19,7 @@ const (
putPath = "PUT_PATH"
cachePath = "CACHE_PATH"
debugKey = "DEIS_DEBUG"
sourceVersion = "SOURCE_VERSION"
objectStore = "objectstorage-keyfile"
dockerSocketName = "docker-socket"
dockerSocketPath = "/var/run/docker.sock"
@@ -42,6 +43,7 @@ func dockerBuilderPod(
namespace string,
env map[string]interface{},
tarKey,
gitShortHash string,
imageName,
storageType,
image,
@@ -57,6 +59,7 @@ func dockerBuilderPod(
pod.Spec.Containers[0].Image = image

addEnvToPod(pod, tarPath, tarKey)
addEnvToPod(pod, sourceVersion, gitShortHash)
addEnvToPod(pod, "IMG_NAME", imageName)
addEnvToPod(pod, builderStorage, storageType)
// inject existing DEIS_REGISTRY_SERVICE_HOST and PORT info to dockerbuilder
@@ -93,6 +96,7 @@ func slugbuilderPod(
tarKey,
putKey,
cacheKey,
gitShortHash string,
buildpackURL,
storageType,
image string,
@@ -107,6 +111,7 @@ func slugbuilderPod(
addEnvToPod(pod, tarPath, tarKey)
addEnvToPod(pod, putPath, putKey)
addEnvToPod(pod, cachePath, cacheKey)
addEnvToPod(pod, sourceVersion, gitShortHash)
addEnvToPod(pod, builderStorage, storageType)

if buildpackURL != "" {
@@ -30,6 +30,7 @@ type slugBuildCase struct {
tarKey string
putKey string
cacheKey string
gitShortHash string
buildPack string
slugBuilderImage string
slugBuilderImagePullPolicy api.PullPolicy
@@ -42,6 +43,7 @@ type dockerBuildCase struct {
namespace string
env map[string]interface{}
tarKey string
gitShortHash string
imgName string
dockerBuilderImage string
dockerBuilderImagePullPolicy api.PullPolicy
@@ -57,13 +59,13 @@ func TestBuildPod(t *testing.T) {
var pod *api.Pod

slugBuilds := []slugBuildCase{
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "cache-url", "", "", api.PullAlways, ""},
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "buildpack", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "customimage", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "customimage", api.PullIfNotPresent, ""},
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "customimage", api.PullNever, ""},
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "deadbeef", "", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "", "", api.PullAlways, ""},
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "customimage", api.PullAlways, ""},
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "customimage", api.PullIfNotPresent, ""},
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "customimage", api.PullNever, ""},
}

for _, build := range slugBuilds {
@@ -75,6 +77,7 @@ func TestBuildPod(t *testing.T) {
build.tarKey,
build.putKey,
build.cacheKey,
build.gitShortHash,
build.buildPack,
build.storageType,
build.slugBuilderImage,
@@ -89,6 +92,7 @@ func TestBuildPod(t *testing.T) {
t.Errorf("expected %v but returned %v ", build.namespace, pod.ObjectMeta.Namespace)
}

checkForEnv(t, pod, "SOURCE_VERSION", build.gitShortHash)
checkForEnv(t, pod, "TAR_PATH", build.tarKey)
checkForEnv(t, pod, "PUT_PATH", build.putKey)
checkForEnv(t, pod, "CACHE_PATH", build.cacheKey)
@@ -110,13 +114,13 @@ func TestBuildPod(t *testing.T) {
}

dockerBuilds := []dockerBuildCase{
{true, "test", "default", emptyEnv, "tar", "", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "", "", api.PullAlways, ""},
{true, "test", "default", emptyEnv, "tar", "img", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "img", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "img", "customimage", api.PullAlways, ""},
{true, "test", "default", env, "tar", "img", "customimage", api.PullIfNotPresent, ""},
{true, "test", "default", env, "tar", "img", "customimage", api.PullNever, ""},
{true, "test", "default", emptyEnv, "tar", "deadbeef", "", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "deadbeef", "", "", api.PullAlways, ""},
{true, "test", "default", emptyEnv, "tar", "deadbeef", "img", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "deadbeef", "img", "", api.PullAlways, ""},
{true, "test", "default", env, "tar", "deadbeef", "img", "customimage", api.PullAlways, ""},
{true, "test", "default", env, "tar", "deadbeef", "img", "customimage", api.PullIfNotPresent, ""},
{true, "test", "default", env, "tar", "deadbeef", "img", "customimage", api.PullNever, ""},
}
regEnv := map[string]string{"REG_LOC": "on-cluster"}
for _, build := range dockerBuilds {
@@ -126,6 +130,7 @@ func TestBuildPod(t *testing.T) {
build.namespace,
build.env,
build.tarKey,
build.gitShortHash,
build.imgName,
build.storageType,
build.dockerBuilderImage,
@@ -142,6 +147,7 @@ func TestBuildPod(t *testing.T) {
t.Errorf("expected %v but returned %v ", build.namespace, pod.ObjectMeta.Namespace)
}

checkForEnv(t, pod, "SOURCE_VERSION", build.gitShortHash)
checkForEnv(t, pod, "TAR_PATH", build.tarKey)
checkForEnv(t, pod, "IMG_NAME", build.imgName)
checkForEnv(t, pod, "REG_LOC", "on-cluster")

0 comments on commit 05522a0

Please sign in to comment.
You can’t perform that action at this time.