BuildConfig and S2I in Open Shift - unix1998/technical_notes GitHub Wiki

In OpenShift, a BuildConfig is used to define the process for building your application from source code to a runnable image. Whether you need to create a BuildConfig depends on how you are deploying your application. Here are some scenarios:

  1. Source-to-Image (S2I) Builds:

    • When to Use: If you have source code in a repository (e.g., GitHub) and want OpenShift to build the image from the source code.
    • BuildConfig: Required to define how the source code should be built into an image.
  2. Dockerfile Builds:

    • When to Use: If you have a Dockerfile in your repository that defines how to build your image.
    • BuildConfig: Required to define how to build the image using the Dockerfile.
  3. Custom Builds:

    • When to Use: If you have a custom build process.
    • BuildConfig: Required to define how the custom build process should be executed.
  4. Pre-built Images:

    • When to Use: If you are deploying an application using a pre-built image available in a container registry (e.g., Docker Hub, Quay.io).
    • BuildConfig: Not required, as you are not building the image within OpenShift.

Example 1: Source-to-Image (S2I) BuildConfig

Here is an example BuildConfig for an S2I build that uses a Git repository as the source:

apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
  name: nginx-build
spec:
  source:
    type: Git
    git:
      uri: https://github.com/your-org/your-repo.git
  strategy:
    type: Source
    sourceStrategy:
      from:
        kind: ImageStreamTag
        name: nginx:latest
  output:
    to:
      kind: ImageStreamTag
      name: nginx:latest

Example 2: Dockerfile BuildConfig

Here is an example BuildConfig that uses a Dockerfile from a Git repository:

apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
  name: nginx-build
spec:
  source:
    type: Git
    git:
      uri: https://github.com/your-org/your-repo.git
      ref: main
    contextDir: /path/to/dockerfile
  strategy:
    type: Docker
  output:
    to:
      kind: ImageStreamTag
      name: nginx:latest

Example DeploymentConfig

Here is an example DeploymentConfig to deploy the built image:

apiVersion: apps.openshift.io/v1
kind: DeploymentConfig
metadata:
  name: nginx
spec:
  replicas: 3
  selector:
    app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80
  triggers:
  - type: ConfigChange
  - type: ImageChange
    imageChangeParams:
      automatic: true
      containerNames:
      - nginx
      from:
        kind: ImageStreamTag
        name: nginx:latest

When You Don't Need a BuildConfig

If you are deploying an Nginx application using a pre-built image, you do not need a BuildConfig. You can directly create a Deployment or DeploymentConfig.

Example Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        ports:
        - containerPort: 80

Summary

  • Use a BuildConfig if you need OpenShift to build your application image from source code or Dockerfile.
  • Skip BuildConfig if you are deploying an application using a pre-built image from a container registry.

This approach allows you to use the appropriate resource definitions based on your deployment needs in OpenShift.