Jenkins + Django 完整实战,细化到每一步操作

Reference:  http://blog.csdn.net/GitChat/article/details/78271099?locationNum=3&fps=1

 

【不要错过文末彩蛋】

一、简介

一般网站部署的流程如下:

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

我们可以通过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

三、创建远程仓库

在码云注册一个账号并创建一个项目。

1)创建代码库:我这里使用的是码云。

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

2)创建公钥和私钥:

[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

 

3)把这个公钥添加到码云上:

image.png

4)同步代码:

[root@vagrant-centos65 opt]# git clone git@git.oschina.net:ZhiLiaoAWen/test.git test-git/

四、升级python 2.6到python 2.7

由于jdnago 1.7之后的版本就不支持python 2.6了,所以我们需要升级python 2.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

2)安装自动部署的项目所需要的插件:

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

【GitChat达人课】

  1. 前端恶棍 · 大漠穷秋 :《Angular 初学者快速上手教程 
  2. Python 中文社区联合创始人 · Zoom.Quiet :《GitQ: GitHub 入味儿 
  3. 前端颜值担当 · 余博伦:《如何从零学习 React 技术栈 
  4. GA 最早期使用者 · GordonChoi:《GA 电商数据分析实践课
  5. 技术总监及合伙人 · 杨彪:《Gradle 从入门到实战
  6. 混元霹雳手 · 江湖前端:《Vue 组件通信全揭秘
  7. 知名互联网公司安卓工程师 · 张拭心:《安卓工程师跳槽面试全指南
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值