本文来自作者
啊文
在
GitChat
上分享
,「
阅读原文
」了解更多知识。
「
文末高能
」
编辑 | 天津饭
一、简介
一般网站部署的流程如下:
需求分析—原型设计—开发代码—内网部署
—
提交测试—确认上线—备份数据—外网更新
—
最终测试,如果发现外网部署的代码有异常,需要及时回滚。
我们可以通过 jenkins 工具平台实现全自动部署 + 测试,是一个可扩展的持续集成引擎,是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能。Jenkins 非常易于安装和配置,简单易用。
-
开发人员:写好代码,不需要自己进行源码编译、打包等工作,直接将代码分支存放在 SVN、GIT 仓库即可。 war 源码多 自动把代码放到服务器上面。
-
运维人员:减轻人工干预的错误率,ansible 一键完成了 同时解放运维人员繁杂的上传代码、手动备份、更新。
-
测试人员:可以通过 jenkins 进行简单的代码及网站测试。
-
持续集成中的任何一个环节都是自动完成的,无需太多的人工干预,有利于减少重复过程以节省时间、费用和工作量。
-
持续集成保障了每个时间点上团队成员提交的代码是能成功集成的。换言之,任何时间点都能第一时间发现软件的集成问题,使任意时间发布可部署的软件成为了可能。
-
持续集成还能利于软件本身的发展趋势,这点在需求不明确或是频繁性变更的情景中尤其重要,持续集成的质量能帮助团队进行有效决策,同时建立团队对开发产品的信心。
-
一个自动构建过程,包括自动编译、分发、部署和测试;
-
一个代码存储库,即需要版本控制软件来保障代码的可维护性,同时作为构建过程的素材库,例如 SVN、GIT 代码库;
-
一个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 [email protected]: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 [email protected]: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
2)安装自动部署的项目所需要的插件:
还需要安装的插件:
创建远程主机。第一步添加凭证:
七、创建 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
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 [email protected]
[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
#哈哈代码提交成功了
八、创建测试任务