jenkins部署django项目

一,简介
一般网站部署的流程如下:
需求分析—原型设计—开发代码—内网部署-提交测试—确认上线—备份数据—外网更新-最终测试,如果发现外网部署的代码有异常,需要及时回滚

我们可以通过jenkins工具平台实现全自动部署+测试,是一个可扩展的持续集成引擎,是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。Jenkins非常易于安装和配置,简单易用

  • 简单来说方便如下人员:
  1. 开发人员:写好代码,不需要自己进行源码编译、打包等工作,直接将代码分支存放在SVN、GIT仓库即可。 war 源码多 自动把代码放到服务器上面
  2. 运维人员:减轻人工干预的错误率,ansible 一键完成了 同时解放运维人员繁杂的上传代码、手动备份、更新
  3. 测试人员:可以通过jenkins进行简单的代码及网站测试
  • 持续集成的意义:
  1. 持续集成中的任何一个环节都是自动完成的,无需太多的人工干预,有利于减少重复过程以节省时间、费用和工作量
  2. 持续集成保障了每个时间点上团队成员提交的代码是能成功集成的。换言之,任何时间点都能第一时间发现软件的集成问题,使任意时间发布可部署的软件成为了可能
  3. 持续集成还能利于软件本身的发展趋势,这点在需求不明确或是频繁性变更的情景中尤其重要,持续集成的质量能帮助团队进行有效决策,同时建立团队对开发产品的信心
  • 持续集成的组件:
  1. 一个自动构建过程,包括自动编译、分发、部署和测试
  2. 一个代码存储库,即需要版本控制软件来保障代码的可维护性,同时作为构建过程的素材库,例如SVN、GIT代码库
  3. 一个jenkins持续集成服务器就是一个配置简单和使用方便的持续集成服务器
    二,安装jenkins
    由于jenkins是使用java代码开发的,所以我们需要安装java容器才能运行jenkins,又因为java的web服务器用的是tomcat,所以我们要安装JDK+Tomcat
  • 安装JDK+Tomcat
[root@vagrant-centos65 ~]# yum -y install java-1.8.0-openjdk.x86_64
[root@vagrant-centos65 ~]# cd /opt/
[root@vagrant-centos65 opt]# wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-7/v7.0.81/bin/apache-tomcat-7.0.81.tar.gz
[root@vagrant-centos65 opt]# tar zxvf apache-tomcat-7.0.81.tar.gz
[root@vagrant-centos65 opt]# mkdir -p /usr/local/tomcat
[root@vagrant-centos65 opt]# mv apache-tomcat-7.0.81/* /usr/local/tomcat
  • 安装jenkins
[root@vagrant-centos65 ~]# cd /opt/
[root@vagrant-centos65 opt]# wget http://mirrors.tuna.tsinghua.edu.cn/jenkins/redhat/jenkins-2.60-1.1.noarch.rpm
[root@vagrant-centos65 opt]# rpm -ivh jenkins-2.60-1.1.noarch.rpm
[root@vagrant-centos65 opt]# /etc/init.d/jenkins start
Starting Jenkins                                           [  OK  ]
[root@vagrant-centos65 opt]# netstat -tnlp | grep 8080
[root@vagrant-centos65 opt]# /etc/init.d/jenkins start
Starting Jenkins                                           [  OK  ]
[root@vagrant-centos65 opt]# netstat -tnlp | grep 8080
tcp        0      0 :::8080                     :::*                        LISTEN      7599/java 
  • 注意:
    有的时候需要启动二次jenkins


    jenkins1.png

    jenkins2查看密码.png
jenkins3.png
jenkins4.png
jenkins5.png
jenkins6.png

三. 创建远程仓库
在码云注册一个账号并创建一个项目

  • 创建代码库 我这里使用的是码云


    jenkins7.png
jenkins8.png
[root@vagrant-centos65 opt]# mkdir test-git  
[root@vagrant-centos65 opt]# git clone git@git.oschina.net:ZhiLiaoAWen/test.git test-git/
jenkins9.png
  • 创建公钥和私钥
[root@vagrant-centos65 opt]# ssh-keygen -t rsa -C "zhiliao@awen.com"  
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ca:f6:1d:56:93:b0:c3:09:70:96:cd:19:98:d7:53:68 zhiliao@awen.com
The key's randomart image is:
+--[ RSA 2048]----+
|      . o*.+ o.  |
|       ++ = E    |
|        .... .   |
|         o + .   |
|        S = +    |
|     . .   o .   |
|      +   o      |
|     . . o .     |
|        . .      |
+-----------------+
# 三次回车即可生成 ssh key

[root@vagrant-centos65 opt]# cat ~/.ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA8n0uaD1t3HRWDxFcBXp769peImctPfBjTV2b3Co6jyzW9FLrVouuvK793hrigi/KYBN//Bewz1Hb7b9mxBMyfWOw9tHBJo4JOk/v6fhAGZf0yFO53wFG3yKCYuwypzbIPZiwrUPbuB12jLiWoR/aSlrDsd3/Y6155g6FZOfp+jnRcAwzlP/Jed066JeeI0eTIiz9qR2pzGzYK+QPwdHur/ZMYvoKep+NBBuly4l2rjtQpoKGdrZtseHNciWrqE4TqyTmJ/jvEyZH1ZAww5E69AoAqGgP7J24GwH5JaT3Ykz9hPb4yeylOs6aNGBt47WtJ8INpJtdj3oot/GnRNyGXQ== zhiliao@awen.com

把这个公钥添加到码云上面就可以了

image.png
  • 同步代码
[root@vagrant-centos65 opt]# git clone git@git.oschina.net:ZhiLiaoAWen/test.git test-git/

四: 升级python2.6到python2.7
由于jdnago1.7之后的版本就不支持python2.6了,所以我们需要升级python2.6-2.7

[root@vagrant-centos65 ~]# yum -y install  zlib zlib-devel openssl openssl-devel  sqlite-devel
[root@vagrant-centos65 ~]# wget http://python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2
[root@vagrant-centos65 ~]# tar -jxvf Python-2.7.3.tar.bz2
[root@vagrant-centos65 ~]# cd Python-2.7.3
[root@vagrant-centos65 Python-2.7.3]# ./configure    --prefix=/usr/local/python2.7
[root@vagrant-centos65 Python-2.7.3]# make && make install
[root@vagrant-centos65 Python-2.7.3]# cd /usr/bin/
[root@vagrant-centos65 bin]# ll | grep python
-rwxr-xr-x.   2 root root      4864 Nov 22  2013 python
lrwxrwxrwx.   1 root root         6 Jan 16  2014 python2 -> python
-rwxr-xr-x.   2 root root      4864 Nov 22  2013 python2.6
[root@vagrant-centos65 bin]# mv python python2.6.bak
[root@vagrant-centos65 bin]# ln -s /usr/local/python2.7/bin/python /usr/bin/python
[root@vagrant-centos65 bin]# vi /usr/bin/yum
#!/usr/bin/python2.6

五, 安装django

  • 安装setuptools
    pip的安装需要依赖setuptools,其实是pip的安装setup.py有这样一条代码from setuptools import setup
[root@vagrant-centos65 bin]# cd /opt/
[root@vagrant-centos65 opt]# wget https://pypi.python.org/packages/61/3c/8d680267eda244ad6391fb8b211bd39d8b527f3b66207976ef9f2f106230/setuptools-1.4.2.tar.gz
[root@vagrant-centos65 opt]# tar zxvf setuptools-1.4.2.tar.gz
[root@vagrant-centos65 opt]# cd setuptools-1.4.2
[root@vagrant-centos65 setuptools-1.4.2]# python setup.py install
  • 安装pip
[root@vagrant-centos65 ~]# cd /opt/
[root@vagrant-centos65 opt]# wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate
[root@vagrant-centos65 opt]# tar zxvf pip-1.5.4.tar.gz
[root@vagrant-centos65 opt]# cd pip-1.5.4
[root@vagrant-centos65 pip-1.5.4]# python setup.py install
[root@vagrant-centos65 pip-1.5.4]# pip
-bash: pip: command not found
[root@vagrant-centos65 pip-1.5.4]# find / -name pip
/usr/local/python2.7/bin/pip
[root@vagrant-centos65 pip-1.5.4]# ln -s /usr/local/python2.7/bin/pip /usr/bin/pip
  • 安装django
[root@vagrant-centos65 pip-1.5.4]# pip install django
[root@vagrant-centos65 pip-1.5.4]# pip list
Django (1.11.3)
pip (1.5.4)
pytz (2017.2)
setuptools (1.4.2)
wsgiref (0.1.2)

六. 安装插件

  1. 更换更新插件源 国内连接块的地址地址: http://mirror.xmission.com/jenkins/updates/current/update-center.json
image.png
image.png
image.png
  1. 安装自动部署的项目所需要的插件
image.png
  • 还需要安装的插件
image.png
image.png
  • 创建远程主机
    第一步添加凭证
image.png
image.png
image.png
image.png

七. 创建django项目

#创建django项目
[root@vagrant-centos65 test-git]# django-admin startproject test11
#出现了一个test11目录就是我们的django项目的容器
[root@vagrant-centos65 test-git]# ll
total 8
-rw-r--r-- 1 root root   29 Sep 15 12:55 README.md
drwxr-xr-x 3 root root 4096 Sep 15 14:55 test11
#创建一个django app 创建静态模板目录 templates
[root@vagrant-centos65 test11]# python manage.py startapp test22
[root@vagrant-centos65 test11]# mkdir  templates
#修改django配置文件
[root@vagrant-centos65 test11]# vim test11/settings.py
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR+"/templates",],
#添加test22  
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'test22',
]
#修改3个文件
[root@vagrant-centos65 test11]# cat templates/index.html 
<h1>hello zhiliaoawen<h1>
[root@vagrant-centos65 test11]# cat test22/views.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.shortcuts import render

# Create your views here.
def index(request):
    return render(request, 'index.html')
[root@vagrant-centos65 test11]# cat test11/urls.py
from django.conf.urls import url
from django.contrib import admin
from test22 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$',views.index)
]
#启动django
[root@vagrant-centos65 test11]# python manage.py runserver 0.0.0.0:8000
  • 结果 我们在访问django的时候可以看到页面 hello zhiliaoawen
image.png
  • 把项目提交到码云
[root@vagrant-centos65 test-git]# git add test11/
[root@vagrant-centos65 test-git]# git commit -m 'test django'
[master cf58679] test django
 Committer: root <root@vagrant-centos65.vagrantup.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

    git config --global user.name "Your Name"
    git config --global user.email you@example.com

If the identity used for this commit is wrong, you can fix it with:

    git commit --amend --author='Your Name <you@example.com>'

 20 files changed, 202 insertions(+), 0 deletions(-)
 create mode 100644 test11/db.sqlite3
 create mode 100755 test11/manage.py
 create mode 100644 test11/templates/index.html
 create mode 100644 test11/test11/__init__.py
 create mode 100644 test11/test11/__init__.pyc
 create mode 100644 test11/test11/settings.py
 create mode 100644 test11/test11/settings.pyc
 create mode 100644 test11/test11/urls.py
 create mode 100644 test11/test11/urls.pyc
 create mode 100644 test11/test11/wsgi.py
 create mode 100644 test11/test11/wsgi.pyc
 create mode 100644 test11/test22/__init__.py
 create mode 100644 test11/test22/__init__.pyc
 create mode 100644 test11/test22/admin.py
 create mode 100644 test11/test22/admin.pyc
 create mode 100644 test11/test22/apps.py
 create mode 100644 test11/test22/migrations/__init__.py
 create mode 100644 test11/test22/migrations/__init__.pyc
 create mode 100644 test11/test22/models.py
 create mode 100644 test11/test22/models.pyc
 create mode 100644 test11/test22/tests.py
 create mode 100644 test11/test22/views.py
 create mode 100644 test11/test22/views.pyc
[root@vagrant-centos65 test-git]# git config --global user.name "zhiliaoawen"
[root@vagrant-centos65 test-git]# git config --global user.email 44262769@qq.com
[root@vagrant-centos65 test-git]# git push
Counting objects: 29, done.
Compressing objects: 100% (25/25), done.
Writing objects: 100% (28/28), 7.02 KiB, done.
Total 28 (delta 2), reused 0 (delta 0)
To git@git.oschina.net:ZhiLiaoAWen/test.git
   47edbdc..cf58679  master -> master
#哈哈代码提交成功了

八. 创建测试任务

image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png
  • 公钥和私钥分配
[root@vagrant-centos65 ~]# ssh-keygen -t rsa -C "xxxx@qq.com"
cat ~/.ssh/id_rsa.pub    发送给码云
cat ~/.ssh/id_rsa        写入到jenkins

九. 创建内网部署任务

  • 注意
    BUILD_ID=DONTKILLME : 在jenkins里面在后台运行的程序都会被jenkins自动杀死 所以需要加上BUILDID这个参数 加了这个参数你的程序就不会被杀
image.png
image.png
image.png
image.png
image.png
image.png

第十. 创建外网部署任务

image.png
image.png
image.png
image.png
  • 远程主机要做的事情
  1. 安装django
  2. 创建一个目录把代码版本库拉取到本地
  3. 写脚本来运行你代码
    脚本内容
  • xxx.sh
[root@vagrant-centos65 data]# cat xxx.sh 
#!/bin/bash
cd /data
sh start.sh
  • start.sh
[root@vagrant-centos65 data]# cat start.sh 
#!/bin/bash
cd /data/git-test
git pull
cd /data/git-test/test11
nohup python manage.py runserver 0.0.0.0:8000 > /dev/null   2>&1 &
image.png
image.png
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 195,898评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,401评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,058评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,539评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,382评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,319评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,706评论 3 386
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,370评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,664评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,715评论 2 312
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,476评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,326评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,730评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,003评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,275评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,683评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,877评论 2 335

推荐阅读更多精彩内容