Build Strategy Options

You are viewing documentation for a release that is no longer supported. The latest supported version of version 3 is [3.11]. For the most recent version 4, see [4]

You are viewing documentation for a release that is no longer supported. The latest supported version of version 3 is [3.11]. For the most recent version 4, see [4]

Source-to-Image Strategy Options

The following options are specific to the S2I build strategy.

Force Pull

By default, if the builder image specified in the build configuration is available locally on the node, that image will be used. However, to override the local image and refresh it from the registry to which the image stream points, create a BuildConfig with the forcePull flag set to true:

  1. strategy:
  2. sourceStrategy:
  3. from:
  4. kind: "ImageStreamTag"
  5. name: "builder-image:latest" (1)
  6. forcePull: true (2)
1The builder image being used, where the local version on the node may not be up to date with the version in the registry to which the image stream points.
2This flag causes the local builder image to be ignored and a fresh version to be pulled from the registry to which the image stream points. Setting forcePull to false results in the default behavior of honoring the image stored locally.

Incremental Builds

S2I can perform incremental builds, which means it reuses artifacts from previously-built images. To create an incremental build, create a BuildConfig with the following modification to the strategy definition:

  1. strategy:
  2. sourceStrategy:
  3. from:
  4. kind: "ImageStreamTag"
  5. name: "incremental-image:latest" (1)
  6. incremental: true (2)
1Specify an image that supports incremental builds. Consult the documentation of the builder image to determine if it supports this behavior.
2This flag controls whether an incremental build is attempted. If the builder image does not support incremental builds, the build will still succeed, but you will get a log message stating the incremental build was not successful because of a missing save-artifacts script.

See the S2I Requirements topic for information on how to create a builder image supporting incremental builds.

Overriding Builder Image Scripts

You can override the assemble, run, and save-artifacts S2I scripts provided by the builder image in one of two ways. Either:

  1. Provide an assemble, run, and/or save-artifacts script in the .s2i/bin directory of your application source repository, or

  2. Provide a URL of a directory containing the scripts as part of the strategy definition. For example:

  1. strategy:
  2. sourceStrategy:
  3. from:
  4. kind: "ImageStreamTag"
  5. name: "builder-image:latest"
  6. scripts: "http://somehost.com/scripts_directory" (1)
1This path will have run, assemble, and save-artifacts appended to it. If any or all scripts are found they will be used in place of the same named script(s) provided in the image.

Files located at the scripts URL take precedence over files located in .s2i/bin of the source repository. See the S2I Requirements topic and the S2I documentation for information on how S2I scripts are used.

Environment Variables

There are two ways to make environment variables available to the source build process and resulting image. Environment files and BuildConfig environment values. Variables provided will be present during the build process and in the output image.

Environment Files

Source build enables you to set environment values (one per line) inside your application, by specifying them in a .s2i/environment file in the source repository. The environment variables specified in this file are present during the build process and in the output image. The complete list of supported environment variables is available in the documentation for each image.

If you provide a .s2i/environment file in your source repository, S2I reads this file during the build. This allows customization of the build behavior as the assemble script may use these variables.

For example, if you want to disable assets compilation for your Rails application, you can add DISABLE_ASSET_COMPILATION=true in the .s2i/environment file to cause assets compilation to be skipped during the build.

In addition to builds, the specified environment variables are also available in the running application itself. For example, you can add RAILS_ENV=development to the .s2i/environment file to cause the Rails application to start in development mode instead of production.

BuildConfig Environment

You can add environment variables to the sourceStrategy definition of the BuildConfig. The environment variables defined there are visible during the assemble script execution and will be defined in the output image, making them also available to the run script and application code.

For example disabling assets compilation for your Rails application:

  1. sourceStrategy:
  2. ...
  3. env:
  4. - name: "DISABLE_ASSET_COMPILATION"
  5. value: "true"

The Build Environment section provides more advanced instructions.

You can also manage environment variables defined in the BuildConfig with the oc set env command.

Adding Secrets via Web Console

To add a secret to your build configuration so that it can access a private repository:

  1. Create a new OKD project.

  2. Create a secret that contains credentials for accessing a private source code repository.

  3. Create a Source-to-Image (S2I) build configuration.

  4. On the build configuration editor page or in the create app from builder image page of the web console, set the Source Secret.

  5. Click the Save button.

Enabling Pulling and Pushing

Enable pulling to a private registry by setting the Pull Secret in the build configuration and enable pushing by setting the Push Secret.

Ignoring Source Files

Source to image supports a .s2iignore file, which contains a list of file patterns that should be ignored. Files in the build working directory, as provided by the various input sources, that match a pattern found in the .s2iignore file will not be made available to the assemble script.

For more details on the format of the .s2iignore file, see the source-to-image documentation.

Docker Strategy Options

The following options are specific to the Docker build strategy.

FROM Image

The FROM instruction of the Dockerfile will be replaced by the from of the BuildConfig:

  1. strategy:
  2. dockerStrategy:
  3. from:
  4. kind: "ImageStreamTag"
  5. name: "debian:latest"

Dockerfile Path

By default, Docker builds use a Dockerfile (named Dockerfile) located at the root of the context specified in the BuildConfig.spec.source.contextDir field.

The dockerfilePath field allows the build to use a different path to locate your Dockerfile, relative to the BuildConfig.spec.source.contextDir field. It can be simply a different file name other than the default Dockerfile (for example, MyDockerfile), or a path to a Dockerfile in a subdirectory (for example, dockerfiles/app1/Dockerfile):

  1. strategy:
  2. dockerStrategy:
  3. dockerfilePath: dockerfiles/app1/Dockerfile

No Cache

Docker builds normally reuse cached layers found on the host performing the build. Setting the noCache option to true forces the build to ignore cached layers and rerun all steps of the Dockerfile:

  1. strategy:
  2. dockerStrategy:
  3. noCache: true

Force Pull

By default, if the builder image specified in the build configuration is available locally on the node, that image will be used. However, to override the local image and refresh it from the registry to which the image stream points, create a BuildConfig with the forcePull flag set to true:

  1. strategy:
  2. dockerStrategy:
  3. forcePull: true (1)
1This flag causes the local builder image to be ignored, and a fresh version to be pulled from the registry to which the image stream points. Setting forcePull to false results in the default behavior of honoring the image stored locally.

Environment Variables

To make environment variables available to the Docker build process and resulting image, you can add environment variables to the dockerStrategy definition of the BuildConfig.

The environment variables defined there are inserted as a single ENV Dockerfile instruction right after the FROM instruction, so that it can be referenced later on within the Dockerfile.

The variables are defined during build and stay in the output image, therefore they will be present in any container that runs that image as well.

For example, defining a custom HTTP proxy to be used during build and runtime:

  1. dockerStrategy:
  2. ...
  3. env:
  4. - name: "HTTP_PROXY"
  5. value: "http://myproxy.net:5187/"

Cluster administrators can also configure global build settings using Ansible.

You can also manage environment variables defined in the BuildConfig with the oc set env command.

Adding Secrets via Web Console

To add a secret to your build configuration so that it can access a private repository”

  1. Create a new OKD project.

  2. Create a secret that contains credentials for accessing a private source code repository.

  3. Create a docker build configuration.

  4. On the build configuration editor page or in the fromimage page of the web console, set the Source Secret.

  5. Click the Save button.

Docker Build Arguments

To set Docker build arguments, add entries to the BuildArgs array, which is located in the dockerStrategy definition of the BuildConfig. For example:

  1. dockerStrategy:
  2. ...
  3. buildArgs:
  4. - name: "foo"
  5. value: "bar"

The build arguments will be passed to Docker when a build is started.

Enabling Pulling and Pushing

Enable pulling to a private registry by setting the Pull Secret in the build configuration and enable pushing by setting the Push Secret.

Custom Strategy Options

The following options are specific to the Custom build strategy.

FROM Image

Use the customStrategy.from section to indicate the image to use for the custom build:

  1. strategy:
  2. customStrategy:
  3. from:
  4. kind: "DockerImage"
  5. name: "openshift/sti-image-builder"

Exposing the Docker Socket

In order to allow the running of Docker commands and the building of container images from inside the container, the build container must be bound to an accessible socket. To do so, set the exposeDockerSocket option to true:

  1. strategy:
  2. customStrategy:
  3. exposeDockerSocket: true

Secrets

In addition to secrets for source and images that can be added to all build types, custom strategies allow adding an arbitrary list of secrets to the builder pod.

Each secret can be mounted at a specific location:

  1. strategy:
  2. customStrategy:
  3. secrets:
  4. - secretSource: (1)
  5. name: "secret1"
  6. mountPath: "/tmp/secret1" (2)
  7. - secretSource:
  8. name: "secret2"
  9. mountPath: "/tmp/secret2"
1secretSource is a reference to a secret in the same namespace as the build.
2mountPath is the path inside the custom builder where the secret should be mounted.

Adding Secrets via Web Console

To add a secret to your build configuration so that it can access a private repository:

  1. Create a new OKD project.

  2. Create a secret that contains credentials for accessing a private source code repository.

  3. Create a custom build configuration.

  4. On the build configuration editor page or in the fromimage page of the web console, set the Source Secret.

  5. Click the Save button.

Enabling Pulling and Pushing

Enable pulling to a private registry by setting the Pull Secret in the build configuration and enable pushing by setting the Push Secret.

Force Pull

By default, when setting up the build pod, the build controller checks if the image specified in the build configuration is available locally on the node. If so, that image will be used. However, to override the local image and refresh it from the registry to which the image stream points, create a BuildConfig with the forcePull flag set to true:

  1. strategy:
  2. customStrategy:
  3. forcePull: true (1)
1This flag causes the local builder image to be ignored, and a fresh version to be pulled from the registry to which the image stream points. Setting forcePull to false results in the default behavior of honoring the image stored locally.

Environment Variables

To make environment variables available to the Custom build process, you can add environment variables to the customStrategy definition of the BuildConfig.

The environment variables defined there are passed to the pod that runs the custom build.

For example, defining a custom HTTP proxy to be used during build:

  1. customStrategy:
  2. ...
  3. env:
  4. - name: "HTTP_PROXY"
  5. value: "http://myproxy.net:5187/"

Cluster administrators can also configure global build settings using Ansible.

You can also manage environment variables defined in the BuildConfig with the oc set env command.

Pipeline Strategy Options

The following options are specific to the Pipeline build strategy.

Providing the Jenkinsfile

You can provide the Jenkinsfile in one of two ways:

  1. Embed the Jenkinsfile in the build configuration.

  2. Include in the build configuration a reference to the Git repository that contains the Jenkinsfile.

Embedded Definition

  1. kind: "BuildConfig"
  2. apiVersion: "v1"
  3. metadata:
  4. name: "sample-pipeline"
  5. spec:
  6. strategy:
  7. jenkinsPipelineStrategy:
  8. jenkinsfile: |-
  9. node('agent') {
  10. stage 'build'
  11. openshiftBuild(buildConfig: 'ruby-sample-build', showBuildLogs: 'true')
  12. stage 'deploy'
  13. openshiftDeploy(deploymentConfig: 'frontend')
  14. }

Reference to Git Repository

  1. kind: "BuildConfig"
  2. apiVersion: "v1"
  3. metadata:
  4. name: "sample-pipeline"
  5. spec:
  6. source:
  7. git:
  8. uri: "https://github.com/openshift/ruby-hello-world"
  9. strategy:
  10. jenkinsPipelineStrategy:
  11. jenkinsfilePath: some/repo/dir/filename (1)
1The optional jenkinsfilePath field specifies the name of the file to use, relative to the source contextDir. If contextDir is omitted, it defaults to the root of the repository. If jenkinsfilePath is omitted, it defaults to Jenkinsfile.

Environment Variables

To make environment variables available to the Pipeline build process, you can add environment variables to the jenkinsPipelineStrategy definition of the BuildConfig.

Once defined, the environment variables will be set as parameters for any Jenkins job associated with the BuildConfig.

For example:

  1. jenkinsPipelineStrategy:
  2. ...
  3. env:
  4. - name: "FOO"
  5. value: "BAR"

You can also manage environment variables defined in the BuildConfig with the oc set env command.

Mapping Between BuildConfig Environment Variables and Jenkins Job Parameters

When a Jenkins job is created or updated based on changes to a Pipeline strategy BuildConfig, any environment variables in the BuildConfig are mapped to Jenkins job parameters definitions, where the default values for the Jenkins job parameters definitions are the current values of the associated environment variables.

After the Jenkins job’s initial creation, you can still add additional parameters to the job from the Jenkins console. The parameter names differ from the names of the environment variables in the BuildConfig. The parameters are honored when builds are started for those Jenkins jobs.

How you start builds for the Jenkins job dictates how the parameters are set. If you start with oc start-build, the values of the environment variables in the BuildConfig are the parameters set for the corresponding job instance. Any changes you make to the parameters’ default values from the Jenkins console are ignored. The BuildConfig values take precedence.

If you start with oc start-build -e, the values for the environment variables specified in the -e option take precedence. And if you specify an environment variable not listed in the BuildConfig, they will be added as a Jenkins job parameter definitions. Also any changes you make from the Jenkins console to the parameters corresponding to the environment variables are ignored. The BuildConfig and what you specify with oc start-build -e takes precedence.

If you start the Jenkins job via the Jenkins console, then you can control the setting of the parameters via the Jenkins console as part of starting a build for the job.