Docker 容器服务重启
Last updated
Last updated
参数化设定(选择需要重新的服务)
Pipeline 脚本
def IP001 = "1.1.1.1"
def IP002 = "2.2.2.2"
def IP003 = "3.3.3.3"
pipeline {
agent any
environment {
SERVER_IP_STR = ''
}
stages {
stage('Get Choose') {
steps {
echo "Restart for environment: $serviceName"
}
}
stage('Init Env') {
steps {
echo "Deploying to Restart ${serviceName}"
script {
switch (serviceName) {
case 'aaaa':
echo "${IP001},${IP002}"
SERVER_IP_STR = "${IP001},${IP002}"
break
case 'bbbb':
SERVER_IP_STR = "${IP001},${IP002}"
break
case 'cccc':
SERVER_IP_STR = "${IP001},${IP002}"
break
case 'dddd':
SERVER_IP_STR = "${IP001},${IP002}"
break
case 'ffff':
SERVER_IP_STR = "${IP002},${IP003}"
break
default:
echo "Unknown environment: ${serviceName}"
break
}
}
echo "Environment Variable After: ${SERVER_IP_STR}"
}
}
stage('Ping') {
steps {
script {
echo "Ping address ${SERVER_IP_STR}"
sh "ansible ${SERVER_IP_STR} -m ping"
}
}
}
stage('Restart}') {
steps {
script {
echo "Restart Service ${env.SERVER_IP_STR}"
sh "ansible ${SERVER_IP_STR} -m shell -a 'docker restart ${serviceName}'"
}
}
}
}
post {
failure {
script {
// 替换为你的 Telegram bot token 和 chat ID
def telegramToken = "TG_TOKEN"
def chatId = "TG_CHAT_ID"
def message = "Jenkins Build Failed! Job: ${env.JOB_NAME} \nBuild: ${env.BUILD_NUMBER} \nURL: ${env.BUILD_URL}"
// 使用 curl 命令发送请求到 Telegram API
sh """
curl -s -X POST https://api.telegram.org/bot${telegramToken}/sendMessage \\
-d chat_id=${chatId} \\
-d text="${message}"
"""
}
}
}
}