Skip to content

Latest commit

 

History

History
128 lines (106 loc) · 4.79 KB

20200518-ws.md

File metadata and controls

128 lines (106 loc) · 4.79 KB

20200518ワークショップ

事前準備

OpenShiftプロジェクトの作成

userXXは利用しているユーザの番号に変更してください。

$ oc new-project userX-development
$ oc policy add-role-to-user edit system:serviceaccount:app-devops:jenkins -n userX-development

レポジトリのフォーク

Githubでこのレポジトリをフォークし、自分のアカウントにレポジトリをコピーしましょう。

また1箇所変更が必要です。Jenkinsfileの3行目を自分のプロジェクト名に変更しましょう。

$ vim Jenkinsfile
def deploy_project = "userX-development"

Jenkinsへのアクセス

プロジェクト

アプリケーションのパイプラインを実行するためにJenkins Itemを作成する。

  1. "New Item"を選択
  2. Itemの種類は"Pipeline"を選択し、userX-jobの名前をつける。わかりやすいようにユーザ名を先頭に付けてください。
  3. "Build Triggers"項目で"Generic Webhook Trigger"にチェックを付け下記を設定する
    • token: 任意の文字列(他の人とかぶらないようにユーザ名を含めてください。)
  4. "Pipeline"の項目で実行するパイプライン定義を設定する
    • "pipeline script from SCM"を選択
    • SCMに"Git"を選択
    • Repository URLにはフォークした自分のレポジトリのURLを入力
    • Branches to buildは*/20200518を入力
    • これでSave

GitレポジトリへWebhook設定

お使いのGitレポジトリへWebhookの設定を行う。 Generic Webhook Triggerの場合下記のURLでトリガーできる。

https://jenkins-app-devops.apps.cluster-393d.393d.sandbox861.opentlc.com/generic-webhook-trigger/invoke?token=<your-token>

パイプライン実行

ここまでできたらパイプラインを実行すれば、アプリケーションがデプロイされる。 アプリケーションの変更にも自動でデプロイできる状態だ。 レポジトリに任意の変更を加えてみよう。

jenkins-stage-view

動作確認

初回は、データベースがないため、アプリケーションの起動に一度失敗するが、 データベースが起動後にアプリケーションが自動的に起動するはずだ。

最後は起動したアプリケーションにブラウザから接続して確認してみよう。 URLはhttps://xxxxxxxxxxxx/hello or https://xxxxxxxxxxxx/freelancers

URLは下記から確認できる。

$ oc get route

なにが起きているのか解説

ここまでできたところで、一度なにが行われたのか解説します。

  • Jenkinsfile
    • test
    • application build
    • image build
    • deploy
  • Kubernetes manifests
  • Jenkins agent

アプリケーションに変更を加える

テストを落としてみる

現状のサンプルアプリケーションでは/hello"hello": "world"を含むAPIを返すことがテストとして書かれています。 src/main/java/com/redhat/freelancer4j/freelancer/rest/HealthCheckEndpoint.javaの34行目の map.put("hello", "world"); を任意の文字列に変更してみよう。

    @GET
    @Path("/hello")
    @Produces(MediaType.APPLICATION_JSON)
    public Map<String, String> getHello() {
        HashMap<String, String> map = new HashMap<>();
        map.put("hello", "world!!!!!");
        map.put("foo", "bar");
        return map;
    }

テストを変更してデプロイする

src/test/java/com/redhat/freelancer4j/freelancer/rest/HealtCheckEndpointTest.javaのテストを変更して変更をデプロイしてみます。

    @Test
    public void invokeHello() throws Exception {
        given().get("/hello").then().assertThat().statusCode(200).body("hello", equalTo("world!!!!!"));
    }

パイプラインに処理を追加する

最後に、実際にパイプラインの処理を追加してみましょう。
上までの前提では何が問題でしょうか?
デプロイしただけでそれがうまく機能しているかどうかは確認できていません。 簡易なShellを使って機能の正常性を確認するパイプラインを追加してみましょう。

//TODO
script {
  openshift.withCluster() {
    openshift.withProject("${deploy_project}") {
      def dc = openshift.selector("route", "${app_name}").object()
      def url = dc.spec.host
      echo "${url}"
      while (true) {
        def app_status = sh(returnStdout: true, script: "curl ${url}/hello -o /dev/null -w '%{http_code}' -s").trim()
        if(app_status == "200") {
          break;
        }
        sleep 5
      }
    }
  }
}