LoginSignup
5
3

More than 3 years have passed since last update.

Gitで特定のtagが付けられた時のみ Circle CIでdeploy・slack通知する方法

Posted at

はじめに

開発をしているとコミット毎でなく、特定の条件のときにアプリを開発・本番環境に自動デプロイしたい、ということがあると思います。
本記事では、Gitでv.*とtagが付けられた時のみ、Circle CIでアプリをデプロイ・さらに結果をSlack通知する方法を記載します。

環境・処理の流れ

環境

・ソースコード管理: GitHub
・Deploy対象のアプリ:Goのサンプルアプリ
・Deploy先:AWS Elastic Beanstalk

処理の流れ

  1. tag付けしたcommitをPush
  2. Circle CIでアプリをArchive
  3. AWS Elastic BeanstalkへDeploy (特定のtag付与時)
  4. SlackでのDeploy結果通知 (特定のtag付与時)

コミット毎にデプロイする方法は前記事にまとめてあります。

設定方法

特定のtag付与時のCircle CIの実行

v.*とtagが付けられたコミットのみCircle CIを実行する場合は、以下のfilters: tags: only:の設定を入れます。
※Circle CIの仕様上、requireされているジョブ(例では archive job)にもfilters: tagsの設定が必要になるようです。
以下、公式ドキュメント抜粋
if a job requires any other jobs (directly or indirectly), you must use regular expressions to specify tag filters for those jobs

config.yml(抜粋)
 jobs:
      - archive:
         filters:
          tags:
            only: /v.*/
      - deploy:
          requires:
            - archive
          filters:
           tags:
             only: /v.*/
           branches:
             ignore: /.*/

Deploy結果のSlack通知

1.Slackのwebhookの設定ページから通知したいチャネルを選択し、Webhook URLをコピーします。
2.Circle CIの対象プロジェクトのBUILD SETTING -> Enviroment Variablesにて以下の項目を設定
Name: $SLACK_WEBHOOK_URL
Value: [上記でコピーしたWebhook URL]
3.Circle CIのconfig.ymlに以下のコマンドを追加

config.yml(抜粋)
  - run:
          name: Notification to Slack
          command:  |
            curl -X POST \
            $SLACK_WEBHOOK_URL \
            -H 'content-type: application/json' \
            -d '{
              "text": "Deploy succeeded!!"
            }'
          when: on_success
      - run:
          name: Notification to Slack
          command:  |
            curl -X POST \
            $SLACK_WEBHOOK_URL \
            -H 'content-type: application/json' \
            -d '{
              "text": "Deploy Failed!!"
            }'
          when: on_fail

上の例ではdeploy成功時と失敗時で通知するメッセージを変えています。

config.ymlの全体設定は以下の通り。

config.yml
version: 2
jobs:
  archive:
    machine: true
    working_directory: ~/project/{{ORG_NAME}}/{{REPO_NAME}}
    steps:
      - checkout

      - run:
          name: Archive
          command: |
            mv ./eb-go-sample/Dockerfile ./eb-go-sample/Dockerfile.local
            zip ./eb-go-sample/go-sample.zip ./eb-go-sample/*

      -  persist_to_workspace:
          root: . # working_directoryからの相対パス
          paths:
            - .   # rootからの相対パス
  deploy:
    machine: true
    working_directory: ~/project/{{ORG_NAME}}/{{REPO_NAME}}
    steps:
      - attach_workspace: # working_directoryからの相対パス
          at: .
      - run:
          name: Deploy to EB
          command: |
            pip install awsebcli --upgrade
            cd ~/project/{{ORG_NAME}}/{{REPO_NAME}}/eb-go-sample/ && eb deploy
      - run:
          name: Notification to Slack
          command:  |
            curl -X POST \
            $SLACK_WEBHOOK_URL \
            -H 'content-type: application/json' \
            -d '{
              "text": "Deploy succeeded!!"
            }'
          when: on_success
      - run:
          name: Notification to Slack
          command:  |
            curl -X POST \
            $SLACK_WEBHOOK_URL \
            -H 'content-type: application/json' \
            -d '{
              "text": "Deploy Failed!!"
            }'
          when: on_fail

workflows:
  version: 2
  archive_and_deploy:
    jobs:
      - archive:
         filters:
          tags:
            only: /v.*/
      - deploy:
          requires:
            - archive
          filters:
           tags:
             only: /v.*/
           branches:
             ignore: /.*/

実行結果
ci0504.png
slack0504.png

その他参考にしたサイト

https://qiita.com/fushikky/items/c7bd05f897eab58ea7e4
https://blog.sshn.me/posts/circleci-when-attribute/
https://qiita.com/oohira/items/09aa91cc01109f8d053b

5
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
5
3