DEV Community

Cover image for Manual Approval and Telegram Notifications in Jenkins
Lester Diaz Perez
Lester Diaz Perez

Posted on • Updated on

Manual Approval and Telegram Notifications in Jenkins

Image description

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
 stage('Approval') {
        steps {
            timeout(time: 15, unit: "MINUTES") {
                        input message: 'Do you want to approve the deployment?', ok: 'Yes'
                    }

                    echo "Initiating deployment"
        }
    } 
Enter fullscreen mode Exit fullscreen mode

Image description


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

    token-http-api

Image description

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

Image description


Creating Jenkins Global Credentials

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

Image description

Configurig pipeline

_At the top of the pipeline

environment {
        // Telegram configuration
        TOKEN = credentials('telegram-api')
        CHAT_ID = credentials('telegram_chatid')
    }
Enter fullscreen mode Exit fullscreen mode

Declare two variables which contain the tokens

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

    }
Enter fullscreen mode Exit fullscreen mode

Image description

I hope helpful!!


LinkedIn

Top comments (0)