第6章 Webhook のシークレットの作成
汎用、GitHub、または GitLab の Webhook で使用して Git リポジトリーでアプリケーションビルドをトリガーできるシークレットを作成できます。アプリケーションコードに使用する Git ホスティングプラットフォームのタイプに応じて、JWS Operator は、Web アプリケーションのカスタムリソースファイルでシークレットを指定するために使用できる webhookSecrets:generic
、webhookSecrets:github
、および webhookSecrets:gitlab
パラメーターを提供します。
手順
Base64 でエンコードされたシークレット文字列を作成します。
以下に例を示します。
echo -n "qwerty" | base64
上記のコマンドは、プレーンテキスト文字列
qwerty
をエンコードし、エンコードされた文字列を表示します。以下に例を示します。
cXdlcnR5
Secret
という種類のオブジェクトを定義するsecret.yaml
ファイルを作成します。以下に例を示します。
kind: Secret apiVersion: v1 metadata: name: jws-secret data: WebHookSecretKey: cXdlcnR5
上記の例では、
jws-secret
はシークレットの名前であり、cXdlcnR5
はエンコードされたシークレット文字列です。シークレットを作成するには、次のコマンドを入力します。
oc create -f secret.yaml
上記のコマンドは、シークレットが作成されたことを確認するメッセージを表示します。
以下に例を示します。
secret/jws-secret created
前の例に基づいて、
webhookSecrets:generic
パラメーターをjws-secret
に設定できます。
検証
Webhook の URL を取得します。
oc describe BuildConfig | grep webhooks
上記のコマンドは、次の形式で Webhook URL を生成します。
https://<host>:<port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/generic
Webhook にリクエストを送信するには、次の
curl
コマンドを入力します。curl -k -X POST https://<host>:<port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/generic
上記のコマンドで、URL 文字列内の
<host>
、<port>
、<namespace>
、<name>
を、ご使用の環境に適した値に置き換えます。<secret>
をプレーンテキストのシークレット文字列 (例:qwerty
) に置き換えます。上記のコマンドは、次のタイプの Webhook 応答を JSON 形式で生成し、ビルドがトリガーされます。
{"kind":"Build","apiVersion":"build.openshift.io/v1","metadata":{"name":"test-2","namespace":"jfc","selfLink":"/apis/build.openshift.io/v1/namespaces/jfc/buildconfigs/test-2/instantiate","uid":"a72dd529-edc6-4e1c-898e-7c0dbbea176e","resourceVersion":"846159","creationTimestamp":"2020-10-30T12:29:30Z","labels":{"application":"test","buildconfig":"test","openshift.io/build-config.name":"test","openshift.io/build.start-policy":"Serial"},"annotations":{"openshift.io/build-config.name":"test","openshift.io/build.number":"2"},"ownerReferences":[{"apiVersion":"build.openshift.io/v1","kind":"BuildConfig","name":"test","uid":"1f78fa3f-2f3b-421b-9f49-192184cc2280","controller":true}],"managedFields":[{"manager":"openshift-apiserver","operation":"Update","apiVersion":"build.openshift.io/v1","time":"2020-10-30T12:29:30Z","fieldsType":"FieldsV1","fieldsV1":{"f:metadata":{"f:annotations":{".":{},"f:openshift.io/build-config.name":{},"f:openshift.io/build.number":{}},"f:labels":{".":{},"f:application":{},"f:buildconfig":{},"f:openshift.io/build-config.name":{},"f:openshift.io/build.start-policy":{}},"f:ownerReferences":{".":{},"k:{\"uid\":\"1f78fa3f-2f3b-421b-9f49-192184cc2280\"}":{".":{},"f:apiVersion":{},"f:controller":{},"f:kind":{},"f:name":{},"f:uid":{}}}},"f:spec":{"f:output":{"f:to":{".":{},"f:kind":{},"f:name":{}}},"f:serviceAccount":{},"f:source":{"f:contextDir":{},"f:git":{".":{},"f:ref":{},"f:uri":{}},"f:type":{}},"f:strategy":{"f:sourceStrategy":{".":{},"f:env":{},"f:forcePull":{},"f:from":{".":{},"f:kind":{},"f:name":{}},"f:pullSecret":{".":{},"f:name":{}}},"f:type":{}},"f:triggeredBy":{}},"f:status":{"f:conditions":{".":{},"k:{\"type\":\"New\"}":{".":{},"f:lastTransitionTime":{},"f:lastUpdateTime":{},"f:status":{},"f:type":{}}},"f:config":{".":{},"f:kind":{},"f:name":{},"f:namespace":{}},"f:phase":{}}}}]},"spec":{"serviceAccount":"builder","source":{"type":"Git","git":{"uri":"https://github.com/jfclere/demo-webapp.git","ref":"master"},"contextDir":"/"},"strategy":{"type":"Source","sourceStrategy":{"from":{"kind":"DockerImage","name":"image-registry.openshift-image-registry.svc:5000/jfc/jboss-webserver54-tomcat9-openshift@sha256:75dcdf81011e113b8c8d0a40af32dc705851243baa13b68352706154174319e7"},"pullSecret":{"name":"builder-dockercfg-rvbh8"},"env":[{"name":"MAVEN_MIRROR_URL"},{"name":"ARTIFACT_DIR"}],"forcePull":true}},"output":{"to":{"kind":"ImageStreamTag","name":"test:latest"}},"resources":{},"postCommit":{},"nodeSelector":null,"triggeredBy":[{"message":"Generic WebHook","genericWebHook":{"secret":"\u003csecret\u003e"}}]},"status":{"phase":"New","config":{"kind":"BuildConfig","namespace":"jfc","name":"test"},"output":{},"conditions":[{"type":"New","status":"True","lastUpdateTime":"2020-10-30T12:29:30Z","lastTransitionTime":"2020-10-30T12:29:30Z"}]}} { "kind": "Status", "apiVersion": "v1", "metadata": {}, "status": "Success", "message": "invalid Content-Type on payload, ignoring payload and continuing with build", "code": 200 }
注記User "system:anonymous" cannot create resource error
が表示される場合は、認証されていないユーザーをsystem:webhook
ロールバインディングに追加するか、トークンを作成してcurl
コマンドを実行することで、このエラーを解決できます。たとえば、トークンを作成して
curl
コマンドを実行するには、次のようにします。TOKEN=`oc create token builder` curl -H "Authorization: Bearer $TOKEN" -k -X POST https://<host>:<port>/apis/build.openshift.io/v1/namespaces/<namespace>/buildconfigs/<name>/webhooks/<secret>/generic
GitHub で Webhook を使用する場合は以下を実行します。
- GitHub プロジェクトで、Settings > Webhooks > Add webhook を選択します。
- Payload URL フィールドに URL を追加します。
-
コンテンツタイプを
application/json
に設定します。 - 必要に応じて SSL 検証を無効にします。
- Add webhook をクリックします。
詳細は、https://docs.openshift.com/container-platform/4.6/builds/triggering-builds-build-hooks.html を参照してください。