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

feat(ko): Enable templating of labels and env #6944

Merged
merged 1 commit into from
Dec 13, 2021

Conversation

halvards
Copy link
Contributor

@halvards halvards commented Dec 7, 2021

This change enables ko builder users to substitute environment variable values in the labels and env config fields.

These fields are used for image labels and build-time environment variables, respectively.

Envvar templating of image labels can be used to add information such as the Git commit SHA to the image, see #6916.

Also, environment variable expansion of flags and ldflags in the ko builder configuration now supports Skaffold's templating syntax, for consistency.

For backwards compatibility, ko's templating syntax still works with flags and ldflags:

Skaffold: {{.FOO}}

ko: {{.Env.FOO}}

Tracking: #6041
Fixes: #6916

@codecov
Copy link

codecov bot commented Dec 8, 2021

Codecov Report

Merging #6944 (fd5150f) into main (290280e) will decrease coverage by 1.65%.
The diff coverage is 56.70%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #6944      +/-   ##
==========================================
- Coverage   70.48%   68.83%   -1.66%     
==========================================
  Files         515      551      +36     
  Lines       23150    25324    +2174     
==========================================
+ Hits        16317    17431    +1114     
- Misses       5776     6716     +940     
- Partials     1057     1177     +120     
Impacted Files Coverage Δ
cmd/skaffold/app/cmd/deploy.go 52.00% <ø> (-1.85%) ⬇️
cmd/skaffold/app/cmd/dev.go 84.61% <0.00%> (ø)
cmd/skaffold/app/cmd/render.go 36.66% <0.00%> (-4.72%) ⬇️
cmd/skaffold/skaffold.go 0.00% <0.00%> (ø)
cmd/skaffold/app/cmd/inspect_tests.go 62.50% <14.28%> (-1.14%) ⬇️
cmd/skaffold/app/cmd/lsp.go 28.12% <28.12%> (ø)
cmd/skaffold/app/cmd/fix.go 68.85% <40.00%> (-7.62%) ⬇️
cmd/skaffold/app/cmd/lint.go 42.85% <42.85%> (ø)
cmd/skaffold/app/cmd/find_configs.go 48.88% <50.00%> (+0.24%) ⬆️
cmd/skaffold/app/skaffold.go 76.19% <70.00%> (-8.43%) ⬇️
... and 170 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4b0579c...fd5150f. Read the comment docs.

@halvards halvards added the kokoro:run runs the kokoro jobs on a PR label Dec 8, 2021
@kokoro-team kokoro-team removed the kokoro:run runs the kokoro jobs on a PR label Dec 8, 2021
bo, err := buildOptions(&test.artifact, test.runMode)
os.Setenv(testKoBuildOptionsEnvVar, test.envVarValue)
gotBo, err := buildOptions(&test.artifact, test.runMode)
os.Unsetenv(testKoBuildOptionsEnvVar)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets call this in defer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, done!

ArtifactType: latestV1.ArtifactType{
KoArtifact: &latestV1.KoArtifact{
Dir: "my-go-mod-is-here",
envVarValue: "baz",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shd we add a test case for {{.Env.baz}} here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

This change enables `ko` builder users to substitute environment
variable values in the `labels` and `env` config fields.

These fields are used for image labels and build-time environment
variables, respectively.

Envvar templating of image labels can be used to add information such as
the Git commit SHA to the image, see GoogleContainerTools#6916.

Also, environment variable expansion of `flags` and `ldflags` in the
`ko` builder configuration now supports Skaffold's templating syntax,
for consistency.

For backwards compatibility, `ko`'s templating syntax still works with
`flags` and `ldflags`:

Skaffold: `{{.FOO}}`

`ko`: `{{.Env.FOO}}`

Tracking: GoogleContainerTools#6041
Fixes: GoogleContainerTools#6916
@tejal29 tejal29 added the kokoro:force-run forces a kokoro re-run on a PR label Dec 13, 2021
@tejal29 tejal29 enabled auto-merge (squash) December 13, 2021 13:14
@kokoro-team kokoro-team removed the kokoro:force-run forces a kokoro re-run on a PR label Dec 13, 2021
@tejal29 tejal29 merged commit 17c4d6c into GoogleContainerTools:main Dec 13, 2021
@halvards halvards deleted the ko-envvar-image-labels branch December 14, 2021 00:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FR: ko builder to support envvars in labels config field
3 participants