Jenkins的Pipeline流水線
主機名 | IP地址 | 備注 |
---|---|---|
Git | 192.168.200.70 | Git服務器 |
Jenkins | 192.168.200.91 | Jenkins服務器 |
cat /etc/redhat-release
uname -r
systemctl stop firewalld
systemctl disable firewalld
systemctl stop NetworkManager
systemctl disable NetworkManager
Pipeline流水線介紹
創建一個基於Pipeline流水線的項目
添加項目Git參數化構建
Git Parameter插件不支持流水線,版本過低
Pipeline腳本語法架構介紹
#Pipeline腳本語法架構
node ('slave節點名') { #被操控的節點服務器
def 變量 #def可以進行變量聲明
stage('階段名A'){ #流水線階段一
執行步驟A
執行步驟B
執行步驟C
}
stage('階段名B'){ #流水線階段二
執行步驟A
執行步驟B
執行步驟C
}
stage('階段名C'){ #流水線階段三
執行步驟A
執行步驟B
執行步驟C
}
}
圖片說明
流水線模板腳本
node {
def mvnHome
stage('Preparation') { // for display purposes
// Get some code from a GitHub repository
git 'https://github.com/jglick/simple-maven-project-with-tests.git'
// Get the Maven tool.
// ** NOTE: This 'M3' Maven tool must be configured
// ** in the global configuration.
mvnHome = tool 'M3'
}
stage('Build') {
// Run the maven build
if (isUnix()) {
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
} else {
bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean package/)
}
}
stage('Results') {
junit '**/target/surefire-reports/TEST-*.xml'
archive 'target/*.jar'
}
}
node {
//def mvnHome
stage('checkout') { // for display purposes
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false,
extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/app.git']]])
}
stage('maven Build') {
echo "maven build ..."
}
stage('deploy') {
echo "deploy..."
}
stage('test') {
echo "test..."
}
}
利用Pipeline Syntax,編寫Pipeline Script並構建
(1)進入Pipeline Syntax
node {
//def mvnHome
stage('checkout') { // for display purposes
}
stage('maven Build') {
echo "maven build ..."
}
stage('deploy') {
echo "deploy..."
}
stage('test') {
echo "test..."
}
}
圖片說明
(2)通過腳本代碼生成器,生成Pipeline腳本代碼
圖片說明
圖片說明
(3)將生成的代碼復制到流水線腳本相應步驟的stage函數里
checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998', url: 'git@192.168.200.70:/home/git/repos/app.git']]])
圖片說明
(4)開始構建Pipeline項目
圖片說明
(5)在Jenkins本地服務器查看拉取結果
cd /var/lib/jenkins/workspace
ls
從遠程倉庫下載Pipeline Script,並構建
(1)在Git服務器上創建一個存放Pipeline腳本的倉庫
su - git
cd /home/git/repos/
pwd
ls
mkdir jenkinsfile #創建存放Pipeline腳本的倉庫
cd jenkinsfile/
git --bare init #初始化倉庫
(2)在jenkins服務器上,往遠程倉庫提交一個Pipeline腳本。
mkdir /test
cd /test
git clone git@192.168.200.70:/home/git/repos/jenkinsfile
ls
cd jenkinsfile/
mkdir itemA
vim itemA/jenkinsfile
cat itemA/jenkinsfile
node {
//def mvnHome
stage('checkout') { // for display purposes
checkout([$class: 'GitSCM', branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false,
extensions: [], submoduleCfg: [],
userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/app.git']]])
}
stage('maven Build') {
echo "maven build ..."
}
stage('deploy') {
echo "deploy..."
}
stage('test') {
echo "test..."
}
}
將腳本推送到遠程倉庫的master分支
git add *
git commit -m "jenkinsfile"
git push -u origin master
(3)利用遠程倉庫里的Pipeline腳本,進行流水線的構建
圖片說明
(4)在jenkins服務器上查詢拉取代碼結果(之前清空過jenkins下的workspace工作目錄)
cd /var/lib/jenkins/workspace/
ls
cd A-PHP
項目案例一:Jenkins+Pipeline+Git+PHP博客項目流水線自動發布
主機名 | IP地址 | 備注 |
---|---|---|
Git | 192.168.200.70 | Git服務器 |
Jenkins | 192.168.200.91 | Jenkins服務器 |
Web | 192.168.200.101 | Web服務器 |
所有服務器進行如下操作
cat /etc/redhat-release
uname -r
systemctl stop firewalld
systemctl disable firewalld
systemctl stop NetworkManager
systemctl disable NetworkManager
創建一個Pipeline流水線項目並進行參數化構建
由於我們仍舊打算將pipeline腳本放在遠程Git倉庫里,因此我們需要從遠程Git倉庫拉取Pipeline腳本,所以,參數化構建不支持Git的參數化。我們只能使用字符結構的參數化構建。
圖片說明
下載用於自動化發布的PHP源碼wordpress源碼包,並上傳遠程git倉庫
在Git服務器上創建用於存放源碼的Git倉庫
hostname -I
cd /home/git/repos/
ls
mkdir wordpress
cd wordpress/
git --bare init
cd ..
ll
chown -R git.git wordpress
在jenkins服務器上,克隆創建好的遠程Git倉庫
mkdir /php-app
cd /php-app
git clone git@192.168.200.70:/home/git/repos/wordpress
ls
在jenkins服務器上,下載wordpres源代碼
鏈接:https://pan.baidu.com/s/1hGyd1xFuL0oPBsFeROuUYQ
提取碼:tmgk --->wordpress百度雲下載地址wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
cd /php-app/wordpress/
ls
tar xf wordpress-4.9.4-zh_CN.tar.gz
ls
mv wordpress-4.9.4-zh_CN.tar.gz /tmp/
ls
mv wordpress/* .
rm -rf wordpress/
ls
在jenkins上提交代碼到遠程Git倉庫
git add *
git commit -m "第一次提交"
git push -u origin master
設置分布式構建Web服務器的slave管理節點
我們計划利用分布式構建的方式,啟動pipeline的流水線項目發布
slave管理節點就設置為需要用於發布項目的Web服務器
添加用於連接slave代理節點的SSH證書(上文已經設置過了)
添加並設置Web服務器的slave管理從節點
圖片說明
圖片說明
Web服務器slave從節點安裝java環境,並啟動jenkins的slave管理節點
鏈接:https://pan.baidu.com/s/1EXO2hSetnkapR29ojJIGTg
提取碼:ixgn --->jdk百度雲下載地址
解壓安裝jdk
tar xf jdk-8u60-linux-x64.tar.gz -C /usr/local/
cd /usr/local
mv jdk1.8.0_60/ jdk
/usr/local/jdk/bin/java -version
配置java環境
sed -i.org '$a export JAVA_HOME=/usr/local/jdk/' /etc/profile
sed -i.org '$a export PATH=$PATH:$JAVA_HOME/bin' /etc/profile
sed -i.org '$a export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/lib/tools.jar' /etc/profile
tail -3 /etc/profile
source /etc/profile
java -version
重新啟動Slave從節點
圖片說明
Web服務器安裝LNMP環境,並手動拉取代碼模擬訪問
yum -y install epel-release
yum -y install nginx php-fpm php-mysql
修改nginx配置文件
cd /etc/nginx/
ls
cp nginx.conf{,.bak}
egrep -v "#|^$" nginx.conf.bak > nginx.conf
cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf; #include了一個配置文件目錄
server {
listen 80 default_server; #默認的server配置(如果用IP訪問就進入這個server)
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html; #默認的網頁目錄
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
由於默認的配置文件include了/etc/nginx/conf.d/*.conf因此我們增加一個server配置文件即可
vim /etc/nginx/conf.d/wp.conf
cat /etc/nginx/conf.d/wp.conf
server {
listen 80;
server_name www.yunjisuan.com;
root /usr/share/nginx/html/www.yunjisuan.com;
location / {
index index.php index.html;
}
location ~\.php {
fastcgi_index index.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
}
創建網頁目錄
cd /usr/share/nginx/html/
ls
mkdir www.yunjisuan.com
cd www.yunjisuan.com
ls
克隆Git倉庫代碼到本地網頁目錄
yum -y install git
git init
git clone git@192.168.200.70:/home/git/repos/wordpress
ls
mv wordpress/* .
ls
rm -rf wordpress/
將網頁目錄權限授權給apache程序用戶
cd ..
ll
id apache
chown -R apache.apache /usr/share/nginx/html/www.yunjisuan.com
啟動nginx服務和php-fpm服務
systemctl start nginx
systemctl start php-fpm
ss -antup | egrep "80|9000"
systemctl enable nginx
systemctl enable php-fpm
做好宿主機的域名映射后,瀏覽器訪問測試
在遠程Git倉庫中創建一個用於構建的Pipeline腳本
在jenkins服務器上進行如下操作
rm -rf /test
mkdir /test
cd /test
git clone git@192.168.200.70:/home/git/repos/jenkinsfile
ls
cd jenkinsfile/
ls
ls itemA/
通過流水線腳本生成器生成如下腳本內容
vim itemA/jenkinsfile-php-wp
cat itemA/jenkinsfile-php-wp
node ("PHP-slave1-192.168.200.101") {
stage('git checkout') {
checkout([$class: 'GitSCM',
branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [],
submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/wordpress']]])
}
stage('code copy') {
sh '''rm -rf ${WORKSPACE}/.git
[ -d /data/backup ] || mkdir -p /data/backup
mv /usr/share/nginx/html/www.yunjisuan.com /data/backup/www.yunjisuan.com-$(date +%F_%T)
cp -rf ${WORKSPACE} /usr/share/nginx/html/www.yunjisuan.com'''
}
stage('test') {
sh 'curl http://www.yunjisuan.com/status.html'
}
}
推送到Git遠程倉庫
git add *
git commit -m "xxx"
git push -u origin master
通過流水線腳本生成器生成的階段代碼示例(這里只生成了url部分,具體內容自定義)
在Web服務器上PHP項目代碼里增加Pipeline驗證用的測試頁面
在項目代碼里加入一個健康檢查測試頁面,並推送到遠程Git倉庫
cd /var/lib/jenkins
git clone git@192.168.200.192:/home/git/repos/wordpress
ls
cd wordpress/
ls
echo "OK-version V2.0" >> status.html
將測試用頁面提交到遠程Git倉庫
git add *
git commit -m "version V2.0"
git config --global user.email "112340"
git config --global user.name "Mr.sun"
git push -u origin master
在web服務器做域名映射(因為要進行curl驗證)
echo "`hostname -I` www.yunjisuan.com" >> /etc/hosts
cat /etc/hosts
瀏覽器訪問jenkins進行PHP項目流水線發布構建
圖片說明
流水線項目發布多節點需要注意的事項
所需要修改的部分如下
腳本需要在流水線腳本寫出多節點,比如
node ("PHP-slave1-192.168.200.101") {
stage('git checkout') {
checkout([$class: 'GitSCM',
branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [],
submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/wordpress']]])
}
stage('code copy') {
sh '''rm -rf ${WORKSPACE}/.git
[ -d /data/backup ] || mkdir -p /data/backup
mv /usr/share/nginx/html/www.yunjisuan.com /data/backup/www.yunjisuan.com-$(date +%F_%T)
cp -rf ${WORKSPACE} /usr/share/nginx/html/www.yunjisuan.com'''
}
stage('test 200.101-php') {
sh 'curl http://www.yunjisuan.com/status.html'
}
}
node ("PHP-slave1-192.168.200.xxx") {
stage('git checkout') {
checkout([$class: 'GitSCM',
branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [],
submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/wordpress']]])
}
stage('code copy') {
sh '''rm -rf ${WORKSPACE}/.git
[ -d /data/backup ] || mkdir -p /data/backup
mv /usr/share/nginx/html/www.yunjisuan.com /data/backup/www.yunjisuan.com-$(date +%F_%T)
cp -rf ${WORKSPACE} /usr/share/nginx/html/www.yunjisuan.com'''
}
stage('test' 200.xxx-php) {
sh 'curl http://www.yunjisuan.com/status.html'
}
}
假如是克隆的機器需要把hosts文件里的映射修改一下
在瀏覽器進入jenkins主用戶來添加新節點信息(有幾點節點就添加幾個)
所有的修改都完事以后,在進行構建測試就可以了
讓ssh支持流水線,需要安裝插件SSH Pipeline Steps
以ssh方式分發流水線腳本模板詳解
def remote = [:] #定義變量不需要改
remote.name = 'test' #遠程主機的名字
remote.host = 'test.domain.com' #遠程主機的IP
remote.user = 'root' #遠程主機的用戶
remote.password = 'password' #遠程主機的密碼
remote.allowAnyHosts = true #照着超就可以
stage('Remote SSH') { #階段名稱
writeFile file: 'abc.sh', text: 'ls -lrt' #把后面的命令,讓如abc.sh腳本里
sshScript remote: remote, script: "abc.sh" #把腳本分發自定義的主機
}
以ssh方式分發流水線腳本語法樣式
node ("PHP-slave1-192.168.200.101") {
def remote = [:]
remote.name = 'test'
remote.user = 'root'
remote.allowAnyHosts = true
stage('git checkout') {
sh `echo "`hostname -I`"`
checkout([$class: 'GitSCM',
branches: [[name: '${branch}']],
doGenerateSubmoduleConfigurations: false, extensions: [],
submoduleCfg: [], userRemoteConfigs: [[credentialsId: '9f98962f-1a82-4da1-8a0f-bc906e92d998',
url: 'git@192.168.200.70:/home/git/repos/wordpress']]])
}
stage('SSH 200.xxx') {
remote.host = '192.168.200.xxx'
remote.password = 'linyaonie'
writeFile file: 'ssh01.sh', text:'echo "`hostname -I`"'
sshScript remote:remote, script: "ssh01.sh"
}
stage('SSH 200.xxx') {
remote.host = '192.168.200.xxx'
remote.password = 'linyaonie'
writeFile file: 'ssh02.sh', text:```
echo "`hostname -I`"
echo "`hostname -I`"
echo "`hostname -I`"
``` --->這里需要注意要定格寫
sshScript remote:remote, script: "ssh02.sh"
}
}
重新把新修過的內容添加到遠程倉庫並構建測試在查看logs日志的結果
git add *
git commit -m "測試SSH"
git push -u origin master