# Docker 镜像打包发布回滚

* 参数化设定(选择需要执行的动作)，变量 Action
* pipeline script

```groovy
// 替换为你的 Telegram bot token 和 chat ID
def telegramToken = "TG_TOKEN"
def chatId = "TG_CHAT_ID"


pipeline {
    agent any
    environment {
      GIT_URL = "https://git.com/root/xxxxx.git"
      GIT_BRANCHES = "Branch"
      GIT_USER_ID  = "1"
    }
    stages {
        stage('Cleanup') {
            steps {
                script {
                    deleteDir()
                }
            }
        }
        stage('Git Clone') {
            when {
                 expression { params.Action == 'Deploy' }
            }
            steps {
                script {
                    git branch: "${GIT_BRANCHES}", credentialsId: "${GIT_USER_ID}", url: "${GIT_URL}"
                }
            }
        }
        stage('Jar Build') {
            when {
                 expression { params.Action == 'Deploy' }
            }
            steps {
                script {
                  sh '''
                  cd ${WORKSPACE}/  &&  ${MVN_PATH}  clean install -Dmaven.test.skip=true  -pl  ${NAME}  -am -amd
                  '''
                  echo 'Jar包编译完成'
                }
            }
        }
        stage('Docker build') {
            when {
                 expression { params.Action == 'Deploy' }
            }
            steps {
                    sh '''
                    echo "打包镜像，上传镜像"
                    '''
            }
        }

        stage('Deploy') {
            when {
                 expression { params.Action == 'Deploy' }
            }
            steps {
                    sh '''
                    echo "执行发布"
                    '''
            }
        }
        stage('Rollback') {
            when {
                 expression { params.Action == 'Rollback' }
            }
            steps {
                    sh '''
                    echo "执行回滚"
                    '''
            }
        } 
    }
    post {
      success {
          echo '发布成功'

          def message = "Jenkins Build SUCCESS! \nJob: ${env.JOB_NAME} \nBuild: ${env.BUILD_NUMBER} \nURL: ${env.BUILD_URL}"
          sh """
              curl -s -X POST https://api.telegram.org/bot${TG_TOKEN}/sendMessage \\
              -d chat_id=${TG_CHAT_ID} \\
              -d text="${message}"
          """
      }
      aborted {
          echo '人为终止'

          def message = "Jenkins Build Warning! \nJob: ${env.JOB_NAME} \nBuild: ${env.BUILD_NUMBER} \nURL: ${env.BUILD_URL}"
          sh """
              curl -s -X POST https://api.telegram.org/bot${TG_TOKEN}/sendMessage \\
              -d chat_id=${TG_CHAT_ID} \\
              -d text="${message}"
          """
      }
      failure  {
          echo '发布失败'

          def message = "Jenkins Build Failed! \nJob: ${env.JOB_NAME} \nBuild: ${env.BUILD_NUMBER} \nURL: ${env.BUILD_URL}"
          sh """
              curl -s -X POST https://api.telegram.org/bot${TG_TOKEN}/sendMessage \\
              -d chat_id=${TG_CHAT_ID} \\
              -d text="${message}"
          """
      }
    }
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://close.gitbook.io/yun-wei-bi-ji/centos/cicd/pipeline/docker-jing-xiang-da-bao-fa-bu-hui-gun.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
