Manual Approval and Telegram Notifications in Jenkins
Tags: tutorial, cloud, devops, jenkins
Published: 2024-03-11 01:43:08


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m39s07p2q1ihbha75flz.png)

## Why use a manual approval step in the deploy code?
>Manual approval in Jenkins provides a mechanism to ensure quality, security and compliance in software development and deployment processes.

**Manual approval**
- Add this stage to the pipeline

```bash
 stage('Approval') {
        steps {
            timeout(time: 15, unit: "MINUTES") {
	                    input message: 'Do you want to approve the deployment?', ok: 'Yes'
	                }
			
	                echo "Initiating deployment"
        }
    } 
```
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x0awadb72xwbig89urew.png)

---

## Telegram Notifications
>_To send notifications we need two things_
**First**: HTTP_API of bot
**Second**: CHAT_ID

**_HTTP_API of bot_**
- Create telegram bot
    1. BotFather in Telegram (@BotFather)
    2. Send /newbot command
    3. Enter bot name and bot username

      <u>token-http-api</u>

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b3berv927qnf987ggm45.png)

**_CHAT_ID_**
    1. Go to @myidbot 
    2. Enter: /start -> /getid
    3. Save your chat_ID

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9qugr3zi80agdi7k9c9j.png)

---
 
## Creating Jenkins Global Credentials
 1. **Manage Jenkins** -> **Credentials** -> **System** -> **Global credentials (unrestricted)**
 2. Add Crediantials(CHAT_ID & TELEGRAM_API)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ih5pvksqgy6o2thho82j.png)

Configurig pipeline
>_At the top of the [pipeline](https://github.com/Sharker3312/Node-App/blob/master/Jenkinsfile)

```bash
environment {
        // Telegram configuration
        TOKEN = credentials('telegram-api')
        CHAT_ID = credentials('telegram_chatid')
    }
```
>_Declare two variables which contain the tokens_


```bash
 post {
        success {
            script {
                sh "curl -X POST -H 'Content-Type: application/json' -d '{\"chat_id\": \"${CHAT_ID}\", \"text\": \"${JOB_NAME}: #${BUILD_NUMBER}\n✅ Deploy succeeded! \", \"disable_notification\": false}' \"https://api.telegram.org/bot${TOKEN}/sendMessage\""
            }
        }
        failure {
            script {
                sh "curl -X POST -H 'Content-Type: application/json' -d '{\"chat_id\": \"${CHAT_ID}\", \"text\": \"${JOB_NAME}: #${BUILD_NUMBER}\n❌Deploy failure!\", \"disable_notification\": false}' \"https://api.telegram.org/bot${TOKEN}/sendMessage\""
            }
        }
   
       aborted {
            script {
                sh "curl -X POST -H 'Content-Type: application/json' -d '{\"chat_id\": \"${CHAT_ID}\", \"text\": \"${JOB_NAME}: #${BUILD_NUMBER}\n❌Deploy aborted!\", \"disable_notification\": false}' \"https://api.telegram.org/bot${TOKEN}/sendMessage\""
            }
        }    
    
    }
```

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbdku9p3yglchktuwwcy.png)

I hope helpful!!  

---
<a href="https://linkedin.com/in/lesterdprez" rel="nofollow"><img src="https://camo.githubusercontent.com/d94940866c98cb4fca5783c4e8ac95776d2f52df6bbf3d5ab9e30d76836f30ae/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c696e6b6564496e2d2532333030373742352e7376673f6c6f676f3d6c696e6b6564696e266c6f676f436f6c6f723d7768697465" alt="LinkedIn" data-canonical-src="https://img.shields.io/badge/LinkedIn-%230077B5.svg?logo=linkedin&amp;logoColor=white" style="max-width: 100%;"></a>