Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate kaniko environment to GCB #6181

Merged
merged 1 commit into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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