爲集成了Travis CI的GitHub項目配置Slack信息推送

文章目錄

Travis CI默認通過郵件發送構建信息,Travis CI也支持通過其它方式發送信息,如Slack。本文記錄如何已集成Travis CIGitHub項目配置Slack信息推送。

本文以個人Blog AxdLog 爲操作示例

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

官方文檔

以下爲Travis CI官方文檔

Slack

此處不涉及Slack的帳號註冊,點擊鏈接 https://slack.com/signin 進入登錄頁面。

輸入用戶名、密碼

創建頻道

點擊窗口左側的Channel或其後的+創建新的channel(頻道)

此處命名爲github_axdlog並設置爲私有

新創建的頻道

生成 Token

點擊鏈接 https://my.slack.com/services/new/travis 進入設置Travis CI的頁面,在下拉列表框中選擇目標頻道,此處爲github_axdlog

生成的token爲maxdsre:ne18Xpc5RbzM1hIdd34nN2aE

頁面其它信息

頁面Encrypting your credentials部分有一段話

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

憑證加密

按照說明文檔並未能成功安裝travis,後通過Docker容器實現token的加密操作。Docker的介紹、安裝和配置,可參考本人Blog 在GNU/Linux中安裝Docker CE(社區版)全記錄

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

容器啓動後,執行如下命令

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

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

操作過程

 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]:/#

注意報錯信息

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

根據官方文檔 Configuring Build Notifications 在文件.travis.yml中配置notifications指令(文件存放在項目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.

消息模板

默認消息模板爲

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}"

此處進行自定義,最終的格式如下

 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}"

測試

修改.travis.yml後,將修改後代碼提交到GitHub,測試配置是否成果。

Travis CI 構建信息

推送信息測試

構建失敗信息

構建成功信息

更新日誌

  • 2018.04.18 16:07 Tue America/Boston
    • 初稿完成
顯示 Disqus 評論