Skip to content

Commit

Permalink
Propagate kaniko environment to GCB
Browse files Browse the repository at this point in the history
  • Loading branch information
briandealwis committed Jul 9, 2021
1 parent 37cdc30 commit 4342a84
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/skaffold/build/gcb/kaniko.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
"fmt"

"google.golang.org/api/cloudbuild/v1"
v1 "k8s.io/api/core/v1"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/kaniko"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/misc"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
)
Expand All @@ -40,10 +42,24 @@ func (b *Builder) kanikoBuildSpec(a *latestV1.Artifact, tag string) (cloudbuild.
return cloudbuild.Build{}, err
}

env, err := misc.EvaluateEnv(envFromVars(k.Env))
if err != nil {
return cloudbuild.Build{}, fmt.Errorf("unable to evaluate env variables: %w", err)
}

return cloudbuild.Build{
Steps: []*cloudbuild.BuildStep{{
Name: b.KanikoImage,
Args: kanikoArgs,
Env: env,
}},
}, nil
}

func envFromVars(env []v1.EnvVar) []string {
s := make([]string, 0, len(env))
for _, envVar := range env {
s = append(s, envVar.Name+"="+envVar.Value)
}
return s
}
11 changes: 11 additions & 0 deletions pkg/skaffold/build/gcb/kaniko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"google.golang.org/api/cloudbuild/v1"
kv1 "k8s.io/api/core/v1"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/kaniko"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
Expand All @@ -35,6 +36,7 @@ func TestKanikoBuildSpec(t *testing.T) {
description string
artifact *latestV1.KanikoArtifact
expectedArgs []string
expectedEnv []string
}{
{
description: "simple build",
Expand All @@ -57,6 +59,14 @@ func TestKanikoBuildSpec(t *testing.T) {
kaniko.BuildArgsFlag, "arg2",
},
},
{
description: "with Env",
artifact: &latestV1.KanikoArtifact{
DockerfilePath: "Dockerfile",
Env: []kv1.EnvVar{{Name: "KEY1", Value: "VALUE1"}, {Name: "KEY2", Value: "VALUE2"}},
},
expectedEnv: []string{"KEY1=VALUE1", "KEY2=VALUE2"},
},
{
description: "with Cache",
artifact: &latestV1.KanikoArtifact{
Expand Down Expand Up @@ -409,6 +419,7 @@ func TestKanikoBuildSpec(t *testing.T) {
Steps: []*cloudbuild.BuildStep{{
Name: "gcr.io/kaniko-project/executor",
Args: append(append(defaultExpectedArgs, imageArgs...), test.expectedArgs...),
Env: test.expectedEnv,
}},
Options: &cloudbuild.BuildOptions{
DiskSizeGb: 100,
Expand Down

0 comments on commit 4342a84

Please sign in to comment.