2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Incoming webhookを使ってCircleCIのSlackへの通知をJob別に分ける

Last updated at Posted at 2018-09-06

CircleCI2.0では、Slackへの通知はWorkflow別に設定できるが、Job別には設定できない
※ CircleCIはGithubと連携していることを前提にしています。

incoming-webhookを使ってJobの正常終了を通知

Slackでincoming wobhookを設定

すごくわかりやすいのでこちらを参考にしてください

Githubのブランチ直下にincoming-webhook.shを作成

例)
#!/bin/bash

COMMIT_MESSAGE=$(git log --format=%B -n 1 ${CIRCLE_SHA1})
HOOK_URL="<incoming webhookのHook URL>"

if [ ${CIRCLE_JOB} = '<CircleCIのジョブ名>' ];then
  curl -X POST --data-urlencode \
    'payload={
      "channel": "<slackのチャンネル名>",
      "username": "<slack通知で使うユーザ名>",
      "attachments": [
        {
          "color": "#36a64f",
          "text": "Success:  '${CIRCLE_USERNAME}' '${CIRCLE_JOB}' (#<'${CIRCLE_BUILD_URL}'|'${CIRCLE_BUILD_NUM}'>) in (<'${CIRCLE_COMPARE_URL}'|'${CIRCLE_BRANCH}'>)\n - '"${COMMIT_MESSAGE}"' "
        }
      ],
      "icon_emoji": "<slackのアイコン名>"
    }' \
  ${HOOK_URL}
fi

権限を設定

  • chmod a+x ./incoming-webhook.sh

通知したいJobの最後に以下を追加

  • - run: ./incoming-webhook.sh

Jobが失敗した場合は、今まで通りすべてを通知する

  • https://circleci.com/gh/<ユーザ名もしくは組織名>/<ブランチ名>/edit#hooksで設定
    • Slack -> Fixed/Failed Only をチェック
    • Webhook URLにSlackで設定したHook URLを設定
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?