码迷,mamicode.com
首页 > 编程语言 > 详细

jenkins_pipeline后端_python_docker

时间:2020-12-29 11:16:14      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ESS   passwd   镜像   nta   $1   失败   doc   ges   shang   

pipeline {
  agent any

  environment {
    imagename = "镜像命名"
    tag = "v${BUILD_NUMBER}"
  }

  stages {
    stage(‘Pull code‘) {
      steps {
        echo "${imagename}:${tag}"
        git credentialsId: ‘git_key‘, url: ‘git地址‘
      }
    }

    stage(‘Clean local history images‘) {
      steps {
        sh ‘docker images | grep ${imagename} | awk \‘{print $1":"$2}\‘ | xargs docker rmi || echo "DONE"‘
      }
    }

    stage(‘Build docker image‘) {
      steps {
        sh ‘docker build --tag=${imagename}:${tag} .‘
        sh ‘docker tag ${imagename}:${tag} 仓库地址/${imagename}:${tag}‘
        sh ‘docker tag ${imagename}:${tag} 仓库地址/${imagename}:latest‘
      }
    }

    stage(‘Push docker image‘) {
      steps {
        sh ‘docker push 仓库地址/${imagename}:${tag}‘
        sh ‘docker push 仓库地址/${imagename}:latest‘
      }
    }

    stage (‘Deploy‘) {
      steps {
        script {
          def remote = [:]
          remote.name = ‘服务器命名‘
          remote.host = ‘项目IP‘
          remote.user = ‘user‘
          remote.password = ‘passwd‘
          remote.allowAnyHosts = true

          sshCommand remote: remote, command: "docker pull 仓库地址/${imagename}:${tag}"
          sshCommand remote: remote, command: "docker pull 仓库地址/${imagename}:latest"
        }
      }
    }

    stage (‘Restart service‘) {
      steps {
        script {
          def remote = [:]
          remote.name = ‘服务器命名‘
          remote.host = ‘项目IP‘
          remote.user = ‘user‘
          remote.password = ‘passwd‘
          remote.allowAnyHosts = true

          sshCommand remote: remote, command: "docker stop ${imagename} || echo ‘DONE‘"
          sshCommand remote: remote, command: "docker rm ${imagename} || echo ‘DONE‘"
          sshCommand remote: remote, command: "docker run -d --name ${imagename} -p 5050:5000 仓库地址/${imagename}:latest"
        }
      }
    }

    stage (‘Clean history docker images on romote server‘) {
      steps {
        script {
          def remote = [:]
          remote.name = ‘服务器命名‘
          remote.host = ‘项目IP‘
          remote.user = ‘user‘
          remote.password = ‘passwd‘
          remote.allowAnyHosts = true

          sshCommand remote: remote, command: "docker images 仓库地址/${imagename} --filter \"before=172.17.0.4:5000/${imagename}:latest\" -q | xargs docker rmi || echo ‘DONE‘"
        }
      }
    }
  }
  #bearychat报警
  post {
    unstable {
      bearychatSend color: ‘red‘, message: "${env.JOB_NAME},构建不稳定"
    }
    failure {
      bearychatSend color: ‘red‘, message: "${env.JOB_NAME},构建失败"
    }
  }

}

_________________________dockerfile______________________________________

FROM ubuntu:18.04

MAINTAINER 乂

RUN echo "deb http://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse" > /etc/apt/sources.list && \
echo "deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic main restricted universe multiverse" >> /etc/apt/sources.list&& \
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list&& \
echo "deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-updates main restricted universe multiverse" >> /etc/apt/sources.list&& \
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list&& \
echo "deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-backports main restricted universe multiverse" >> /etc/apt/sources.list&& \
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list&& \
echo "deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-security main restricted universe multiverse" >> /etc/apt/sources.list&& \
echo "deb http://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list&& \
echo "deb-src http://mirrors.ustc.edu.cn/ubuntu/ bionic-proposed main restricted universe multiverse" >> /etc/apt/sources.list

ENV TZ Asia/Shanghai
ENV PruneTreeEnv production

RUN apt-get update \
&& ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone \
&& apt-get install -y tzdata \
&& apt-get clean \
&& apt-get autoclean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN apt-get update -y \
&& apt-get install -y \
vim \
gunicorn \
python3-dev \
python3-pip

ADD . /code

WORKDIR /code

RUN pip3 install -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip
RUN pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/


CMD ["gunicorn", "-b 0.0.0.0:5000", "-k gevent", "app:app"]

EXPOSE 5000

jenkins_pipeline后端_python_docker

标签:ESS   passwd   镜像   nta   $1   失败   doc   ges   shang   

原文地址:https://www.cnblogs.com/sqbk/p/14177576.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!