Skip to content

Commit

Permalink
Support variable substitution with --var and --var-file in `odo b…
Browse files Browse the repository at this point in the history
…uild-images` (redhat-developer#6776)

* Add integration tests

* Add support for --var and --var-file to the 'build-images' command

* Update 'build-images' command reference
  • Loading branch information
rm3l authored Apr 27, 2023
1 parent ef9206a commit 00918c3
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
11 changes: 9 additions & 2 deletions docs/website/docs/command-reference/build-images.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ Successfully tagged quay.io/user/myimage:latest
```
</details>


### Faking the image build
You can also fake the image build by exporting `PODMAN_CMD=echo` or `DOCKER_CMD=echo` to your environment. Read [environment variables controlling `odo` behaviour](../overview/configure.md#environment-variables-controlling-odo-behavior) for more information.
You can also fake the image build by exporting `PODMAN_CMD=echo` or `DOCKER_CMD=echo` to your environment. Read [environment variables controlling `odo` behaviour](../overview/configure.md#environment-variables-controlling-odo-behavior) for more information.

## Substituting variables

The Devfile can define variables to make the Devfile parameterizable. The Devfile can define values for these variables, and you
can override the values for variables from the command line when running `odo build-images`, using the `--var` and `--var-file` options.

See [Substituting variables in `odo` dev](dev.md#substituting-variables) for more information.

2 changes: 2 additions & 0 deletions pkg/odo/cli/build_images/build_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/redhat-developer/odo/pkg/devfile/image"
"github.com/redhat-developer/odo/pkg/odo/cmdline"
"github.com/redhat-developer/odo/pkg/odo/commonflags"
odocontext "github.com/redhat-developer/odo/pkg/odo/context"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions"
"github.com/redhat-developer/odo/pkg/odo/genericclioptions/clientset"
Expand Down Expand Up @@ -82,6 +83,7 @@ func NewCmdBuildImages(name, fullName string) *cobra.Command {

util.SetCommandGroup(buildImagesCmd, util.MainGroup)
buildImagesCmd.SetUsageTemplate(odoutil.CmdUsageTemplate)
commonflags.UseVariablesFlags(buildImagesCmd)
buildImagesCmd.Flags().BoolVar(&o.pushFlag, "push", false, "If true, build and push the images")
clientset.Add(buildImagesCmd, clientset.FILESYSTEM)

Expand Down
21 changes: 20 additions & 1 deletion tests/examples/source/devfiles/nodejs/devfile-variables.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
schemaVersion: 2.1.0
schemaVersion: 2.2.0
metadata:
name: nodejs
variables:
VARIABLE_TEST: FOO
VALUE_TEST: bar
VARIABLE_CONTAINER_IMAGE_1: "my-image-1:1.2.3-rc4"
VARIABLE_CONTAINER_IMAGE_2: "my-image-2:2.3.4-alpha5"

components:
- name: my-image-1
image:
autoBuild: false
imageName: "{{ VARIABLE_CONTAINER_IMAGE_1 }}"
dockerfile:
buildContext: ${PROJECT_SOURCE}
uri: ./Dockerfile

- name: my-image-2
image:
autoBuild: false
imageName: "{{ VARIABLE_CONTAINER_IMAGE_2 }}"
dockerfile:
buildContext: ${PROJECT_SOURCE}
uri: ./Dockerfile

- name: runtime
container:
image: registry.access.redhat.com/ubi8/nodejs-12:1-36
Expand Down
62 changes: 62 additions & 0 deletions tests/integration/cmd_devfile_build_images_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"

"github.com/redhat-developer/odo/tests/helper"
)
Expand Down Expand Up @@ -324,4 +325,65 @@ CMD ["npm", "start"]
})
})
})

When("using a Devfile with variable image names", func() {
BeforeEach(func() {
helper.CopyExample(filepath.Join("source", "devfiles", "nodejs", "project"), commonVar.Context)
helper.CopyExampleDevFile(
filepath.Join("source", "devfiles", "nodejs", "devfile-variables.yaml"),
filepath.Join(commonVar.Context, "devfile.yaml"),
helper.DevfileMetadataNameSetter(cmpName))
})

checkOutput := func(stdout string, images []string, push bool) {
var matchers []types.GomegaMatcher
for _, img := range images {
msg := "Building"
if push {
msg += " & Pushing"
}
matchers = append(matchers, ContainSubstring("%s Image: %s", msg, img))
matchers = append(matchers, ContainSubstring("build -t %s -f %s %s", img, filepath.Join(commonVar.Context, "Dockerfile"), commonVar.Context))
if push {
matchers = append(matchers, ContainSubstring("push %s", img))
}
}
Expect(stdout).Should(SatisfyAll(matchers...))
}

for _, push := range []bool{false, true} {
push := push
initialArgs := []string{"build-images"}
if push {
initialArgs = append(initialArgs, "--push")
}
It(fmt.Sprintf("should build images with default variable values (push=%v)", push), func() {
args := initialArgs
stdout := helper.Cmd("odo", args...).AddEnv("PODMAN_CMD=echo").ShouldPass().Out()
checkOutput(stdout, []string{"my-image-1:1.2.3-rc4", "my-image-2:2.3.4-alpha5"}, push)
})

It(fmt.Sprintf("should build images with --var (push=%v)", push), func() {
args := initialArgs
args = append(args, "--var", "VARIABLE_CONTAINER_IMAGE_2=my-image-2-overridden:next")
stdout := helper.Cmd("odo", args...).
AddEnv("PODMAN_CMD=echo").ShouldPass().Out()
checkOutput(stdout, []string{"my-image-1:1.2.3-rc4", "my-image-2-overridden:next"}, push)
})

It(fmt.Sprintf("should build images with --var-file (push=%v)", push), func() {
var varFilename = filepath.Join(commonVar.Context, "vars.txt")
err := helper.CreateFileWithContent(varFilename, `VARIABLE_CONTAINER_IMAGE_1=my-image-1-overridden-from-file:next
VARIABLE_CONTAINER_IMAGE_2=my-image-2-overridden-from-file:next
`)
Expect(err).ShouldNot(HaveOccurred())

args := initialArgs
args = append(args, "--var-file", varFilename)
stdout := helper.Cmd("odo", args...).
AddEnv("PODMAN_CMD=echo").ShouldPass().Out()
checkOutput(stdout, []string{"my-image-1-overridden-from-file:next", "my-image-2-overridden-from-file:next"}, push)
})
}
})
})

0 comments on commit 00918c3

Please sign in to comment.