Setting Up Slack Build Notification in Travis CI for Github Project

Table of Contents

By default, Travis CI uses email to send build notification, it also support other method, e.g. Slack. This article documents how to set up Slack notification for GitHub project integrated Travis CI.

I take my personal blog AxdLog as operation example.

item details
repo MaxdSre/maxdsre.github.io
channel github_axdlog

Official Document

Travis CI official documents

Slack

This step doesn’t include Slack account registration. Clicking link https://slack.com/signin to login in.

Inputing user name, password

Create New Channel

Clicking Channel or + on the left of the page to create a new channel

Here I set name as github_axdlog and choose private

newly created channel

Token Generation

Clicking link https://my.slack.com/services/new/travis into Travis configuration page, choose target channel (github_axdlog) in the selection menu

Newly generated token is maxdsre:ne18Xpc5RbzM1hIdd34nN2aE

Encrypting your credentials

Your integration token is semi-secret, but we recommend encrypting your credentials using the Travis command line client.

Encrypting Credentials

After I failed to install travis according to official docs, I choose Docker image to generate encrypted credential.

If you don’t know how to install Docker, you may consider reference my blog Installing And Configuring Docker Community Edition(CE) On GNU/Linux.

1
2
docker pull ruby
docker run -ti --rm ruby bash

Executing the following commands in container

1
2
3
4
5
6
# install travis
gem install travis

touch .travis.yml
travis encrypt "maxdsre:ne18Xpc5RbzM1hIdd34nN2aE" --add notifications.slack.rooms -r MaxdSre/maxdsre.github.io
cat .travis.yml

details

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
[email protected]:/# travis --version
Shell completion not installed. Would you like to install it now? |y| y
1.8.8
[email protected]:/# travis encrypt "maxdsre:ne18Xpc5RbzM1hIdd34nN2aE" --add notifications.slack.rooms
Can't figure out GitHub repo name. Ensure you're in the repo directory, or specify the repo name via the -r option (e.g. travis <command> -r <owner>/<repo>)
[email protected]:/# travis encrypt "maxdsre:ne18Xpc5RbzM1hIdd34nN2aE" --add notifications.slack.rooms -r MaxdSre/maxdsre.github.io
no .travis.yml found
[email protected]:/# touch .travis.yml
[email protected]:/# travis encrypt "maxdsre:ne18Xpc5RbzM1hIdd34nN2aE" --add notifications.slack.rooms -r MaxdSre/maxdsre.github.io
[email protected]:/# cat .travis.yml
notifications:
  slack:
    rooms:
      secure: o5TEEslShhI5AGKaVMbV5bPT7Hkh/lQkqd2T76fXfgXVGpxGWY+rQmijyh4hm7APKzOwxw64J/L7rTVKlW14QlCXe4ff8+diooG365g6j9h8esuXpJhRDtP72SrPHkAOwo8loX72DR7uazRUYSs0AOGwmYHyctLnhNOmWPVkp3ISAK9unEOIZ0UJ7381rZVWTiVReAVrbpe5jlX3nSbBdineKVar+511BHO+eKTU28AlCFgUSG3isvV1dUhZZfHHUaOx3ApzZAmx7GYDBIIqcfZ6r5x1PmZIDjnH3v4TvxRhUpAp6L5IGbpqhAps5dg73nZfzJz4+bM4ruMRBo9PDfgVbA+9zMpN8/yBL6E/OS2u045TJ491n8PE/+GoajMCzijuoKgHjTzWyVhIV9dEmVOpjOZL9rv6R1mqKoUwbV8rs8WjqciBlfoC6WwZOAWTcXok5RIMmSQ94oBZEtGSmTtALS5D0D7uCuHJt2Nz+GhrKbuwM/eSRYUwyWvNJdqSTbFnEJp27AhP5hxHrzg7uEv9cablPNf1NTFquZJH/ROUf0nKqsVZOJMQElRdcr37Es4tSg6WTKYs4XIeh1R9Yjws53KLOv2phTvFzhGPmycgPrADhHVnGoWtePyNCwASc+x7HQJQoUM6Y2i9AVruHN3XmsXq7dSIu3srqPy2lRw=
[email protected]:/#

Error info

GitHub repo name

Can’t figure out GitHub repo name. Ensure you’re in the repo directory, or specify the repo name via the -r option (e.g. travis -r /)

.travis.yml

no .travis.yml found

token maxdsre:ne18Xpc5RbzM1hIdd34nN2aEtravis加密後生成的字符串爲

1
o5TEEslShhI5AGKaVMbV5bPT7Hkh/lQkqd2T76fXfgXVGpxGWY+rQmijyh4hm7APKzOwxw64J/L7rTVKlW14QlCXe4ff8+diooG365g6j9h8esuXpJhRDtP72SrPHkAOwo8loX72DR7uazRUYSs0AOGwmYHyctLnhNOmWPVkp3ISAK9unEOIZ0UJ7381rZVWTiVReAVrbpe5jlX3nSbBdineKVar+511BHO+eKTU28AlCFgUSG3isvV1dUhZZfHHUaOx3ApzZAmx7GYDBIIqcfZ6r5x1PmZIDjnH3v4TvxRhUpAp6L5IGbpqhAps5dg73nZfzJz4+bM4ruMRBo9PDfgVbA+9zMpN8/yBL6E/OS2u045TJ491n8PE/+GoajMCzijuoKgHjTzWyVhIV9dEmVOpjOZL9rv6R1mqKoUwbV8rs8WjqciBlfoC6WwZOAWTcXok5RIMmSQ94oBZEtGSmTtALS5D0D7uCuHJt2Nz+GhrKbuwM/eSRYUwyWvNJdqSTbFnEJp27AhP5hxHrzg7uEv9cablPNf1NTFquZJH/ROUf0nKqsVZOJMQElRdcr37Es4tSg6WTKYs4XIeh1R9Yjws53KLOv2phTvFzhGPmycgPrADhHVnGoWtePyNCwASc+x7HQJQoUM6Y2i9AVruHN3XmsXq7dSIu3srqPy2lRw=

.travis.yml

Refering to official document Configuring Build Notifications, configuring directive notifications in file .travis.yml which is saved in the root dir of project MaxdSre/maxdsre.github.io.

1
2
3
4
5
6
7
notifications:
  email: false    # default notification method
  slack:
    rooms:
      - secure: o5TEEslShhI5AGKaVMbV5bPT7Hkh/lQkqd2T76fXfgXVGpxGWY+rQmijyh4hm7APKzOwxw64J/L7rTVKlW14QlCXe4ff8+diooG365g6j9h8esuXpJhRDtP72SrPHkAOwo8loX72DR7uazRUYSs0AOGwmYHyctLnhNOmWPVkp3ISAK9unEOIZ0UJ7381rZVWTiVReAVrbpe5jlX3nSbBdineKVar+511BHO+eKTU28AlCFgUSG3isvV1dUhZZfHHUaOx3ApzZAmx7GYDBIIqcfZ6r5x1PmZIDjnH3v4TvxRhUpAp6L5IGbpqhAps5dg73nZfzJz4+bM4ruMRBo9PDfgVbA+9zMpN8/yBL6E/OS2u045TJ491n8PE/+GoajMCzijuoKgHjTzWyVhIV9dEmVOpjOZL9rv6R1mqKoUwbV8rs8WjqciBlfoC6WwZOAWTcXok5RIMmSQ94oBZEtGSmTtALS5D0D7uCuHJt2Nz+GhrKbuwM/eSRYUwyWvNJdqSTbFnEJp27AhP5hxHrzg7uEv9cablPNf1NTFquZJH/ROUf0nKqsVZOJMQElRdcr37Es4tSg6WTKYs4XIeh1R9Yjws53KLOv2phTvFzhGPmycgPrADhHVnGoWtePyNCwASc+x7HQJQoUM6Y2i9AVruHN3XmsXq7dSIu3srqPy2lRw=
    on_success: always  # change: send a notification when the build status changes.
    on_failure: always  # always: always send a notification.

Template

Default message template

1
2
3
4
5
# push
- "Build <%{build_url}|#%{build_number}> (<%{compare_url}|%{commit}>) of %{repository_slug}@%{branch} by %{author} %{result} in %{duration}"

# pull
- "Build <%{build_url}|#%{build_number}> (<%{compare_url}|%{commit}>) of %{repository_slug}@%{branch} in PR <%{pull_request_url}|#%{pull_request_number}> by %{author} %{result} in %{duration}"

custom template, the final format

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
notifications:
  email: false    # default notification method
  slack:
    rooms:
      - secure: BwlcaZ7keVQyKFxqT5DMKXQAhT9SY+w4WzDh4Y1kVFHa6LvuALpSIElfeVwCWnIbEjDgLCoZBfcYPqEuhmIG2K4TF25bNZYNrkDIulN51cEgvfT6gW2vt+0VLulnQPtGpz0hLV0MNNgegG3znT8Nz6/F31iGpKUykZsZZXX9cUkycQESxIKn9Gu66Sggv/WAjQ24dmxZp5f9yhYY06hteCqycmYglplukJCB0j4vMn9oSzFGQizKv8tQeiKzPotzpoj26pSC/HU3C+YEz0FiZVY9eXcdq2hs2zPoHW/PAZ9kL9tj5YatN/IDSP/i54CRdlm/ZfUeZxQn1vtFlV1XiWp2iYXJ5C8m6TIX9hptvf3EEQWHwztJ0qv3RFx8bTZAyJBssLcDYp3sjfYeJQ79cefMP4kQpaqO4xk7oHrSTQx8kw7l9atnymbNLzFgR74WEZFUwOrPtLHq1V2fax/pReAqNTZyIuxak9Spa7buTiQJ6NifnkrQA6/rVgeAQBZXmgQkc8uJZlLibRXGc+pvdLb+cqU/zOuM+k6JSSUwagRvXbUjJIbB9IO64CgcuDpnjAv58R0nY7fAhU8b+2PVxmRUhFom2OZ1NLOdLH3yrBfrthxmtcPMZJemGm7rLYAVYbKhtZYpyfddzumkOgzv1zSo4srN1x6lFhGxsSO7UxI=
    on_success: always  # change: send a notification when the build status changes.
    on_failure: always  # always: always send a notification.
    template:
      - "Repo `%{repository_slug}` *%{result}* build (<%{build_url}|#%{build_number}>) for commit (<%{compare_url}|%{commit}>) on branch `%{branch}`."
      - "Execution time: *%{duration}*"
      - "Message: %{message}"

Testing

Committing codes to GitHub after file .travis.yml is modified.

Travis CI build info

Slack notification

Build fail notification

Build success notification

Change Log

  • 2018.04.18 16:07 Tue America/Boston
    • first draft
Show Disqus Comments