Openshift-build-automation
OpenShift-ビルドオートメーション
OpenShiftには、ビルドパイプラインを自動化する複数の方法があります。 そのためには、ビルドフローを記述するBuildConfigリソースを作成する必要があります。 BuildConfigのフローは、Jenkinsジョブ定義のジョブ定義と比較できます。 ビルドフローの作成中に、ビルド戦略を選択する必要があります。
BuildConfigファイル
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には、4種類のビルド戦略があります。
- ソースからイメージへの戦略
- 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パス
- キャッシュなし
- 強制プル
カスタム戦略
これは、さまざまな種類のビルド戦略の1つであり、ビルドの出力がイメージになるような強制はありません。 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>