GitChat 作者:啊文
交流实录:Jenkins + Django 完整实战,细化到每一步操作
关注微信公众号:「GitChat 技术杂谈」 一本正经的讲技术
【不要错过文末彩蛋】
一般网站部署的流程如下:
需求分析—原型设计—开发代码—内网部署-提交测试—确认上线—备份数据—外网更新-最终测试,如果发现外网部署的代码有异常,需要及时回滚
我们可以通过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。
在码云注册一个账号并创建一个项目。
1)创建代码库:我这里使用的是码云。
[root@vagrant-centos65 opt]# mkdir test-git
[root@vagrant-centos65 opt]# git clone git@git.oschina.net:ZhiLiaoAWen/test.git test-git/
2)创建公钥和私钥:
[root@vagrant-centos65 opt]# ssh-keygen -t rsa -C "[email protected]"
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 [email protected]
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== [email protected]
3)把这个公钥添加到码云上:
4)同步代码:
[root@vagrant-centos65 opt]# git clone git@git.oschina.net:ZhiLiaoAWen/test.git test-git/
由于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
安装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
2)安装自动部署的项目所需要的插件:
还需要安装的插件:
创建远程主机。第一步添加凭证:
#创建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
hello zhiliaoawen
[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。
把项目提交到码云:
[root@vagrant-centos65 test-git]# git add test11/
[root@vagrant-centos65 test-git]# git commit -m 'test django'
[master cf58679] test django
Committer: root
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 [email protected]
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name '
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 [email protected]:ZhiLiaoAWen/test.git
47edbdc..cf58679 master -> master
#哈哈代码提交成功了
公钥和私钥分配:
[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这个参数。加了这个参数你的程序就不会被杀。
远程主机要做的事情:
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 &