-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Jenkinsfile
88 lines (75 loc) · 3.53 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
pipeline {
agent any
environment {
// Telegram configre
TOKEN = credentials('telegram_token')
CHAT_ID = credentials('telegram_chatid')
// Telegram message
GIT_MESSAGE = sh(returnStdout: true, script: "git log -n 1 --format=%s ${GIT_COMMIT}").trim()
GIT_AUTHOR = sh(returnStdout: true, script: "git log -n 1 --format=%ae ${GIT_COMMIT}").trim()
GIT_COMMIT_SHORT = sh(returnStdout: true, script: "git rev-parse --short ${GIT_COMMIT}").trim()
GIT_INFO = "Branch: ${GIT_BRANCH}\nLast Message: ${GIT_MESSAGE}\nAuthor: ${GIT_AUTHOR}\nCommit: ${GIT_COMMIT_SHORT}"
TEXT_BREAK = '----------------------------------------'
TEXT_PRE = "${TEXT_BREAK}\n${GIT_INFO}"
TEXT_BUILD = "${JOB_NAME} is Building"
TEXT_PUSH = "${JOB_NAME} is Pushing"
TEXT_CLEAN = "${JOB_NAME} is Cleaning"
TEXT_RUN = "${JOB_NAME} is Running"
// Telegram parameters
TEXT_SUCCESS_BUILD = "${JOB_NAME} is Success"
TEXT_FAILURE_BUILD = "${JOB_NAME} is Failure"
}
stages {
stage('Build') {
steps {
sh "curl --location --request POST 'https://api.telegram.org/bot${TOKEN}/sendMessage' --form text='${TEXT_PRE}' --form chat_id='${CHAT_ID}'"
sh "curl --location --request POST 'https://api.telegram.org/bot${TOKEN}/sendMessage' --form text='${TEXT_BUILD}' --form chat_id='${CHAT_ID}'"
sh 'docker build -t yamiannephilim/yanlib:latest .'
}
}
stage('Push') {
steps {
sh "curl --location --request POST 'https://api.telegram.org/bot${TOKEN}/sendMessage' --form text='${TEXT_PUSH}' --form chat_id='${CHAT_ID}'"
withDockerRegistry(credentialsId: 'docker_hub', url: 'https://index.docker.io/v1/') {
sh 'docker push yamiannephilim/yanlib'
}
}
}
stage('Clean') {
steps {
sh "curl --location --request POST 'https://api.telegram.org/bot${TOKEN}/sendMessage' --form text='${TEXT_CLEAN}' --form chat_id='${CHAT_ID}'"
script {
def containerId = sh(returnStdout: true, script: 'docker ps -aqf "name=yanlib"').trim()
if (containerId) {
sh "docker stop $containerId"
sh "docker rm $containerId"
}
}
}
}
stage('Run') {
steps {
sh "curl --location --request POST 'https://api.telegram.org/bot${TOKEN}/sendMessage' --form text='${TEXT_RUN}' --form chat_id='${CHAT_ID}'"
sh 'docker container stop yanlib || echo "this container does not exist"'
sh 'docker network create yan || echo "this network exist"'
sh 'echo y | docker container prune'
sh 'docker run --name yanlib --network yan --restart=unless-stopped -d yamiannephilim/yanlib:latest'
}
}
}
post {
always {
cleanWs()
}
success {
script {
sh "curl --location --request POST 'https://api.telegram.org/bot${TOKEN}/sendMessage' --form text='${TEXT_SUCCESS_BUILD}' --form chat_id='${CHAT_ID}'"
}
}
failure {
script {
sh "curl --location --request POST 'https://api.telegram.org/bot${TOKEN}/sendMessage' --form text='${TEXT_FAILURE_BUILD}' --form chat_id='${CHAT_ID}'"
}
}
}
}