Search code examples
dockerenvironment-variablesjenkins-pipelineopenshiftbuildconfig

OpenShift BuildConfig Docker strategy spec.output.to.name using environment variable for tagging


I have defined the following OpenShift BuildConfig:

<snip>
    output:
      to:
        kind: DockerImage
        name: myregistry.com/myapp:${TAG}
    strategy:
      type: Docker
      dockerStrategy:
        from:
          kind: ImageStreamTag
          name: nodejs-12:latest
          namespace: myproject
        env:
          - name: TAG
            value: latest

I would like the TAG to be a version number which consists of version from the application package.json and the Jenkins #build number. So I plan to pass this env value in the OpenShift start-build command line using the -e option. However, when applying the BuildConfig definition, it complains that name is not a valid Docker pull specification: invalid reference format. How do I use variable to specify image tag string value? Any advice and insight is appreciated.


Solution

  • Sadly, this is not possible using a single BuildConfig. The output part is evaluated during object creation, your build container environment can't set that tag later on.

    You may want to use a Template instead, creating BuildConfigs for earch tag you need to build.

    Or, assuming OpenShift 4+, you could look into Tekton. The Pipeline object allows for generic build definitions - though still requires creating PipelineResources for each image tag, as well as git source ref.