Invoke scripts embedded in an image

To extend builder image behavior while preserving supported script logic and upgrade compatibility in Red Hat OpenShift Container Platform, you can start embedded S2I image scripts by creating wrapper scripts. These wrapper scripts run custom logic and then call the default scripts from the image.

Procedure
  1. Inspect the value of the io.openshift.s2i.scripts-url label to determine the location of the scripts inside of the builder image:

    $ podman inspect --format='{{ index .Config.Labels "io.openshift.s2i.scripts-url" }}' wildfly/wildfly-centos7
    Example output
    image:///usr/libexec/s2i
  2. Create a script that includes an invocation of one of the standard scripts wrapped in other commands:

    .s2i/bin/assemble script
    #!/bin/bash
    echo "Before assembling"
    
    /usr/libexec/s2i/assemble
    rc=$?
    
    if [ $rc -eq 0 ]; then
        echo "After successful assembling"
    else
        echo "After failed assembling"
    fi
    
    exit $rc

    This example shows a custom assemble script that prints the message, runs the standard assemble script from the image, and prints another message depending on the exit code of the assemble script.

    Important

    When wrapping the run script, you must use exec for invoking it to ensure signals are handled properly. The use of exec also precludes the ability to run additional commands after invoking the default image run script.

    .s2i/bin/run script
    #!/bin/bash
    echo "Before running application"
    exec /usr/libexec/s2i/run
Override 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, complete one of the following actions:

    • 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 in the BuildConfig object. For example:

      strategy:
        sourceStrategy:
          from:
            kind: "ImageStreamTag"
            name: "builder-image:latest"
          scripts: "http://somehost.com/scripts_directory" 1
      1 The build process appends run, assemble, and save-artifacts to the path. If any or all scripts with these names exist, the build process uses these scripts in place of scripts with the same name that are provided in the image.
      Note

      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. The variables that you provide using either method will be present during the build process and in the output image.

Use 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. The complete list of supported environment variables is available in the using images section for each image.

Procedure
  1. To disable assets compilation for your Rails application during the build, add the following line to the .s2i/environment file:

    DISABLE_ASSET_COMPILATION=true
  2. In addition to builds, the specified environment variables are also available in the running application itself. To start the Rails application in development mode instead of production mode, add the following line to the .s2i/environment file:

    RAILS_ENV=development
Ignore 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.