OpenShift - 构建自动化

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

BuildConfig文件

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

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中,有四种类型的构建策略.

  • 源到图像策略

  • Docker策略

  • 自定义策略

  • 管道策略

源到图像策略

允许从源代码开始创建容器图像.在此流程中,实际代码首先在容器中下载,然后在其中进行编译.编译后的代码部署在同一个容器中,图像是根据该代码构建的.

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

有多种策略政策.

  • Forcepull

  • 增量构建

  • 外部构建

Docker策略

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

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

Docker文件选项可以在从文件路径,无缓存和强制拉取开始的多个位置使用.

  • 来自图片

  • Dockerfile路径

  • 无缓存

  • 强制拉动

自定义策略

这是不同类型的构建策略之一,其中没有如此强制,构建的输出将成为一个图像.它可以与Jenkins的自由风格作业进行比较.有了这个,我们可以创建Jar,rpm和其他包.

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

它由多个构建策略组成.

  • 暴露Docker套接字

  • 秘密

  • 强制拉动

管道策略

管道策略用于创建自定义构建管道.这基本上用于实现管道中的工作流程.此构建流程使用Groovy DSL语言使用自定义构建管道流程. OpenShift将在Jenkins中创建一个管道作业并执行它.此管道流程也可用于Jenkins.在这个策略中,我们使用Jenkinsfile并将其附加到buildconfig定义中.

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

使用构建管道

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>