Skip to content

Commit

Permalink
add ability to set env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
dskiff committed May 2, 2024
1 parent 4fc0eb2 commit 8325db2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
11 changes: 10 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ build:
annotations:
label3: value3
label4: value4
env:
VAR1: value1
VAR2: value2
tmp: /tmp-dir
verbose: true
Expand Down Expand Up @@ -56,6 +59,8 @@ build:
assert.Equal(t, "value2", cli.Build.DefaultAnnotations["label2"])
assert.Equal(t, "value3", cli.Build.Annotations["label3"])
assert.Equal(t, "value4", cli.Build.Annotations["label4"])
assert.Equal(t, "value1", cli.Build.Env["VAR1"])
assert.Equal(t, "value2", cli.Build.Env["VAR2"])

assert.Equal(t, "/tmp-dir", cli.Build.Tmp)
assert.Equal(t, true, cli.Build.Verbose)
Expand All @@ -76,7 +81,7 @@ func TestBuildArgs(t *testing.T) {
_, err := parser.Parse([]string{"build", "/source",
"-b", "base-image@sha256:1234",
"-p", "custom-os/arch-variant",
"-e", "/entrypoint",
"--entrypoint", "/entrypoint",
"-d", "/destination",
"--destination-chown=false",
"-t", "repo/target",
Expand All @@ -85,6 +90,8 @@ func TestBuildArgs(t *testing.T) {
"-A", "label2=value2",
"-a", "label3=value3",
"-a", "label4=value4",
"-e", "VAR1=value1",
"-e", "VAR2=value2",
"-T", "REMOTE",
"-v",
"--tmp", "/tmp-dir",
Expand All @@ -105,6 +112,8 @@ func TestBuildArgs(t *testing.T) {
assert.Equal(t, "value2", cli.Build.DefaultAnnotations["label2"])
assert.Equal(t, "value3", cli.Build.Annotations["label3"])
assert.Equal(t, "value4", cli.Build.Annotations["label4"])
assert.Equal(t, "value1", cli.Build.Env["VAR1"])
assert.Equal(t, "value2", cli.Build.Env["VAR2"])

assert.Equal(t, "/tmp-dir", cli.Build.Tmp)
assert.Equal(t, true, cli.Build.Verbose)
Expand Down
11 changes: 9 additions & 2 deletions pkg/build/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type BuildSpec struct {

Author string
Annotations map[string]string
Env map[string]string
}

type BuildContext struct {
Expand Down Expand Up @@ -97,11 +98,11 @@ func Build(ctx BuildContext, spec BuildSpec) error {
}

func mutateConfig(img v1.Image, spec BuildSpec, metadata BaseImageMetadata) (v1.Image, error) {
imgCfg, err := img.ConfigFile()
initImgCfg, err := img.ConfigFile()
if err != nil {
return nil, err
}
imgCfg = imgCfg.DeepCopy()
imgCfg := initImgCfg.DeepCopy()

imgCfg.Config.WorkingDir = spec.InjectLayer.DestinationPath
imgCfg.Config.Entrypoint = []string{spec.InjectLayer.Entrypoint}
Expand All @@ -112,6 +113,12 @@ func mutateConfig(img v1.Image, spec BuildSpec, metadata BaseImageMetadata) (v1.
imgCfg.Container = ""
imgCfg.DockerVersion = ""

imgCfg.Config.Env = []string{}
imgCfg.Config.Env = append(imgCfg.Config.Env, initImgCfg.Config.Env...)
for k, v := range spec.Env {
imgCfg.Config.Env = append(imgCfg.Config.Env, k+"="+v)
}

imgCfg.Config.Labels = map[string]string{}
imgCfg.Config.Labels["org.opencontainers.image.base.name"] = metadata.name

Expand Down
4 changes: 3 additions & 1 deletion pkg/cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type BuildCmd struct {
SourcePath string `arg:"" help:"Path to artifacts to embed" type:"path" env:"TKO_SOURCE_PATH"`
DestinationPath string `short:"d" help:"Path to embed artifacts in" env:"TKO_DEST_PATH" default:"/tko-app"`
DestinationChown bool `help:"Whether to chown the destination path to root:root" default:"true"`
Entrypoint string `short:"e" help:"Entrypoint for the embedded artifacts" env:"TKO_ENTRYPOINT" default:"/tko-app/app"`
Entrypoint string `help:"Entrypoint for the embedded artifacts" env:"TKO_ENTRYPOINT" default:"/tko-app/app"`

TargetRepo string `short:"t" help:"Target repository" env:"TKO_TARGET_REPO" required:"true"`
TargetType string `short:"T" help:"Target type" env:"TKO_TARGET_TYPE" default:"REMOTE" enum:"REMOTE,LOCAL_DAEMON,LOCAL_FILE"`
Expand All @@ -32,6 +32,7 @@ type BuildCmd struct {
DefaultAnnotations map[string]string `short:"A" help:"Default annotations to apply to the image" env:"TKO_DEFAULT_ANNOTATIONS" default:"" mapsep:"," sep:"="`
Annotations map[string]string `short:"a" help:"Additional annotations to apply to the image. Can override default-annotations." env:"TKO_ANNOTATIONS" default:"" mapsep:"," sep:"="`
AutoVersionAnnotation string `help:"Automatically version annotations" env:"TKO_AUTO_VERSION_ANNOTATION" default:"none" enum:"git,none"`
Env map[string]string `short:"e" help:"Environment variables to set in the build" env:"TKO_ENV_VARS" default:"" mapsep:"," sep:"="`

RegistryUser string `help:"Registry user. Used for target registry url. You can use standard docker config for more complex auth." env:"TKO_REGISTRY_USER"`
RegistryPass string `help:"Registry password. Used for target registry url. You can use standard docker config for more complex auth." env:"TKO_REGISTRY_PASS"`
Expand Down Expand Up @@ -131,6 +132,7 @@ func (b *BuildCmd) Run(cliCtx *CliCtx) error {

Author: b.Author,
Annotations: annotations,
Env: b.Env,
}

out, err := yaml.Marshal(cfg)
Expand Down

0 comments on commit 8325db2

Please sign in to comment.