分类

链接

2022 年 10 月
 12
3456789
10111213141516
17181920212223
24252627282930
31  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > DevOps, Docker > 正文
共享办公室出租
jenkinsfile
DevOps, Docker 暂无评论 阅读(120)

1

  1. pipeline {
  2.     agent any
  3.  
  4.     environment{
  5.         def build_env="dev"
  6.         def mailFrom="xxxxx@163.com"
  7.         def mailRecipients="xxxxx@xxx.com"
  8.     }
  9.  
  10.     stages {
  11.         stage('Pull Code') {
  12.             steps {
  13.  
  14.                 //
  15.                 checkout([$class: 'GitSCM',branches: [[name: '*/main']], extensions: [], userRemoteConfigs: [[credentialsId: '511c4c13-45bc-4c9f-ba38-3b37d05e4a9a', url: 'git@github.com:xxxx/CICD.git']]])
  16.             }
  17.  
  18.             post { 
  19.                 success { 
  20.                     echo 'pull code success.'
  21.                 }
  22.             }
  23.         }
  24.         stage('Deploy'){
  25.             steps {
  26.                 script {
  27.                     echo "Start build python-api"
  28.  
  29.                      
  30.                     def count = sh(script: "docker ps|grep python-api |wc -l", returnStdout: true).trim()
  31.                 
  32.                     if (count!='0'){
  33.                         echo "docker stop python-api"
  34.                         sh "docker stop python-api" 
  35.                     }
  36.  
  37.                     def count2 = sh(script: "docker ps -a|grep python-api |wc -l", returnStdout: true).trim()
  38.                     if (count2!='0'){
  39.                         echo "docker rm python-api"
  40.                         sh "docker rm python-api"
  41.                     }
  42.  
  43.  
  44.                     echo "docker rmi python-api"
  45.                     sh "docker rmi python-api:${build_env}"
  46.  
  47.                     echo "docker build"
  48.                     sh "docker build -t python-api:${build_env} ."
  49.  
  50.                     echo "clear old images(status=none)"
  51.                     sh "docker image prune -f"
  52.  
  53.                     
  54.                 }
  55.             }
  56.             post { 
  57.                 success { 
  58.                     echo 'build docker success.'
  59.                 }
  60.             }
  61.         }
  62.  
  63.         stage('Run'){
  64.             steps {
  65.                 echo "docker run python-api"
  66.                 sh "docker run  --name=python-api -d -p 8088:8088 python-api:${build_env}"
  67.             }
  68.         }
  69.  
  70.         stage('Test'){
  71.             steps {
  72.                 script{
  73.                     echo "test status...."
  74.                     def response = httpRequest "http://139.x.x.x:8088/"
  75.                     println('Status: '+response.status)
  76.                     println('Response: '+response.content)
  77.  
  78.                     def res =  response.content.replace("\"", "")
  79.  
  80.                     if(res == "ok"){
  81.                         //send email
  82.                         echo "Deploy success"
  83.                     }else{
  84.                         error "Deploy failed. API can not access."
  85.                         
  86.                     }
  87.                 }
  88.                 
  89.             }
  90.  
  91.         }
  92.     }
  93.  
  94.  
  95.     post {
  96.         always {
  97.             echo 'Build completed...'
  98.         }
  99.         success {
  100.             echo 'build success!'
  101.             mail subject: "[Jenkins] ${env.JOB_NAME} [Build#${env.BUILD_NUMBER}] - Success!",
  102.             body: "Check console output at ${env.BUILD_URL} to view the results.",
  103.             charset: 'utf-8',
  104.             from: "${mailFrom}",
  105.             mimeType: 'text/html',
  106.             to : "${mailRecipients}"
  107.          }
  108.         failure {
  109.             echo 'build failure!'
  110.             mail subject: "[Jenkins] ${env.JOB_NAME} [Build#${env.BUILD_NUMBER}] - Failure!",
  111.             body: "Check console output at ${env.BUILD_URL} to view the results.",
  112.             charset: 'utf-8',
  113.             from: "${mailFrom}",
  114.             mimeType: 'text/html',
  115.             to: "${mailRecipients}"
  116.         }
  117.         unstable {
  118.             echo 'the job is marked as unstable.'
  119.         }
  120.         changed {
  121.             echo ''
  122.         }
  123.     }
  124. }

2

 

 

 

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:jenkinsfile | Bruce's Blog

发表评论

留言无头像?