11pipeline {
22 agent any
33
4+ environment {
5+ // Telegram configre
6+ TOKEN = credentials(' telegram_token' )
7+ CHAT_ID = credentials(' telegram_chatid' )
8+
9+ // Telegram message
10+ GIT_MESSAGE = sh(returnStdout : true , script : " git log -n 1 --format=%s ${ GIT_COMMIT} " ). trim()
11+ GIT_AUTHOR = sh(returnStdout : true , script : " git log -n 1 --format=%ae ${ GIT_COMMIT} " ). trim()
12+ GIT_COMMIT_SHORT = sh(returnStdout : true , script : " git rev-parse --short ${ GIT_COMMIT} " ). trim()
13+ GIT_INFO = " Branch: ${ GIT_BRANCH} \n Last Message: ${ GIT_MESSAGE} \n Author: ${ GIT_AUTHOR} \n Commit: ${ GIT_COMMIT_SHORT} "
14+ TEXT_BREAK = ' ----------------------------------------'
15+ TEXT_PRE = " ${ TEXT_BREAK} \n ${ GIT_INFO} "
16+ TEXT_BUILD = " ${ JOB_NAME} is Building"
17+ TEXT_PUSH = " ${ JOB_NAME} is Pushing"
18+ TEXT_CLEAN = " ${ JOB_NAME} is Cleaning"
19+ TEXT_RUN = " ${ JOB_NAME} is Running"
20+
21+ // Telegram parameters
22+ TEXT_SUCCESS_BUILD = " ${ JOB_NAME} is Success"
23+ TEXT_FAILURE_BUILD = " ${ JOB_NAME} is Failure"
24+ }
25+
426 stages {
527 stage(' Build' ) {
628 steps {
29+ sh " curl --location --request POST 'https://api.telegram.org/bot${ TOKEN} /sendMessage' --form text='${ TEXT_PRE} ' --form chat_id='${ CHAT_ID} '"
30+ sh " curl --location --request POST 'https://api.telegram.org/bot${ TOKEN} /sendMessage' --form text='${ TEXT_BUILD} ' --form chat_id='${ CHAT_ID} '"
731 sh ' docker build -t yamiannephilim/ecommerce:latest .'
832 }
933 }
1034
1135 stage(' Push' ) {
1236 steps {
37+ sh " curl --location --request POST 'https://api.telegram.org/bot${ TOKEN} /sendMessage' --form text='${ TEXT_PUSH} ' --form chat_id='${ CHAT_ID} '"
38+
1339 withDockerRegistry(credentialsId : ' docker_hub' , url : ' https://index.docker.io/v1/' ) {
1440 sh ' docker push yamiannephilim/ecommerce'
1541 }
@@ -18,6 +44,8 @@ pipeline {
1844
1945 stage(' Clean' ) {
2046 steps {
47+ sh " curl --location --request POST 'https://api.telegram.org/bot${ TOKEN} /sendMessage' --form text='${ TEXT_CLEAN} ' --form chat_id='${ CHAT_ID} '"
48+
2149 script {
2250 def containerId = sh(returnStdout : true , script : ' docker ps -aqf "name=ecommerce"' ). trim()
2351 if (containerId) {
@@ -30,6 +58,7 @@ pipeline {
3058
3159 stage(' Run' ) {
3260 steps {
61+ sh " curl --location --request POST 'https://api.telegram.org/bot${ TOKEN} /sendMessage' --form text='${ TEXT_RUN} ' --form chat_id='${ CHAT_ID} '"
3362 sh ' docker container stop ecommerce || echo "this container does not exist"'
3463 sh ' docker network create yan || echo "this network exist"'
3564 sh ' echo y | docker container prune'
@@ -42,5 +71,17 @@ pipeline {
4271 always {
4372 cleanWs()
4473 }
74+
75+ success {
76+ script {
77+ sh " curl --location --request POST 'https://api.telegram.org/bot${ TOKEN} /sendMessage' --form text='${ TEXT_SUCCESS_BUILD} ' --form chat_id='${ CHAT_ID} '"
78+ }
79+ }
80+
81+ failure {
82+ script {
83+ sh " curl --location --request POST 'https://api.telegram.org/bot${ TOKEN} /sendMessage' --form text='${ TEXT_FAILURE_BUILD} ' --form chat_id='${ CHAT_ID} '"
84+ }
85+ }
4586 }
4687}
0 commit comments