[Day 27] Oops!CI/CD trigger WebHook to Telegram Bot

前言

有時候我們在CI/CD流程進行中時,通常都是被動告知的,那如果想要透過Pipeline主動告知要怎麼做呢?

我們在第六天[Day 6] Oops!Gitea - WebHook有介紹到Gitea搭配WebHook的做法,而如果要在CI/CD上觸發WebHook去通知Telegram要怎麼做呢?

Telegram Bot API

首先讓我們來瞧瞧Telegram Bot API,Telegram Bot提供API可以直接打Telegram Bot API發送訊息到指定的聊天室

https://api.telegram.org/bot${token}/sendMessage?chat_id=${chat_id}&text=${message}

透過 curl

exmaple for drone

kind: pipeline
type: docker
name: linter

steps:
- name: alert_production_ci
  detach: true
  when:
    branch: master
  image: rain123473/alpine-curl:3.11.6
  commands:
  - curl https://api.telegram.org/bot${token}/sendMessage?chat_id=${chat_id}&text=【溫馨提醒】〖ITHOME〗【鐵人賽】 發文動作開始!!

Telegram 跳出

https://ithelp.ithome.com.tw/upload/images/20201005/20115289Y016sLjm4I.png

透過 Drone Plugin

從Drone Plugin可以找到很多套件,可以搭配CI/CD使用,而我從這找到Telegram by appleboy的套件。 https://ithelp.ithome.com.tw/upload/images/20201005/20115289F93CDzjh81.png

CI/CD for Drone with Drone Plugin

exmaple for drone yaml

kind: pipeline
type: docker
name: ithome_telegram_webhook

steps:
- name: ithome_alert
  image: appleboy/drone-telegram:1.3.8
  settings:
    token: 
      from_secret: telegram_token
    to: 
      from_secret: telegram_to
    message: "【溫馨提醒】〖ITHOME〗【鐵人賽】 發文動作開始!!"

Telegram 跳出

https://ithelp.ithome.com.tw/upload/images/20201005/20115289o3sjmAOxyB.png

今日小結

大家可以利用CI/CD的Plugin來幫助你們在CI/CD的流程更加完整,有空也不妨去Plugin網站上逛逛吧~