Skip to content

Commit 05522a0

Browse files
committed
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
1 parent 9c54cdd commit 05522a0

3 files changed

Lines changed: 27 additions & 14 deletions

File tree

pkg/gitreceive/build.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func build(
167167
conf.PodNamespace,
168168
appConf.Values,
169169
slugBuilderInfo.TarKey(),
170+
gitSha.Short(),
170171
slugName,
171172
conf.StorageType,
172173
conf.DockerBuilderImage,
@@ -185,6 +186,7 @@ func build(
185186
slugBuilderInfo.TarKey(),
186187
slugBuilderInfo.PushKey(),
187188
slugBuilderInfo.CacheKey(),
189+
gitSha.Short(),
188190
buildPackURL,
189191
conf.StorageType,
190192
conf.SlugBuilderImage,

pkg/gitreceive/k8s_util.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
putPath = "PUT_PATH"
2020
cachePath = "CACHE_PATH"
2121
debugKey = "DEIS_DEBUG"
22+
sourceVersion = "SOURCE_VERSION"
2223
objectStore = "objectstorage-keyfile"
2324
dockerSocketName = "docker-socket"
2425
dockerSocketPath = "/var/run/docker.sock"
@@ -42,6 +43,7 @@ func dockerBuilderPod(
4243
namespace string,
4344
env map[string]interface{},
4445
tarKey,
46+
gitShortHash string,
4547
imageName,
4648
storageType,
4749
image,
@@ -57,6 +59,7 @@ func dockerBuilderPod(
5759
pod.Spec.Containers[0].Image = image
5860

5961
addEnvToPod(pod, tarPath, tarKey)
62+
addEnvToPod(pod, sourceVersion, gitShortHash)
6063
addEnvToPod(pod, "IMG_NAME", imageName)
6164
addEnvToPod(pod, builderStorage, storageType)
6265
// inject existing DEIS_REGISTRY_SERVICE_HOST and PORT info to dockerbuilder
@@ -93,6 +96,7 @@ func slugbuilderPod(
9396
tarKey,
9497
putKey,
9598
cacheKey,
99+
gitShortHash string,
96100
buildpackURL,
97101
storageType,
98102
image string,
@@ -107,6 +111,7 @@ func slugbuilderPod(
107111
addEnvToPod(pod, tarPath, tarKey)
108112
addEnvToPod(pod, putPath, putKey)
109113
addEnvToPod(pod, cachePath, cacheKey)
114+
addEnvToPod(pod, sourceVersion, gitShortHash)
110115
addEnvToPod(pod, builderStorage, storageType)
111116

112117
if buildpackURL != "" {

pkg/gitreceive/k8s_util_test.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type slugBuildCase struct {
3030
tarKey string
3131
putKey string
3232
cacheKey string
33+
gitShortHash string
3334
buildPack string
3435
slugBuilderImage string
3536
slugBuilderImagePullPolicy api.PullPolicy
@@ -42,6 +43,7 @@ type dockerBuildCase struct {
4243
namespace string
4344
env map[string]interface{}
4445
tarKey string
46+
gitShortHash string
4547
imgName string
4648
dockerBuilderImage string
4749
dockerBuilderImagePullPolicy api.PullPolicy
@@ -57,13 +59,13 @@ func TestBuildPod(t *testing.T) {
5759
var pod *api.Pod
5860

5961
slugBuilds := []slugBuildCase{
60-
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "", "", api.PullAlways, ""},
61-
{true, "test", "default", env, "tar", "put-url", "cache-url", "", "", api.PullAlways, ""},
62-
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "buildpack", "", api.PullAlways, ""},
63-
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "", api.PullAlways, ""},
64-
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "customimage", api.PullAlways, ""},
65-
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "customimage", api.PullIfNotPresent, ""},
66-
{true, "test", "default", env, "tar", "put-url", "cache-url", "buildpack", "customimage", api.PullNever, ""},
62+
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "deadbeef", "", "", api.PullAlways, ""},
63+
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "", "", api.PullAlways, ""},
64+
{true, "test", "default", emptyEnv, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "", api.PullAlways, ""},
65+
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "", api.PullAlways, ""},
66+
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "customimage", api.PullAlways, ""},
67+
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "customimage", api.PullIfNotPresent, ""},
68+
{true, "test", "default", env, "tar", "put-url", "cache-url", "deadbeef", "buildpack", "customimage", api.PullNever, ""},
6769
}
6870

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

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

112116
dockerBuilds := []dockerBuildCase{
113-
{true, "test", "default", emptyEnv, "tar", "", "", api.PullAlways, ""},
114-
{true, "test", "default", env, "tar", "", "", api.PullAlways, ""},
115-
{true, "test", "default", emptyEnv, "tar", "img", "", api.PullAlways, ""},
116-
{true, "test", "default", env, "tar", "img", "", api.PullAlways, ""},
117-
{true, "test", "default", env, "tar", "img", "customimage", api.PullAlways, ""},
118-
{true, "test", "default", env, "tar", "img", "customimage", api.PullIfNotPresent, ""},
119-
{true, "test", "default", env, "tar", "img", "customimage", api.PullNever, ""},
117+
{true, "test", "default", emptyEnv, "tar", "deadbeef", "", "", api.PullAlways, ""},
118+
{true, "test", "default", env, "tar", "deadbeef", "", "", api.PullAlways, ""},
119+
{true, "test", "default", emptyEnv, "tar", "deadbeef", "img", "", api.PullAlways, ""},
120+
{true, "test", "default", env, "tar", "deadbeef", "img", "", api.PullAlways, ""},
121+
{true, "test", "default", env, "tar", "deadbeef", "img", "customimage", api.PullAlways, ""},
122+
{true, "test", "default", env, "tar", "deadbeef", "img", "customimage", api.PullIfNotPresent, ""},
123+
{true, "test", "default", env, "tar", "deadbeef", "img", "customimage", api.PullNever, ""},
120124
}
121125
regEnv := map[string]string{"REG_LOC": "on-cluster"}
122126
for _, build := range dockerBuilds {
@@ -126,6 +130,7 @@ func TestBuildPod(t *testing.T) {
126130
build.namespace,
127131
build.env,
128132
build.tarKey,
133+
build.gitShortHash,
129134
build.imgName,
130135
build.storageType,
131136
build.dockerBuilderImage,
@@ -142,6 +147,7 @@ func TestBuildPod(t *testing.T) {
142147
t.Errorf("expected %v but returned %v ", build.namespace, pod.ObjectMeta.Namespace)
143148
}
144149

150+
checkForEnv(t, pod, "SOURCE_VERSION", build.gitShortHash)
145151
checkForEnv(t, pod, "TAR_PATH", build.tarKey)
146152
checkForEnv(t, pod, "IMG_NAME", build.imgName)
147153
checkForEnv(t, pod, "REG_LOC", "on-cluster")

0 commit comments

Comments
 (0)