Using build strategies

The following sections define the primary supported build strategies, and how to use them.

Docker build

OKD uses Buildah to build a container image from a Dockerfile. For more information on building container images with Dockerfiles, see the Dockerfile reference documentation.

If you set Docker build arguments by using the buildArgs array, see Understand how ARG and FROM interact in the Dockerfile reference documentation.

Replacing Dockerfile FROM image

You can replace the FROM instruction of the Dockerfile with the from of the BuildConfig object. If the Dockerfile uses multi-stage builds, the image in the last FROM instruction will be replaced.

Procedure

To replace the FROM instruction of the Dockerfile with the from of the BuildConfig.

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

Using Dockerfile path

By default, docker builds use a 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 a different file name than the default Dockerfile, such as MyDockerfile, or a path to a Dockerfile in a subdirectory, such as dockerfiles/app1/Dockerfile.

Procedure

To use the dockerfilePath field for the build to use a different path to locate your Dockerfile, set:

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

Using docker 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 build configuration.

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.

Procedure

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/"

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

Adding docker build arguments

You can set docker build arguments using the buildArgs array. The build arguments are passed to docker when a build is started.

See Understand how ARG and FROM interact in the Dockerfile reference documentation.

Procedure

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

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

Only the name and value fields are supported. Any settings on the valueFrom field are ignored.

Squash layers with docker builds

Docker builds normally create a layer representing each instruction in a Dockerfile. Setting the imageOptimizationPolicy to SkipLayers merges all instructions into a single layer on top of the base image.

Procedure

  • Set the imageOptimizationPolicy to SkipLayers:

    1. strategy:
    2. dockerStrategy:
    3. imageOptimizationPolicy: SkipLayers

Source-to-image build

Source-to-image (S2I) is a tool for building reproducible container images. It produces ready-to-run images by injecting application source into a container image and assembling a new image. The new image incorporates the base image, the builder, and built source and is ready to use with the buildah run command. S2I supports incremental builds, which re-use previously downloaded dependencies, previously built artifacts, and so on.

Performing source-to-image incremental builds

Source-to-image (S2I) can perform incremental builds, which means it reuses artifacts from previously-built images.

Procedure

  • To create an incremental build, create a 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.

Additional resources

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

Overriding source-to-image builder image scripts

You can override the assemble, run, and save-artifacts source-to-image (S2I) scripts provided by the builder image.

Procedure

To override the assemble, run, and save-artifacts S2I scripts provided by the builder image, either:

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

  • 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 scripts provided in the image.

Files located at the scripts URL take precedence over files located in .s2i/bin of the source repository.

Source-to-image 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.

Using source-to-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.

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

Procedure

For example, to disable assets compilation for your Rails application during the build:

  • Add DISABLE_ASSET_COMPILATION=true in the .s2i/environment file.

In addition to builds, the specified environment variables are also available in the running application itself. For example, to cause the Rails application to start in development mode instead of production:

  • Add RAILS_ENV=development to the .s2i/environment file.

The complete list of supported environment variables is available in the using images section for each image.

Using source-to-image build configuration environment

You can add environment variables to the sourceStrategy definition of the build configuration. 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.

Procedure

  • For example, to disable assets compilation for your Rails application:

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

Additional resources

  • The build environment section provides more advanced instructions.

  • You can also manage environment variables defined in the build configuration with the oc set env command.

Ignoring source-to-image source files

Source-to-image (S2I) 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.

Creating images from source code with source-to-image

Source-to-image (S2I) is a framework that makes it easy to write images that take application source code as an input and produce a new image that runs the assembled application as output.

The main advantage of using S2I for building reproducible container images is the ease of use for developers. As a builder image author, you must understand two basic concepts in order for your images to provide the best S2I performance, the build process and S2I scripts.

Understanding the source-to-image build process

The build process consists of the following three fundamental elements, which are combined into a final container image:

  • Sources

  • Source-to-image (S2I) scripts

  • Builder image

S2I generates a Dockerfile with the builder image as the first FROM instruction. The Dockerfile generated by S2I is then passed to Buildah.

How to write source-to-image scripts

You can write source-to-image (S2I) scripts in any programming language, as long as the scripts are executable inside the builder image. S2I supports multiple options providing assemble/run/save-artifacts scripts. All of these locations are checked on each build in the following order:

  1. A script specified in the build configuration.

  2. A script found in the application source .s2i/bin directory.

  3. A script found at the default image URL with the io.openshift.s2i.scripts-url label.

Both the io.openshift.s2i.scripts-url label specified in the image and the script specified in a build configuration can take one of the following forms:

  • image:///path_to_scripts_dir: absolute path inside the image to a directory where the S2I scripts are located.

  • file:///path_to_scripts_dir: relative or absolute path to a directory on the host where the S2I scripts are located.

  • http(s)://path_to_scripts_dir: URL to a directory where the S2I scripts are located.

Table 1. S2I scripts
ScriptDescription

assemble

The assemble script builds the application artifacts from a source and places them into appropriate directories inside the image. This script is required. The workflow for this script is:

  1. Optional: Restore build artifacts. If you want to support incremental builds, make sure to define save-artifacts as well.

  2. Place the application source in the desired location.

  3. Build the application artifacts.

  4. Install the artifacts into locations appropriate for them to run.

run

The run script executes your application. This script is required.

save-artifacts

The save-artifacts script gathers all dependencies that can speed up the build processes that follow. This script is optional. For example:

  • For Ruby, gems installed by Bundler.

  • For Java, .m2 contents.

These dependencies are gathered into a tar file and streamed to the standard output.

usage

The usage script allows you to inform the user how to properly use your image. This script is optional.

test/run

The test/run script allows you to create a process to check if the image is working correctly. This script is optional. The proposed flow of that process is:

  1. Build the image.

  2. Run the image to verify the usage script.

  3. Run s2i build to verify the assemble script.

  4. Optional: Run s2i build again to verify the save-artifacts and assemble scripts save and restore artifacts functionality.

  5. Run the image to verify the test application is working.

The suggested location to put the test application built by your test/run script is the test/test-app directory in your image repository.

Example S2I scripts

The following example S2I scripts are written in Bash. Each example assumes its tar contents are unpacked into the /tmp/s2i directory.

assemble script:

  1. #!/bin/bash
  2. # restore build artifacts
  3. if [ "$(ls /tmp/s2i/artifacts/ 2>/dev/null)" ]; then
  4. mv /tmp/s2i/artifacts/* $HOME/.
  5. fi
  6. # move the application source
  7. mv /tmp/s2i/src $HOME/src
  8. # build application artifacts
  9. pushd ${HOME}
  10. make all
  11. # install the artifacts
  12. make install
  13. popd

run script:

  1. #!/bin/bash
  2. # run the application
  3. /opt/application/run.sh

save-artifacts script:

  1. #!/bin/bash
  2. pushd ${HOME}
  3. if [ -d deps ]; then
  4. # all deps contents to tar stream
  5. tar cf - deps
  6. fi
  7. popd

usage script:

  1. #!/bin/bash
  2. # inform the user how to use the image
  3. cat <<EOF
  4. This is a S2I sample builder image, to use it, install
  5. https://github.com/openshift/source-to-image
  6. EOF

Additional resources

Custom build

The custom build strategy allows developers to define a specific builder image responsible for the entire build process. Using your own builder image allows you to customize your build process.

A custom builder image is a plain container image embedded with build process logic, for example for building RPMs or base images.

Custom builds run with a high level of privilege and are not available to users by default. Only users who can be trusted with cluster administration permissions should be granted access to run custom builds.

Using FROM image for custom builds

You can use the customStrategy.from section to indicate the image to use for the custom build

Procedure

  • Set the customStrategy.from section:

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

Using secrets in custom builds

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.

Procedure

  • To mount each secret at a specific location, edit the secretSource and mountPath fields of the strategy YAML file:

    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.

Using environment variables for custom builds

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

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

Procedure

  1. Define a custom HTTP proxy to be used during build:

    1. customStrategy:
    2. ...
    3. env:
    4. - name: "HTTP_PROXY"
    5. value: "http://myproxy.net:5187/"
  2. To manage environment variables defined in the build configuration, enter the following command:

    1. $ oc set env <enter_variables>

Using custom builder images

OKD’s custom build strategy enables you to define a specific builder image responsible for the entire build process. When you need a build to produce individual artifacts such as packages, JARs, WARs, installable ZIPs, or base images, use a custom builder image using the custom build strategy.

A custom builder image is a plain container image embedded with build process logic, which is used for building artifacts such as RPMs or base container images.

Additionally, the custom builder allows implementing any extended build process, such as a CI/CD flow that runs unit or integration tests.

Custom builder image

Upon invocation, a custom builder image receives the following environment variables with the information needed to proceed with the build:

Table 2. Custom Builder Environment Variables
Variable NameDescription

BUILD

The entire serialized JSON of the Build object definition. If you must use a specific API version for serialization, you can set the buildAPIVersion parameter in the custom strategy specification of the build configuration.

SOURCE_REPOSITORY

The URL of a Git repository with source to be built.

SOURCE_URI

Uses the same value as SOURCE_REPOSITORY. Either can be used.

SOURCE_CONTEXT_DIR

Specifies the subdirectory of the Git repository to be used when building. Only present if defined.

SOURCE_REF

The Git reference to be built.

ORIGIN_VERSION

The version of the OKD master that created this build object.

OUTPUT_REGISTRY

The container image registry to push the image to.

OUTPUT_IMAGE

The container image tag name for the image being built.

PUSH_DOCKERCFG_PATH

The path to the container registry credentials for running a podman push operation.

Custom builder workflow

Although custom builder image authors have flexibility in defining the build process, your builder image must adhere to the following required steps necessary for running a build inside of OKD:

  1. The Build object definition contains all the necessary information about input parameters for the build.

  2. Run the build process.

  3. If your build produces an image, push it to the output location of the build if it is defined. Other output locations can be passed with environment variables.

Pipeline build

The Pipeline build strategy is deprecated in OKD 4. Equivalent and improved functionality is present in the OKD Pipelines based on Tekton.

Jenkins images on OKD are fully supported and users should follow Jenkins user documentation for defining their jenkinsfile in a job or store it in a Source Control Management system.

The Pipeline build strategy allows developers to define a Jenkins pipeline for use by the Jenkins pipeline plug-in. The build can be started, monitored, and managed by OKD in the same way as any other build type.

Pipeline workflows are defined in a jenkinsfile, either embedded directly in the build configuration, or supplied in a Git repository and referenced by the build configuration.

Understanding OKD pipelines

The Pipeline build strategy is deprecated in OKD 4. Equivalent and improved functionality is present in the OKD Pipelines based on Tekton.

Jenkins images on OKD are fully supported and users should follow Jenkins user documentation for defining their jenkinsfile in a job or store it in a Source Control Management system.

Pipelines give you control over building, deploying, and promoting your applications on OKD. Using a combination of the Jenkins Pipeline build strategy, jenkinsfiles, and the OKD Domain Specific Language (DSL) provided by the Jenkins Client Plug-in, you can create advanced build, test, deploy, and promote pipelines for any scenario.

OKD Jenkins Sync Plugin

The OKD Jenkins Sync Plugin keeps the build configuration and build objects in sync with Jenkins jobs and builds, and provides the following:

  • Dynamic job and run creation in Jenkins.

  • Dynamic creation of agent pod templates from image streams, image stream tags, or config maps.

  • Injecting of environment variables.

  • Pipeline visualization in the OKD web console.

  • Integration with the Jenkins git plugin, which passes commit information from

  • Synchronizing secrets into Jenkins credential entries OKD builds to the Jenkins git plugin.

OKD Jenkins Client Plugin

The OKD Jenkins Client Plugin is a Jenkins plugin which aims to provide a readable, concise, comprehensive, and fluent Jenkins Pipeline syntax for rich interactions with an OKD API Server. The plugin uses the OKD command line tool, oc, which must be available on the nodes executing the script.

The Jenkins Client Plug-in must be installed on your Jenkins master so the OKD DSL will be available to use within the jenkinsfile for your application. This plug-in is installed and enabled by default when using the OKD Jenkins image.

For OKD Pipelines within your project, you will must use the Jenkins Pipeline Build Strategy. This strategy defaults to using a jenkinsfile at the root of your source repository, but also provides the following configuration options:

  • An inline jenkinsfile field within your build configuration.

  • A jenkinsfilePath field within your build configuration that references the location of the jenkinsfile to use relative to the source contextDir.

The 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.

Providing the Jenkins file for pipeline builds

The Pipeline build strategy is deprecated in OKD 4. Equivalent and improved functionality is present in the OKD Pipelines based on Tekton.

Jenkins images on OKD are fully supported and users should follow Jenkins user documentation for defining their jenkinsfile in a job or store it in a Source Control Management system.

The jenkinsfile uses the standard groovy language syntax to allow fine grained control over the configuration, build, and deployment of your application.

You can supply the jenkinsfile in one of the following ways:

  • A file located within your source code repository.

  • Embedded as part of your build configuration using the jenkinsfile field.

When using the first option, the jenkinsfile must be included in your applications source code repository at one of the following locations:

  • A file named jenkinsfile at the root of your repository.

  • A file named jenkinsfile at the root of the source contextDir of your repository.

  • A file name specified via the jenkinsfilePath field of the JenkinsPipelineStrategy section of your BuildConfig, which is relative to the source contextDir if supplied, otherwise it defaults to the root of the repository.

The jenkinsfile is run on the Jenkins agent pod, which must have the OKD client binaries available if you intend to use the OKD DSL.

Procedure

To provide the Jenkins file, you can either:

  • Embed the Jenkins file in the build configuration.

  • Include in the build configuration a reference to the Git repository that contains the Jenkins file.

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.

Using environment variables for pipeline builds

The Pipeline build strategy is deprecated in OKD 4. Equivalent and improved functionality is present in the OKD Pipelines based on Tekton.

Jenkins images on OKD are fully supported and users should follow Jenkins user documentation for defining their jenkinsfile in a job or store it in a Source Control Management system.

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

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

Procedure

  • To define environment variables to be used during build, edit the YAML file:

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

You can also manage environment variables defined in the build configuration 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 build configuration, any environment variables in the build configuration 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 build configuration. 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 build configuration 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 build configuration values take precedence.

  • If you start with oc start-build -e, the values for the environment variables specified in the -e option take precedence.

    • If you specify an environment variable not listed in the build configuration, they will be added as a Jenkins job parameter definitions.

    • Any changes you make from the Jenkins console to the parameters corresponding to the environment variables are ignored. The build configuration and what you specify with oc start-build -e takes precedence.

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

It is recommended that you specify in the build configuration all possible environment variables to be associated with job parameters. Doing so reduces disk I/O and improves performance during Jenkins processing.

Pipeline build tutorial

The Pipeline build strategy is deprecated in OKD 4. Equivalent and improved functionality is present in the OKD Pipelines based on Tekton.

Jenkins images on OKD are fully supported and users should follow Jenkins user documentation for defining their jenkinsfile in a job or store it in a Source Control Management system.

This example demonstrates how to create an OKD Pipeline that will build, deploy, and verify a Node.js/MongoDB application using the nodejs-mongodb.json template.

Procedure

  1. Create the Jenkins master:

    1. $ oc project <project_name>

    Select the project that you want to use or create a new project with oc new-project <project_name>.

    1. $ oc new-app jenkins-ephemeral (2)

    If you want to use persistent storage, use jenkins-persistent instead.

  2. Create a file named nodejs-sample-pipeline.yaml with the following content:

    This creates a BuildConfig object that employs the Jenkins pipeline strategy to build, deploy, and scale the Node.js/MongoDB example application.

    1. kind: "BuildConfig"
    2. apiVersion: "v1"
    3. metadata:
    4. name: "nodejs-sample-pipeline"
    5. spec:
    6. strategy:
    7. jenkinsPipelineStrategy:
    8. jenkinsfile: <pipeline content from below>
    9. type: JenkinsPipeline
  3. Once you create a BuildConfig object with a jenkinsPipelineStrategy, tell the pipeline what to do by using an inline jenkinsfile:

    This example does not set up a Git repository for the application.

    The following jenkinsfile content is written in Groovy using the OKD DSL. For this example, include inline content in the BuildConfig object using the YAML Literal Style, though including a jenkinsfile in your source repository is the preferred method.

    1. def templatePath = 'https://raw.githubusercontent.com/openshift/nodejs-ex/master/openshift/templates/nodejs-mongodb.json' (1)
    2. def templateName = 'nodejs-mongodb-example' (2)
    3. pipeline {
    4. agent {
    5. node {
    6. label 'nodejs' (3)
    7. }
    8. }
    9. options {
    10. timeout(time: 20, unit: 'MINUTES') (4)
    11. }
    12. stages {
    13. stage('preamble') {
    14. steps {
    15. script {
    16. openshift.withCluster() {
    17. openshift.withProject() {
    18. echo "Using project: ${openshift.project()}"
    19. }
    20. }
    21. }
    22. }
    23. }
    24. stage('cleanup') {
    25. steps {
    26. script {
    27. openshift.withCluster() {
    28. openshift.withProject() {
    29. openshift.selector("all", [ template : templateName ]).delete() (5)
    30. if (openshift.selector("secrets", templateName).exists()) { (6)
    31. openshift.selector("secrets", templateName).delete()
    32. }
    33. }
    34. }
    35. }
    36. }
    37. }
    38. stage('create') {
    39. steps {
    40. script {
    41. openshift.withCluster() {
    42. openshift.withProject() {
    43. openshift.newApp(templatePath) (7)
    44. }
    45. }
    46. }
    47. }
    48. }
    49. stage('build') {
    50. steps {
    51. script {
    52. openshift.withCluster() {
    53. openshift.withProject() {
    54. def builds = openshift.selector("bc", templateName).related('builds')
    55. timeout(5) { (8)
    56. builds.untilEach(1) {
    57. return (it.object().status.phase == "Complete")
    58. }
    59. }
    60. }
    61. }
    62. }
    63. }
    64. }
    65. stage('deploy') {
    66. steps {
    67. script {
    68. openshift.withCluster() {
    69. openshift.withProject() {
    70. def rm = openshift.selector("dc", templateName).rollout()
    71. timeout(5) { (9)
    72. openshift.selector("dc", templateName).related('pods').untilEach(1) {
    73. return (it.object().status.phase == "Running")
    74. }
    75. }
    76. }
    77. }
    78. }
    79. }
    80. }
    81. stage('tag') {
    82. steps {
    83. script {
    84. openshift.withCluster() {
    85. openshift.withProject() {
    86. openshift.tag("${templateName}:latest", "${templateName}-staging:latest") (10)
    87. }
    88. }
    89. }
    90. }
    91. }
    92. }
    93. }
    1Path of the template to use.
    2Name of the template that will be created.
    3Spin up a node.js agent pod on which to run this build.
    4Set a timeout of 20 minutes for this pipeline.
    5Delete everything with this template label.
    6Delete any secrets with this template label.
    7Create a new application from the templatePath.
    8Wait up to five minutes for the build to complete.
    9Wait up to five minutes for the deployment to complete.
    10If everything else succeeded, tag the $ {templateName}:latest image as $ {templateName}-staging:latest. A pipeline build configuration for the staging environment can watch for the $ {templateName}-staging:latest image to change and then deploy it to the staging environment.

    The previous example was written using the declarative pipeline style, but the older scripted pipeline style is also supported.

  4. Create the Pipeline BuildConfig in your OKD cluster:

    1. $ oc create -f nodejs-sample-pipeline.yaml
    1. If you do not want to create your own file, you can use the sample from the Origin repository by running:

      1. $ oc create -f https://raw.githubusercontent.com/openshift/origin/master/examples/jenkins/pipeline/nodejs-sample-pipeline.yaml
  5. Start the Pipeline:

    1. $ oc start-build nodejs-sample-pipeline

    Alternatively, you can start your pipeline with the OKD web console by navigating to the Builds → Pipeline section and clicking Start Pipeline, or by visiting the Jenkins Console, navigating to the Pipeline that you created, and clicking Build Now.

    Once the pipeline is started, you should see the following actions performed within your project:

    • A job instance is created on the Jenkins server.

    • An agent pod is launched, if your pipeline requires one.

    • The pipeline runs on the agent pod, or the master if no agent is required.

      • Any previously created resources with the template=nodejs-mongodb-example label will be deleted.

      • A new application, and all of its associated resources, will be created from the nodejs-mongodb-example template.

      • A build will be started using the nodejs-mongodb-example BuildConfig.

        • The pipeline will wait until the build has completed to trigger the next stage.
      • A deployment will be started using the nodejs-mongodb-example deployment configuration.

        • The pipeline will wait until the deployment has completed to trigger the next stage.
      • If the build and deploy are successful, the nodejs-mongodb-example:latest image will be tagged as nodejs-mongodb-example:stage.

    • The agent pod is deleted, if one was required for the pipeline.

      The best way to visualize the pipeline execution is by viewing it in the OKD web console. You can view your pipelines by logging in to the web console and navigating to Builds → Pipelines.

Adding secrets with web console

You can add a secret to your build configuration so that it can access a private repository.

Procedure

To add a secret to your build configuration so that it can access a private repository from the OKD web console:

  1. Create a new OKD project.

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

  3. Create a 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

You can enable pulling to a private registry by setting the pull secret and pushing by setting the push secret in the build configuration.

Procedure

To enable pulling to a private registry:

  • Set the pull secret in the build configuration.

To enable pushing:

  • Set the push secret in the build configuration.