jenkins_pipeline后端_python_docker

  1 pipeline {
  2   agent any
  3 
  4   environment {
  5     imagename = "镜像命名"
  6     tag = "v${BUILD_NUMBER}"
  7   }
  8 
  9   stages {
 10     stage('Pull code') {
 11       steps {
 12         echo "${imagename}:${tag}"
 13         git credentialsId: 'git_key', url: 'git地址'
 14       }
 15     }
 16 
 17     stage('Clean local history images') {
 18       steps {
 19         sh 'docker images | grep ${imagename} | awk \'{print $1":"$2}\' | xargs docker rmi || echo "DONE"'
 20       }
 21     }
 22 
 23     stage('Build docker image') {
 24       steps {
 25         sh 'docker build --tag=${imagename}:${tag} .'
 26         sh 'docker tag ${imagename}:${tag} 仓库地址/${imagename}:${tag}'
 27         sh 'docker tag ${imagename}:${tag} 仓库地址/${imagename}:latest'
 28       }
 29     }
 30 
 31     stage('Push docker image') {
 32       steps {
 33         sh 'docker push 仓库地址/${imagename}:${tag}'
 34         sh 'docker push 仓库地址/${imagename}:latest'
 35       }
 36     }
 37 
 38     stage ('Deploy') {
 39       steps {
 40         script {
 41           def remote = [:]
 42           remote.name = '服务器命名'
 43           remote.host = '项目IP'
 44           remote.user = 'user'
 45           remote.password = 'passwd'
 46           remote.allowAnyHosts = true
 47 
 48           sshCommand remote: remote, command: "docker pull 仓库地址/${imagename}:${tag}"
 49           sshCommand remote: remote, command: "docker pull 仓库地址/${imagename}:latest"
 50         }
 51       }
 52     }
 53 
 54     stage ('Restart service') {
 55       steps {
 56         script {
 57           def remote = [:]
 58           remote.name = '服务器命名'
 59           remote.host = '项目IP'
 60           remote.user = 'user'
 61           remote.password = 'passwd'
 62           remote.allowAnyHosts = true
 63 
 64           sshCommand remote: remote, command: "docker stop ${imagename} || echo 'DONE'"
 65           sshCommand remote: remote, command: "docker rm ${imagename} || echo 'DONE'"
 66           sshCommand remote: remote, command: "docker run -d --name ${imagename} -p 5050:5000 仓库地址/${imagename}:latest"
 67         }
 68       }
 69     }
 70 
 71     stage ('Clean history docker images on romote server') {
 72       steps {
 73         script {
 74           def remote = [:]
 75           remote.name = '服务器命名'
 76           remote.host = '项目IP'
 77           remote.user = 'user'
 78           remote.password = 'passwd'
 79           remote.allowAnyHosts = true
 80 
 81           sshCommand remote: remote, command: "docker images 仓库地址/${imagename} --filter \"before=172.17.0.4:5000/${imagename}:latest\" -q | xargs docker rmi || echo 'DONE'"
 82         }
 83       }
 84     }
 85   }
 86   #bearychat报警
 87   post {
 88     unstable {
 89       bearychatSend color: 'red', message: "${env.JOB_NAME},构建不稳定"
 90     }
 91     failure {
 92       bearychatSend color: 'red', message: "${env.JOB_NAME},构建失败"
 93     }
 94   }
 95 
 96 }
 97 
 98 _________________________dockerfile______________________________________
 99 
100 FROM ubuntu:18.04
101 
102 MAINTAINER 乂
103 
104 RUN echo "deb http://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse" > /etc/apt/sources.list && \
105 echo "deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list&& \
106 echo "deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list&& \
107 echo "deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list&& \
108 echo "deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list&& \
109 echo "deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list&& \
110 echo "deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list&& \
111 echo "deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list&& \
112 echo "deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list&& \
113 echo "deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list
114 
115 ENV TZ Asia/Shanghai
116 ENV PruneTreeEnv production
117 
118 RUN apt-get update \
119 && ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
120 && apt-get install -y tzdata \
121 && apt-get clean \
122 && apt-get autoclean \
123 && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
124 
125 RUN apt-get update -y \
126 && apt-get install -y \
127 vim \
128 gunicorn \
129 python3-dev \
130 python3-pip
131 
132 ADD . /code
133 
134 WORKDIR /code
135 
136 RUN pip3 install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip
137 RUN pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/
138 
139 
140 CMD ["gunicorn", "-b 0.0.0.0:5000", "-k gevent", "app:app"]
141 
142 EXPOSE 5000

 

posted @ 2020-12-23 11:23  风儿飘  阅读(167)  评论(0编辑  收藏  举报