- OpenShift 教程
- OpenShift - 主页
- OpenShift - 概述
- OpenShift - 类型
- OpenShift - 架构
- OpenShift - 环境设置
- OpenShift - 基本概念
- OpenShift - 入门
- OpenShift - 构建自动化
- OpenShift-CLI
- OpenShift - CLI 操作
- OpenShift - 集群
- OpenShift - 应用程序扩展
- OpenShift - 管理
- OpenShift - Docker 和 Kubernetes
- OpenShift - 安全
- OpenShift 有用资源
- OpenShift - 快速指南
- OpenShift - 有用的资源
- OpenShift - 讨论
OpenShift - 构建自动化
在 OpenShift 中,我们有多种方法来自动化构建管道。为此,我们需要创建一个 BuildConfig 资源来描述构建流程。BuildConfig 中的流程可以与 Jenkins 作业定义中的作业定义进行比较。在创建构建流程时,我们必须选择构建策略。
构建配置文件
在 OpenShift 中,BuildConfig 是一个剩余对象,用于连接到 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
有多种战略方针。
- 力拉
- 增量构建
- 外部构建
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>