Carpe Diem

備忘録

CircleCIの新UIでのslack連携 (Webhook URL版)

追記

現在(slack orb v4)は新しい連携方法に変わっています↓

christina04.hatenablog.com

以下(slack orb v3)は古い連携方法です。

概要

旧UIでは以下のような設定画面でslack連携を行っていましたが

f:id:quoll00:20200924011339p:plain

新UIからはこの設定方法はサポートされなくなり、新しくSlack Orbを使った設定方法に変わりました。

環境

  • CircleCI 2.1
  • circleci/slack 3.4.2

設定方法

新UIでのslack連携は以下の2ステップです。

  1. 環境変数の設定
  2. Orbの設定

環境変数の設定

Slack Integrationのところから設定できます。

f:id:quoll00:20200924011737p:plain

Webhook URLを入力します。

f:id:quoll00:20200924011726p:plain

こうするとSlack Orbで使う環境変数が設定されます。

f:id:quoll00:20200924011935p:plain

Orbの設定

Slack Orbには現状1つのJobと4つのCommandがあります。

この中でよく使うのはjobの成功、失敗を通知するstatusになるかと思います。

以下のように設定します。

version: 2.1

orbs:
  slack: circleci/slack@3.4.2

jobs:
  build:
    docker:
      - image: cimg/base:stable
    steps:
      - run:
          name: Build
          command: make build
      - slack/status

こうすると失敗時には f:id:quoll00:20200924014825p:plain

成功時には f:id:quoll00:20200924014757p:plain

といった通知が飛びます。

ただし注意としてstatusはcommandなのでworkflow毎に設定できず、job毎に設定する必要があります。

Tips

以下Tipsです。

失敗時だけ通知したい

先程の設定だと成功時も通知されるため、workflowで以下のようなpipelineを組むと各jobで通知が飛んでうるさいです。

f:id:quoll00:20200924015413p:plain

なのでfail_only: trueで失敗時のみ通知する形にすると毎回飛ぶことを防げます。

version: 2.1

orbs:
  slack: circleci/slack@3.4.2

executors:
  default:
    docker:
      - image: cimg/base:stable

jobs:
  build:
    executor: default
    steps:
      - run:
          name: Build
          command: make build
      - slack/status:
          fail_only: true

  test:
    executor: default
    steps:
      - run:
          name: Test
          command: make test
      - slack/status:
          fail_only: true

workflows:
  version: 2
  build_and_test:
    jobs:
      - build:
          filters:
            branches:
              ignore: master
      - test:
          requires:
            - build

でもworkflowの最後には成功も通知したい

愚直に最後のjobのときはfail_only: trueを付けないようにします。公式もこのやり方だと説明しています。

公式ではpost-stepsを活用することでjobでなくworkflowで管理する方法を紹介しています。

jobs:
  build:
    executor: default
    steps:
      - run:
          name: Build
          command: make build

  test:
    executor: default
    steps:
      - run:
          name: Test
          command: make test

workflows:
  version: 2
  build_and_test:
    jobs:
      - build:
          filters:
            branches:
              ignore: master
          post-steps:
            - slack/status:
                fail_only: true
      - test:
          requires:
            - build
          post-steps:
            - slack/status

並列なpipelineの場合

また直列でなく並列なpipelineの場合は、通知用のジョブを作って並行部分をrequireする必要があります。
かなり面倒ですね。

f:id:quoll00:20200924021417p:plain

jobs:
  build:
    executor: default
    steps:
      - run:
          name: Build
          command: make build

  test:
    executor: default
    steps:
      - run:
          name: Test
          command: make test

  coverage:
    executor: default
    steps:
      - run:
          name: Coverage
          command: make cover

  integration_test:
    executor: default
    steps:
      - run:
          name: Integration Test
          command: make test-integration

  notify_workflow_end:
    executor: default
    steps:
      - slack/status:
          success_message: ':tada: A PR build has succeeded!'

workflows:
  version: 2
  build_and_test:
    jobs:
      - build:
          filters:
            branches:
              ignore: master
          post-steps:
            - slack/status:
                fail_only: true
      - test:
          requires:
            - build
          post-steps:
            - slack/status:
                fail_only: true
      - coverage:
          requires:
            - build
          post-steps:
            - slack/status:
                fail_only: true
      - integration_test:
          requires:
            - build
          post-steps:
            - slack/status:
                fail_only: true
      - notify_workflow_end:
          requires:
            - test
            - coverage
            - integration_test

何回も同じことを書くのを避けたい

jobが増えてくると同じような記述も増えるのでできるだけスリムにしたいですね。2通りあります。

commandsを使う

CirlceCI 2.1のcommandsを使う方法です。

commands:
  notify_failure:
    steps:
      - slack/status:
          fail_only: true

workflows:
  version: 2
  build_and_test:
    jobs:
      - build:
          filters:
            branches:
              ignore: master
          post-steps:
            - notify_failure
      - test:
          requires:
            - build
          post-steps:
            - notify_failure
      - coverage:
          requires:
            - build
          post-steps:
            - notify_failure
      - integration_test:
          requires:
            - build
          post-steps:
            - notify_failure
      - notify_workflow_end:
          requires:
            - test
            - coverage
            - integration_test

Yamlのアンカーとマージを使う

もう1つはYamlのアンカーとマージ機能を使う方法です。

notify_failure: &notify_failure
  post-steps:
    - slack/status:
        fail_only: true

workflows:
  version: 2
  build_and_test:
    jobs:
      - build:
          filters:
            branches:
              ignore: master
          <<: *notify_failure
      - test:
          requires:
            - build
          <<: *notify_failure
      - coverage:
          requires:
            - build
          <<: *notify_failure
      - integration_test:
          requires:
            - build
          <<: *notify_failure
      - notify_workflow_end:
          requires:
            - test
            - coverage
            - integration_test

まとめ

  • Orbを使うことでプラガブルになった
  • コードベースになって設定が統一されやすくなった

のは良い一方、以前よりユーザ側の設定が増えて使いづらくなった印象がありますね。
今後のDXの改善に期待したいです。