OpenShift - 構建自動化



在 OpenShift 中,我們有多種方法可以自動化構建管道。為此,我們需要建立一個 BuildConfig 資源來描述構建流程。BuildConfig 中的流程可以與 Jenkins 作業定義中的作業定義進行比較。在建立構建流程時,我們必須選擇構建策略。

BuildConfig 檔案

在 OpenShift 中,BuildConfig 是一個用於連線 API 並建立新例項的 REST 物件。

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

有多種策略策略。

  • 強制拉取
  • 增量構建
  • 外部構建

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 並將其附加到構建配置定義中。

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>
廣告

© . All rights reserved.