7.5. OpenShift Pipeline ビルド
7.5.1. はじめに
シンプルな web サイトを作成する場合も、複雑なマイクロサービス web を作成する場合も、OpenShift Pipeline を使用して、OpenShift でアプリケーションをビルド、テスト、デプロイ、プロモートを実行します。
標準の Jenkins Pipeline 構文のほかにも、OpenShift Jenkins イメージは (OpenShift Jenkins Client プラグインを使用して) OpenShift の Domain Specific Language (DSL) を提供します。 これは、OpenShift API サーバーと高度な対話を行う、読み取り可能でコンパクトで総合的な、かつ Fluent (流れるような) 構文を提供することを目的とし、OpenShift クラスターのアプリケーションのビルド、デプロイメント、プロモートのより詳細な制御が可能になります。
以下の例では、nodejs-mongodb.json
テンプレートを使用して Node.js/MongoDB
アプリケーションをビルドし、デプロイし、検証する OpenShift Pipeline を作成する方法を紹介します。
7.5.2. Jenkins Master の作成
Jenkins master を作成するには以下を実行します。
$ oc project <project_name> 1 $ oc new-app jenkins-ephemeral 2
Jenkins の自動プロビジョニングがクラスターで有効化されており、Jenkins master をカスタマイズする必要がない場合には、以前の手順を省略できます。
7.5.3. Pipeline のビルド設定
Jenkins master が機能するようになったので、Jenkins Pipeline ストラテジーを使用して Node.js/MongoDB
のサンプルアプリケーションをビルドし、デプロイし、スケーリングする BuildConfig を作成します。
以下の内容で nodejs-sample-pipeline.yaml という名前のファイルを作成します。
kind: "BuildConfig" apiVersion: "v1" metadata: name: "nodejs-sample-pipeline" spec: strategy: jenkinsPipelineStrategy: jenkinsfile: <pipeline content from below> type: JenkinsPipeline
Pipeline ビルドストラテジーに関する情報は、「Pipeline ストラテジーオプション」を参照してください。
7.5.4. Jenkinsfile
jenkinsPipelineStrategy
で BuildConfig を作成したら、インラインの jenkinsfile
を使用して、パイプラインに指示を出します。この例では、アプリケーションに Git リポジトリーを設定しません。
以下の jenkinsfile
の内容は、OpenShift DSL を使用して Groovy で記述されています。ソースリポジトリーに jenkinsfile
を追加することが推奨される方法ですが、この例では、YAML Literal Style を使用して BuildConfig にインラインコンテンツを追加しています。
完了した BuildConfig は、OpenShift Origin リポジトリーの examples ディレクトリーの nodejs-sample-pipeline.yaml
で確認できます。
def templatePath = 'https://raw.githubusercontent.com/openshift/nodejs-ex/master/openshift/templates/nodejs-mongodb.json' 1 def templateName = 'nodejs-mongodb-example' 2 pipeline { agent { node { label 'nodejs' 3 } } options { timeout(time: 20, unit: 'MINUTES') 4 } stages { stage('preamble') { steps { script { openshift.withCluster() { openshift.withProject() { echo "Using project: ${openshift.project()}" } } } } } stage('cleanup') { steps { script { openshift.withCluster() { openshift.withProject() { openshift.selector("all", [ template : templateName ]).delete() 5 if (openshift.selector("secrets", templateName).exists()) { 6 openshift.selector("secrets", templateName).delete() } } } } } } stage('create') { steps { script { openshift.withCluster() { openshift.withProject() { openshift.newApp(templatePath) 7 } } } } } stage('build') { steps { script { openshift.withCluster() { openshift.withProject() { def builds = openshift.selector("bc", templateName).related('builds') timeout(5) { 8 builds.untilEach(1) { return (it.object().status.phase == "Complete") } } } } } } } stage('deploy') { steps { script { openshift.withCluster() { openshift.withProject() { def rm = openshift.selector("dc", templateName).rollout().latest() timeout(5) { 9 openshift.selector("dc", templateName).related('pods').untilEach(1) { return (it.object().status.phase == "Running") } } } } } } } stage('tag') { steps { script { openshift.withCluster() { openshift.withProject() { openshift.tag("${templateName}:latest", "${templateName}-staging:latest") 10 } } } } } } }
- 1
- 使用するテンプレートへのパス
- 2
- 作成するテンプレート名
- 3
- このビルドを実行する
node.js
のスレーブ Pod をスピンアップします。 - 4
- この Pipeline に 20 分間のタイムアウトを設定します。
- 5
- このテンプレートラベルが指定されたものすべてを削除します。
- 6
- このテンプレートラベルが付いたシークレットをすべて削除します。
- 7
templatePath
から新規アプリケーションを作成します。- 8
- ビルドが完了するまで最大 5 分待機します。
- 9
- デプロイメントが完了するまで最大 5 分待機します。
- 10
- すべてが正常に完了した場合は、
$ {templateName}:latest
イメージに$ {templateName}-staging:latest
のタグを付けます。ステージング環境向けのパイプラインの BuildConfig は、変更する$ {templateName}-staging:latest
イメージがないかを確認し、このイメージをステージング環境にデプロイします。
以前の例は、declarative pipeline スタイルを使用して記述されていますが、以前の scripted pipeline スタイルもサポートされます。
7.5.5. パイプラインの作成
OpenShift の BuildConfig を作成するには、以下を実行します。
$ oc create -f nodejs-sample-pipeline.yaml
独自のファイルを作成しない場合には、以下を実行して Origin リポジトリーからサンプルを使用できます。
$ oc create -f https://raw.githubusercontent.com/openshift/origin/master/examples/jenkins/pipeline/nodejs-sample-pipeline.yaml
使用する OpenShift DSL 構文の情報は、「OpenShift Jenkins クライアントプラグイン」を参照してください。
7.5.6. パイプラインの起動
以下のコマンドでパイプラインを起動します。
$ oc start-build nodejs-sample-pipeline
または、OpenShift Web コンソールで Builds
パイプラインが起動したら、以下のアクションがプロジェクト内で実行されるはずです。
- ジョブインスタンスが Jenkins サーバー上で作成される
- パイプラインで必要な場合には、スレーブ Pod が起動される
Pipeline がスレーブ Pod で実行されるか、またはスレーブが必要でない場合には master で実行される
-
template=nodejs-mongodb-example
ラベルの付いた以前に作成されたリソースは削除されます。 -
新規アプリケーションおよびそれに関連するすべてのリソースは、
nodejs-mongodb-example
テンプレートで作成されます。 ビルドは
nodejs-mongodb-example
BuildConfig を使用して起動されます。- パイプラインは、ビルドが完了して次のステージをトリガーするまで待機します。
デプロイメントは、
nodejs-mongodb-example
のデプロイメント設定を使用して開始されます。- パイプラインは、デプロイメントが完了して次のステージをトリガーするまで待機します。
-
ビルドとデプロイに成功すると、
nodejs-mongodb-example:latest
イメージがnodejs-mongodb-example:stage
としてトリガーされます。
-
- Pipeline で以前に要求されていた場合には、スレーブ Pod が削除される
OpenShift Web コンソールで確認すると、最適な方法で Pipeline の実行を視覚的に把握することができます。Web コンソールにログインして、Builds