Notify the build result with slack with CircleCI. You can do it in 5 minutes.

Notify slack of CircleCI job results

This article is about some kind of automated testing and how to notify slack of the results of automated deployment. We've put it together in a way that's easy, easy to understand, and quick, so let's get it done and do something different! !!

environment

CircleCI 2.1
orbs: slack: circleci/[email protected]

conditions

  1. You have already created some kind of job.
  2. You can build normally.
  3. I'm using slack

If you meet these conditions, follow the procedure for the first time. If this is not the case, be aware that it can cause confusion when an error occurs!

Process

  1. Get webhook
  2. Set environment variables on CircleCI
  3. Describe the required sources in the config.yml file

1. Get a webhook

https://slack.com/services/new/incoming-webhook

⬆️⬆️ Please get the URL to set in the environment variable from the above URL. After logging in to the specified account and specifying the channel to notify, the URL will be issued. slack.png

When you log in, the screen for selecting a channel will appear in the above picture, so select it. The URL will appear on the next screen, so copy it and make a note of it.

2. Set environment variables in CircleCI

Set environment variables in CircleCI. Register environment variables on the project setting screen Select a project and import circleci.png

name is SLACK_WEBHOOK Value copy and paste the URL you wrote down earlier

3. Set notification parameters in the /.circleci/config.yml file

config.yml



orbs:
  slack: circleci/[email protected]
    

#abridgement

    - slack/status:
        success_message: ':circleci-pass: $CIRCLE_BRANCH build is complete\n:github_octocat: User:$CIRCLE_USERNAME'
        failure_message: ':circleci-fail: $CIRCLE_BRANCH build failed\n:github_octocat: User:$CIRCLE_USERNAME'
        webhook: '${SLACK_WEBHOOK}'

Make these two types of description

My situation

config.yml


version: 2.1

orbs:
  slack: circleci/[email protected]

jobs:
  build:
    docker:
    - image: circleci/ruby:2.6.5-node-browsers
      environment:
        - BUNDLER_VERSION: 2.2.3
        - RAILS_ENV: 'test'
    - image: circleci/mysql:5.7
      environment:
        - MYSQL_ALLOW_EMPTY_PASSWORD: 'true'
        - MYSQL_ROOT_HOST: '127.0.0.1'

    working_directory: **


    steps:
    - checkout

    - restore_cache:
        keys:
        - v1-dependencies-{{ checksum "Gemfile.lock" }}
        - v1-dependencies-

    - run:
        name: install dependencies
        command: |
          gem install bundler -v 2.2.0
          bundle install --jobs=4 --retry=3 --path vendor/bundle

    - save_cache:
        paths:
        - ./vendor/bundle
        key: v1-dependencies-{{ checksum "Gemfile.lock" }}

    # Database setup
    - run: mv ./config/database.yml.ci ./config/database.yml

    # Database setup
    - run:
        name: DatabaseSetup
        command: |
           bundle exec rake db:create
           bundle exec rake db:schema:load

    # yarn install
    - run:
        name: yarn Install
        command: yarn install


    - run: bundle exec bin/webpack

    # run tests!
    - run:
        name:RSpec parallel execution
        command: |
          mkdir /tmp/test-results
          TEST_FILES="$(circleci tests glob " **/spec/**/*_spec.rb" | circleci tests split --split-by=timings)"

          bundle exec rspec --format progress \
                          --out /tmp/test-results/rspec.xml \
                          --format progress \
                          $TEST_FILES

    # collect reports
    - store_test_results:
        path: /tmp/test-results
    - store_artifacts:
        path: /tmp/test-results
        destination: test-results

          #ssh connection
    - add_ssh_keys:
        fingerprints:
          - "**"

      #Deploy
    - deploy:
        name: Capistrano deploy
        command: |
          if [ "${CIRCLE_BRANCH}" != "master" ]; then
            exit 0
          fi
            bundle exec cap production deploy

    - slack/status:
        success_message: ':circleci-pass: $CIRCLE_BRANCH build is complete\n:github_octocat: User:$CIRCLE_USERNAME'
        failure_message: ':circleci-fail: $CIRCLE_BRANCH build failed\n:github_octocat: User:$CIRCLE_USERNAME'
        webhook: '${SLACK_WEBHOOK}'

If you run a job with this and get a notification, it is successful. slack.png

When you receive this notification, you are done. Thank you for your hard work! !!

Recommended Posts

Notify the build result with slack with CircleCI. You can do it in 5 minutes.
You can do it with copy! Aspect-oriented programming (Android)
In Redmine you can get the project with Project.find (<identifier>)
Until you build a project described in scala with Maven and execute it with the scala command.
With Tomcat you can use placeholders ($ {...}) in web.xml
In Ruby you can define a method with any name
If it doesn't change with the disable_with option in Safari