Skip to content

Commit

Permalink
cnbBuild: allow expansion of buildEnvVars (#4802)
Browse files Browse the repository at this point in the history
* cnbBuild: allow expansion of `buildEnvVars`

* Update resources/metadata/cnbBuild.yaml

Co-authored-by: Ralf Pannemans <[email protected]>

---------

Co-authored-by: Ralf Pannemans <[email protected]>
  • Loading branch information
phil9909 and c0d1ngm0nk3y authored Feb 1, 2024
1 parent 689c79e commit 28fa260
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
17 changes: 17 additions & 0 deletions cmd/cnbBuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,10 @@ func runCnbBuild(config *cnbBuildOptions, telemetry *buildpacks.Telemetry, image
}
commonPipelineEnvironment.container.imageNames = append(commonPipelineEnvironment.container.imageNames, imageNameAlias)

if config.ExpandBuildEnvVars {
config.BuildEnvVars = expandEnvVars(config.BuildEnvVars)
}

if config.BuildEnvVars != nil && len(config.BuildEnvVars) > 0 {
log.Entry().Infof("Setting custom environment variables: '%v'", config.BuildEnvVars)
imageSummary.AddEnv(config.BuildEnvVars)
Expand Down Expand Up @@ -619,6 +623,19 @@ func runCnbBuild(config *cnbBuildOptions, telemetry *buildpacks.Telemetry, image
return nil
}

func expandEnvVars(envVars map[string]any) map[string]any {
expandedEnvVars := map[string]any{}
for key, value := range envVars {
valueString, valueIsString := value.(string)
if valueIsString {
expandedEnvVars[key] = os.ExpandEnv(valueString)
} else {
expandedEnvVars[key] = value
}
}
return expandedEnvVars
}

func createInitialTelemetrySegment(config *cnbBuildOptions, utils cnbutils.BuildUtils) *buildpacks.Segment {
telemetrySegment := buildpacks.NewSegment()
projectPath, _, _ := config.resolvePath(utils) // ignore error here, telemetry problems should not fail the build
Expand Down
11 changes: 11 additions & 0 deletions cmd/cnbBuild_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions cmd/cnbBuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,17 @@ func TestRunCnbBuild(t *testing.T) {
assert.Equal(t, "my-image:0.0.1", commonPipelineEnvironment.container.imageNameTag)
})

t.Run("success case (custom buildpacks and custom env variables, renaming docker conf file, additional tag)", func(t *testing.T) {
t.Parallel()
t.Run("success case (custom buildpacks and custom env variables with expand, renaming docker conf file, additional tag)", func(t *testing.T) {
t.Setenv("BAR", "BAZZ")
config := cnbBuildOptions{
ContainerImageName: "my-image",
ContainerImageTag: "0.0.1",
ContainerRegistryURL: imageRegistry,
DockerConfigJSON: "/path/to/test.json",
Buildpacks: []string{"test"},
ExpandBuildEnvVars: true,
BuildEnvVars: map[string]interface{}{
"FOO": "BAR",
"FOO": "${BAR}",
},
AdditionalTags: []string{"latest"},
}
Expand All @@ -269,6 +270,8 @@ func TestRunCnbBuild(t *testing.T) {

copiedFileExists, _ := utils.FileExists("/tmp/config.json")
assert.True(t, copiedFileExists)

assetBuildEnv(t, utils, "FOO", "BAZZ")
})

t.Run("success case (custom buildpacks, pre and post buildpacks and custom env variables, renaming docker conf file, additional tag)", func(t *testing.T) {
Expand All @@ -281,8 +284,9 @@ func TestRunCnbBuild(t *testing.T) {
PreBuildpacks: []string{"pre-test"},
PostBuildpacks: []string{"post-test"},
Buildpacks: []string{"test"},
ExpandBuildEnvVars: false,
BuildEnvVars: map[string]interface{}{
"FOO": "BAR",
"FOO": "${BAR}",
},
AdditionalTags: []string{"latest"},
}
Expand All @@ -304,6 +308,8 @@ func TestRunCnbBuild(t *testing.T) {

copiedFileExists, _ := utils.FileExists("/tmp/config.json")
assert.True(t, copiedFileExists)

assetBuildEnv(t, utils, "FOO", "${BAR}")
})

t.Run("success case (custom pre and post buildpacks and custom env variables, renaming docker conf file, additional tag)", func(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions resources/metadata/cnbBuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ spec:
- PARAMETERS
- STAGES
- STEPS
- name: expandBuildEnvVars
type: "bool"
default: false
description: |
Expand environment variables used in `buildEnvVars`.
Example:
```yaml
expandBuildEnvVars: true
buildEnvVars:
foo: ${BAR}
```
scope:
- PARAMETERS
- STAGES
- STEPS
- name: path
type: string
description: |
Expand Down

0 comments on commit 28fa260

Please sign in to comment.