Openshift 简明教程

OpenShift - Build Automation

在 OpenShift 中,我们有多种自动化构建流程的方法。为此,我们需要创建一个 BuildConfig 资源来描述构建流程。BuildConfig 中的流程可以与 Jenkins 作业定义中的作业定义进行比较。创建构建流程时,我们必须选择构建策略。

In OpenShift, we have multiple methods of automating the build pipeline. In order to do that we need to create a BuildConfig resource to describe the build flow. The flow in BuildConfig can be compared with the job definition in Jenkins job definition. While creating the build flow, we have to choose the build strategy.

BuildConfig File

在 OpenShift 中,BuildConfig 是用于连接 API 的 rest 对象,然后创建一个新实例。

In OpenShift, BuildConfig is a rest object used to connect to API and then create a new instance.

kind: "BuildConfig"
apiVersion: "v1"
metadata:
   name: "<Name of build config file>"
spec:
   runPolicy: "Serial"
   triggers:
   -
      type: "GitHub"
      github:
         secret: "<Secrete file name>"
   - type: "Generic"
   generic:
      secret: "secret101"
   -
   type: "ImageChange"
   source:
      type: "<Source of code>"
      git:
   uri: "https://github.com/openshift/openshift-hello-world"
   dockerfile: "FROM openshift/openshift-22-centos7\nUSER example"
   strategy:
      type: "Source"

sourceStrategy:
   from:
      kind: "ImageStreamTag"
      name: "openshift-20-centos7:latest"
   output:
      to:
         kind: "ImageStreamTag"
         name: "origin-openshift-sample:latest"
   postCommit:
      script: "bundle exec rake test"

在 OpenShift 中,有四种类型的构建策略。

In OpenShift, there are four types of build strategies.

  1. Source-to-image strategy

  2. Docker strategy

  3. Custom strategy

  4. Pipeline strategy

Source-to-image Strategy

允许从源代码开始创建容器映像。在此流程中,实际代码首先下载到容器中,然后在其中进行编译。已编译代码部署到同一容器中,并从该代码构建映像。

Allows creating container images starting from the source code. In this flow, the actual code gets downloaded first in the container and then gets compiled inside it. The compiled code gets deployed inside the same container and the image is built from that code.

strategy:
   type: "Source"
   sourceStrategy:
      from:
         kind: "ImageStreamTag"
         name: "builder-image:latest"
      forcePull: true

有多个策略。

There are multiple strategy policies.

  1. Forcepull

  2. Incremental Builds

  3. External Builds

Docker Strategy

在此流程中,OpenShift 使用 Dockerfile 构建映像,然后将创建的映像上传到 Docker 注册表。

In this flow, OpenShift uses Dockerfile to build the image and then upload the created images to the Docker registry.

strategy:
   type: Docker
   dockerStrategy:
      from:
         kind: "ImageStreamTag"
         name: "ubuntu:latest"

Docker 文件选项可以在文件路径、无缓存和强制拉取等多个位置使用。

Docker file option can be used in multiple locations starting from file path, no cache, and force pull.

  1. From Image

  2. Dockerfile path

  3. No cache

  4. Force pull

Custom Strategy

这是不同类型的构建策略之一,其中并不强制构建输出为映像。它可以与 Jenkins 的自由式作业进行比较。使用此策略,我们可以创建 Jar、rpm 和其他软件包。

This is one of the different kinds of build strategy, wherein there is no such compulsion that the output of the build is going to be an image. It can be compared to a free style job of Jenkins. With this, we can create Jar, rpm, and other packages.

strategy:
   type: "Custom"
   customStrategy:
      from:
         kind: "DockerImage"
         name: "openshift/sti-image-builder"

它包含多个构建策略。

It consists of multiple build strategies.

  1. Expose Docker socket

  2. Secrets

  3. Force pull

Pipeline Strategy

流水线策略用于创建自定义构建流水线。这主要用于在流水线中实现工作流。此构建流使用 Groovy DSL 语言自定义构建流水线流。OpenShift 将在 Jenkins 中创建一个流水线作业并执行它。此流水线流也可以在 Jenkins 中使用。在此策略中,我们使用 Jenkinsfile 并将其附加在 buildconfig 定义中。

Pipeline strategy is used to create custom build pipelines. This is basically used to implement the workflow in the pipeline. This build flow uses custom build pipeline flow using Groovy DSL language. OpenShift will create a pipeline job in Jenkins and execute it. This pipeline flow can also be used in Jenkins. In this strategy, we use Jenkinsfile and append that in the buildconfig definition.

Strategy:
   type: "JenkinsPipeline"
   jenkinsPipelineStrategy:
   jenkinsfile: "node('agent') {\nstage 'build'\nopenshiftBuild(buildConfig: 'OpenShift-build', showBuildLogs: 'true')\nstage 'deploy'\nopenshiftDeploy(deploymentConfig: 'backend')\n}"

Using build pipeline

Using build pipeline

kind: "BuildConfig"
apiVersion: "v1"
metadata:
   name: "test-pipeline"
spec:
   source:
      type: "Git"
      git:
         uri: "https://github.com/openshift/openshift-hello-world"
   strategy:
      type: "JenkinsPipeline"
      jenkinsPipelineStrategy:
         jenkinsfilePath: <file path repository>