CircleCI notifications in RocketChat

There is no official CircleCI integration for RocketChat, therefore this has to be done a little manually.

Below is an example of how I use it.

.circleci/config.yml

version: 2
jobs:
  build-development:
    docker:
      - image: debian:stretch
    steps:
        - checkout
        - run: 
            command: apt-get update && apt-get install -y curl
        - run:
            name: Greeting from hello
            command: echo "Hello, world from hello."
        - run:
            name: Notifiation Failed
            command: bash .circleci/notify "failed"
            when: on_fail
        - run:
            name: Notification Sucess
            command: bash ./circleci/notify "success"
            when: on_success
        
workflows:
  version: 2
  build-deploy:
    jobs:
      - build-development
          filters:
            branches:
              only:
                - develop

Note that on_fail will only execute if one of the above will fail, same with on_success, it only executes if all of the above are successful.

.circleci/notify


#!/bin/bash
set -euo pipefail

payload=$(
cat <<EOM
{
    "status": "$1",
    "job": "$CIRCLE_JOB",
    "build_num": "$CIRCLE_BUILD_NUM",
    "project_reponame": "$CIRCLE_PROJECT_REPONAME",
    "branch": "$CIRCLE_BRANCH",
    "build_url": "$CIRCLE_BUILD_URL",
    "compare_url": "$CIRCLE_COMPARE_URL",
    "sha1": "$CIRCLE_SHA1"
}
EOM
)

curl -X POST -H 'Content-Type: application/json' --data "$payload" https://YOUR-ROCKETCHAT/hooks/INCOMING-WEBHOOK-INTEGRATION-URL

RocketChat Incoming WebHook Integration

class Script {
  
  process_incoming_request({ request }) {
     
    var alertColor = "warning";
    
    let status = request.content.status;
    let job = request.content.job;
    let build_num = request.content.build_num;
    let project_reponame = request.content.project_reponame;
    let branch = request.content.branch;
    let build_url = request.content.build_url;
    let sha1 = request.content.sha1;
      
    if (status == "success") {
    	alertColor = "good";
    } else if (status == "failed") {
    	alertColor = "danger";
    }

    let textMessage = (status == "failed" ? "@all\n" : "");
        
    let title = status == "failed" ? " build failed!" : " was built successfully!"
    
    textMessage = textMessage + "*Build no*: " + build_num + "\n*Project*: " + project_reponame + "\n*Branch*: " + branch + "\n*Commit*: " + sha1

    return {
    	content: {
	    username: "CircleCI",
            attachments: [{
		text: textMessage,
                color: alertColor,
                title: job + title,
                title_link: build_url
            }]
        }
    };

    return {
    	error: {
        	success: false
        }
    };
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s