5.4.4. Pipeline ビルドのチュートリアル

重要

パイプラインビルドストラテジーは OpenShift Container Platform 4 では非推奨になりました。同等の機能および改善機能は、Tekton をベースとする OpenShift Container Platform Pipeline にあります。

OpenShift Container Platform の Jenkins イメージは完全にサポートされており、ユーザーは Jenkins ユーザーのドキュメントに従ってジョブで jenkinsfile を定義するか、またはこれをソースコントロール管理システムに保存します。

以下の例では、nodejs-mongodb.json テンプレートを使用して Node.js/MongoDB アプリケーションをビルドし、デプロイし、検証する OpenShift Container Platform Pipeline を作成する方法を紹介します。

手順

  1. Jenkins マスターを作成するには、以下を実行します。

      $ oc project <project_name>

    oc new-project <project_name> で新規プロジェクトを使用するか、または作成するプロジェクトを選択します。

      $ oc new-app jenkins-ephemeral 1

    永続ストレージを使用する場合は、jenkins-persistent を代わりに使用します。

  2. 以下の内容で nodejs-sample-pipeline.yaml という名前のファイルを作成します。

    注記

    Jenkins Pipeline ストラテジーを使用して Node.js/MongoDB のサンプルアプリケーションをビルドし、デプロイし、スケーリングする BuildConfig オブジェクトを作成します。

    kind: "BuildConfig"
    apiVersion: "v1"
    metadata:
      name: "nodejs-sample-pipeline"
    spec:
      strategy:
        jenkinsPipelineStrategy:
          jenkinsfile: <pipeline content from below>
        type: JenkinsPipeline
  3. jenkinsPipelineStrategyBuildConfig オブジェクトを作成したら、インラインの jenkinsfile を使用して、Pipeline に指示を出します。

    注記

    この例では、アプリケーションに Git リポジトリーを設定しません。

    以下の jenkinsfile の内容は、OpenShift Container Platform DSL を使用して Groovy で記述されています。ソースリポジトリーに jenkinsfile を追加することが推奨される方法ですが、この例では YAML Literal Style を使用して BuildConfig にインラインコンテンツを追加しています。

    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()
                      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
    使用するテンプレートへのパス
    1 2
    作成するテンプレート名
    3
    このビルドを実行する node.js のエージェント Pod をスピンアップします。
    4
    この Pipeline に 20 分間のタイムアウトを設定します。
    5
    このテンプレートラベルが指定されたものすべてを削除します。
    6
    このテンプレートラベルが付いたシークレットをすべて削除します。
    7
    templatePath から新規アプリケーションを作成します。
    8
    ビルドが完了するまで最大 5 分待機します。
    9
    デプロイメントが完了するまで最大 5 分待機します。
    10
    すべてが正常に完了した場合は、$ {templateName}:latest イメージに $ {templateName}-staging:latest のタグを付けます。ステージング環境向けのパイプラインのビルド設定は、変更する $ {templateName}-staging:latest イメージがないかを確認し、このイメージをステージング環境にデプロイします。
    注記

    以前の例は、宣言型のパイプラインスタイルを使用して記述されていますが、以前のスクリプト化されたパイプラインスタイルもサポートされます。

  4. OpenShift Container Platform クラスターに Pipeline BuildConfig を作成します。

    $ oc create -f nodejs-sample-pipeline.yaml
    1. 独自のファイルを作成しない場合には、以下を実行して Origin リポジトリーからサンプルを使用できます。

      $ oc create -f https://raw.githubusercontent.com/openshift/origin/master/examples/jenkins/pipeline/nodejs-sample-pipeline.yaml
  5. Pipeline を起動します。

    $ oc start-build nodejs-sample-pipeline
    注記

    または、OpenShift Container Platform Web コンソールで Builds → Pipeline セクションに移動して、Start Pipeline をクリックするか、Jenkins コンソールから作成した Pipeline に移動して、Build Now をクリックして Pipeline を起動できます。

    パイプラインが起動したら、以下のアクションがプロジェクト内で実行されるはずです。

    • ジョブインスタンスが Jenkins サーバー上で作成される
    • パイプラインで必要な場合には、エージェント Pod が起動される
    • Pipeline がエージェント Pod で実行されるか、またはエージェントが必要でない場合には master で実行される

      • template=nodejs-mongodb-example ラベルの付いた以前に作成されたリソースは削除されます。
      • 新規アプリケーションおよびそれに関連するすべてのリソースは、nodejs-mongodb-example テンプレートで作成されます。
      • ビルドは nodejs-mongodb-example BuildConfig を使用して起動されます。

        • Pipeline は、ビルドが完了して次のステージをトリガーするまで待機します。
      • デプロイメントは、nodejs-mongodb-example のデプロイメント設定を使用して開始されます。

        • パイプラインは、デプロイメントが完了して次のステージをトリガーするまで待機します。
      • ビルドとデプロイに成功すると、nodejs-mongodb-example:latest イメージが nodejs-mongodb-example:stage としてトリガーされます。
    • パイプラインで以前に要求されていた場合には、スレーブ Pod が削除される

      注記

      OpenShift Container Platform Web コンソールで確認すると、最適な方法で Pipeline の実行を視覚的に把握することができます。Web コンソールにログインして、Builds → Pipelines に移動し、Pipeline を確認します。