はじめに
開発をしているとコミット毎でなく、特定の条件のときにアプリを開発・本番環境に自動デプロイしたい、ということがあると思います。
本記事では、Gitでv.*とtagが付けられた時のみ、Circle CIでアプリをデプロイ・さらに結果をSlack通知する方法を記載します。
環境・処理の流れ
環境
・ソースコード管理: GitHub
・Deploy対象のアプリ:Goのサンプルアプリ
・Deploy先:AWS Elastic Beanstalk
処理の流れ
- tag付けしたcommitをPush
- Circle CIでアプリをArchive
- AWS Elastic BeanstalkへDeploy (特定のtag付与時)
- 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
 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に以下のコマンドを追加
  - 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の全体設定は以下の通り。
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: /.*/
その他参考にしたサイト
https://qiita.com/fushikky/items/c7bd05f897eab58ea7e4
https://blog.sshn.me/posts/circleci-when-attribute/
https://qiita.com/oohira/items/09aa91cc01109f8d053b

