# Docker 容器服务重启



* 参数化设定(选择需要重新的服务)

<figure><img src="https://2134947750-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvILwbD2PrkBCkSitM3m4%2Fuploads%2F99fFm6UL8iFciWKBkkCW%2Fimage.png?alt=media&#x26;token=5aab2953-4fe3-4604-950a-dd8b8ea93e1a" alt=""><figcaption></figcaption></figure>



* Pipeline 脚本

<figure><img src="https://2134947750-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvILwbD2PrkBCkSitM3m4%2Fuploads%2FxPzY8munPulXTYUUvHw0%2Fimage.png?alt=media&#x26;token=984a1a91-cb14-47e4-b148-ce30a6cfc8f6" alt=""><figcaption></figcaption></figure>



```groovy
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}"
                """
            }
        }
    }
}
```
