一,簡介
一般網站部署的流程如下:
需求分析—原型設計—開發(fā)代碼—內網部署-提交測試—確認上線—備份數(shù)據(jù)—外網更新-最終測試,如果發(fā)現(xiàn)外網部署的代碼有異常,需要及時回滾
我們可以通過jenkins工具平臺實現(xiàn)全自動部署+測試,是一個可擴展的持續(xù)集成引擎,是一個開源軟件項目,旨在提供一個開放易用的軟件平臺,使軟件的持續(xù)集成變成可能。Jenkins非常易于安裝和配置,簡單易用
- 簡單來說方便如下人員:
- 開發(fā)人員:寫好代碼,不需要自己進行源碼編譯、打包等工作,直接將代碼分支存放在SVN、GIT倉庫即可。 war 源碼多 自動把代碼放到服務器上面
- 運維人員:減輕人工干預的錯誤率,ansible 一鍵完成了 同時解放運維人員繁雜的上傳代碼、手動備份、更新
- 測試人員:可以通過jenkins進行簡單的代碼及網站測試
- 持續(xù)集成的意義:
- 持續(xù)集成中的任何一個環(huán)節(jié)都是自動完成的,無需太多的人工干預,有利于減少重復過程以節(jié)省時間、費用和工作量
- 持續(xù)集成保障了每個時間點上團隊成員提交的代碼是能成功集成的。換言之,任何時間點都能第一時間發(fā)現(xiàn)軟件的集成問題,使任意時間發(fā)布可部署的軟件成為了可能
- 持續(xù)集成還能利于軟件本身的發(fā)展趨勢,這點在需求不明確或是頻繁性變更的情景中尤其重要,持續(xù)集成的質量能幫助團隊進行有效決策,同時建立團隊對開發(fā)產品的信心
- 持續(xù)集成的組件:
- 一個自動構建過程,包括自動編譯、分發(fā)、部署和測試
- 一個代碼存儲庫,即需要版本控制軟件來保障代碼的可維護性,同時作為構建過程的素材庫,例如SVN、GIT代碼庫
- 一個jenkins持續(xù)集成服務器就是一個配置簡單和使用方便的持續(xù)集成服務器
二,安裝jenkins
由于jenkins是使用java代碼開發(fā)的,所以我們需要安裝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
三. 創(chuàng)建遠程倉庫
在碼云注冊一個賬號并創(chuàng)建一個項目
-
創(chuàng)建代碼庫 我這里使用的是碼云
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
- 創(chuàng)建公鑰和私鑰
[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)
六. 安裝插件
- 更換更新插件源 國內連接塊的地址地址: http://mirror.xmission.com/jenkins/updates/current/update-center.json

image.png

image.png

image.png
- 安裝自動部署的項目所需要的插件

image.png
- 還需要安裝的插件

image.png

image.png
- 創(chuàng)建遠程主機
第一步添加憑證

image.png

image.png

image.png

image.png
七. 創(chuàng)建django項目
#創(chuàng)建django項目
[root@vagrant-centos65 test-git]# django-admin startproject test11
#出現(xiàn)了一個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
#創(chuàng)建一個django app 創(chuàng)建靜態(tài)模板目錄 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
#哈哈代碼提交成功了
八. 創(chuàng)建測試任務

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 發(fā)送給碼云
cat ~/.ssh/id_rsa 寫入到jenkins
九. 創(chuàng)建內網部署任務
- 注意
BUILD_ID=DONTKILLME : 在jenkins里面在后臺運行的程序都會被jenkins自動殺死 所以需要加上BUILDID這個參數(shù) 加了這個參數(shù)你的程序就不會被殺

image.png

image.png

image.png

image.png

image.png

image.png
第十. 創(chuàng)建外網部署任務

image.png

image.png

image.png

image.png
- 遠程主機要做的事情
- 安裝django
- 創(chuàng)建一個目錄把代碼版本庫拉取到本地
- 寫腳本來運行你代碼
腳本內容
- 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


