DEV Community

loading...
Cover image for RocketChat notifications on your CI/CD pipeline

RocketChat notifications on your CI/CD pipeline

jadolg profile image Jorge Alberto Díaz Orozco (Akiel) Updated on ・3 min read

Hey there :)
I'm a big fan of Rocket.Chat. Such a big fan that I even wrote my own REST API wrapper to use it with Python and convinced everyone in my company to migrate from Slack. And since I work in a software house we soon started using it for many things. We even have a bot to set up reminders in the organization :)
One thing we love about the work we do is continuous integration. We are a very quality focused team and we like everything to be automated. I want to do git push and everything should automagically start building, testing, measuring and deploying and I, of course, want to have notifications on my awesome chat application so my team is also aware of what's going on without even opening our CI tools.
I know there are plenty of CI tools, but one of the more popular in my team is gitlab-ci. I personally switched from jenkins because I loved the way gitlab works all with Docker out of the box and I find the sintax more clear and beautiful, therefore I will write my examples using gitlab-ci notation.
So, you can technically push notifications to RocketChat using only curl, but it will probably be verbose. First, you would have to login into your chat server and save the tokens and then post the message you want, all using, for instance, curl.
According to the REST API documentation, you would log in like this

curl http://localhost:3000/api/v1/login \
     -d "username=myusername&password=mypassword"

which will result in a response like this:

{
  "status": "success",
  "data": {
      "authToken": "9HqLlyZOugoStsXCUfD_0YdwnNnunAJF8V47U3QHXSq",
      "userId": "aobEdbYhXfu5hkeqG",
      "me": {
            "_id": "aYjNnig8BEAWeQzMh",
            "name": "Rocket Cat",
            "emails": [
                {
                  "address": "rocket.cat@rocket.chat",
                  "verified": false
                }
            ],
            "status": "offline",
            "statusConnection": "offline",
            "username": "rocket.cat",
            "utcOffset": -3,
            "active": true,
            "roles": [
                "admin"
            ],
            "settings": {
                "preferences": {}
              }
        }
   }
}

and you would need to save the values of authToken and userId to be used to post the message.
Posting the message after this using curl is also not difficult:

curl -H "X-Auth-Token: 9HqLlyZOugoStsXCUfD_0YdwnNnunAJF8V47U3QHXSq" \
     -H "X-User-Id: aobEdbYhXfu5hkeqG" \
     -H "Content-type:application/json" \
     http://localhost:3000/api/v1/chat.postMessage \
     -d '{ "channel": "#general", "text": "This is a test!" }'

It should be very easy to make a script with this, but since I wanted to practice some GO I decided to make this application and since I love the way GO just works almost everywhere it seemed like a good idea and it was.
So the idea now is a little bit more simple:
You just have to download the released application (or compile it yourself) and executed as shown in the help.

wget https://github.com/aleph-engineering/rocketchat-notification/releases/download/1.4.1/rocketchat-notification -P /usr/bin/ && chmod +x /usr/bin/rocketchat-notification

and

rocketchat-notification -c general -u user -p ${ROCKET_PASSWORD} -s https://myserver.rocket.chat -m "hello from terminal"

Pretty simple right?
Now let's use it in a sentence :)
On my gitlab-ci (.gitlab-ci.yml on your files) I would do something like this:
First, let's put the password of the user we want to use as an environment variable on gitlab so it is not persisted on our files improving our security:
This is done on our project's settings under CI/CD
gitlab_CI
And let us write in our .gitlab-ci.yml something like this:

stages:
- test

image: docker:stable-dind

test_nitofication:
  stage: test
  before_script:
  - wget https://github.com/aleph-engineering/rocketchat-notification/releases/download/1.4.1/rocketchat-notification -P /usr/bin/ && chmod +x /usr/bin/rocketchat-notification
  script:
  - rocketchat-notification -c general -u gitlab -p ${ROCKET_PASSWORD} -s https://yourserver.rocket.chat -m "hello from CI"

Commit and push your changes and wait for the notification :)
notification
Do you want to send the output of a command to your chat? No problem. You can always run rocketchat-notification to read from standard input like this:

docker ps | rocketchat-notification -c general -u gitlab -p ${ROCKET_PASSWORD} -s https://yourserver.rocket.chat

and using the -code true flag your text will appear in a codebox in the chat.
And that's it. I hope you find this interesting and practical and have fun with your Continuous Integration.

Discussion (0)

pic
Editor guide