Docker 镜像打包发布回滚

  • 参数化设定(选择需要执行的动作),变量 Action

  • pipeline script

// 替换为你的 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}"
          """
      }
    }
}

Last updated