目錄
案例概述
案例環(huán)境
基于 Jenkins+Git+Docker 發(fā)布 Java 項目
1、配置 Git 倉庫
2、配置 Docker服務器
3、部署鏡像倉庫
5、配置Jenkins 環(huán)境
6、Jenkins 創(chuàng)建項目并發(fā)布測試
7、版本回滾
基于Jenkins+Git+Ansible 發(fā)布PHP 項目
1、部署PHP 運行環(huán)境
2、安裝Ansible 插件
3、上傳PHP 項目代碼到Git倉庫
4、Jenkins 創(chuàng)建項目并發(fā)布測試
案例概述
在之前的jenkins持續(xù)集成章節(jié)中主要實現(xiàn)的是jenkins的項目構建及部署。那在本章將結合新項目來實現(xiàn)自動化構建及發(fā)布,并將項目打包成鏡像上傳到私有倉庫,來實現(xiàn)一鍵發(fā)布和回滾等操作。
?
案例環(huán)境
操作系統(tǒng) |
IP地址 |
主機名 |
角色 |
CentOS7.5 |
192.168.50.51 |
git |
Git/Docker registry |
CentOS7.5 |
192.168.50.53 |
jenkins |
Jenkins/Docker |
CentOS7.5 |
192.168.50.54 |
docker |
Docker |
基于 Jenkins+Git+Docker 發(fā)布 Java 項目
所有主機上操作
[root@git ~]# setenforce 0
[root@git ~]# ?iptables -F
[root@git ~]# systemctl stop firewalld
[root@git ~]# systemctl stop NetworkManager
案例需求
開發(fā)者將開發(fā)完成的代碼提交到 Git 代碼版本倉庫后,點擊 Jenkins 任務按鈕自動拉取代碼編譯構建,并自動部署到 Web 服務器,用戶可訪問最新項目版本。
1、配置 Git 倉庫
Git 是一個開源的分布式版本控制系統(tǒng),可以有效、高速的處理從很小到非常大的項目版本管理, 是目前企業(yè)中使用最為廣泛的代碼版本管理系統(tǒng)。
1)使用 yum 安裝 Git 并配置
[root@git ~]# yum -y install git
[root@git ~]# useradd git
[root@git ~]# echo "123456" | passwd --stdin git
2)配置 Jenkins 主機免交互拉取 Git 倉庫
在 Jenkins 主機上創(chuàng)建密鑰對,將 id_rsa.pub 內(nèi)容追加到 Git 服務器上的 /home/git/.ssh/authorized_keys 文件中。
盡量所有主機互通
ssh-keygen
ssh-copy-id
3)在Git 服務器創(chuàng)建probe版本倉庫,一般對Git 的規(guī)范的方式要以.git 為后綴。如下:
[root@git ~]# ?su - git
上一次登錄:五 8月 25 15:09:12 CST 2023從 192.168.50.53pts/2 上
[git@git ~]$ ll
總用量 0
drwxrwxr-x. 7 git git 119 8月 ?25 13:57 probe.git
[git@git ~]$ ?cd probe.git
[git@git probe.git]$ ?git --bare init
重新初始化現(xiàn)存的 Git 版本庫于 /home/git/probe.git/
[git@git probe.git]$ ?exit
登出
4)從Github 拉取開源Java 博客系統(tǒng)psi-probe。
[root@git ~]# git clone https://github.com/psi-probe/psi-probe.git
[root@git ~]# cd psi-probe/
5)移除舊的推送地址,添加新的Git 提交地址。如下:
[root@git psi-probe]# git remote remove origin
[root@git psi-probe]# ?git remote add origin git@192.168.50.51:/home/git/probe.git
6)提交代碼到Git 倉庫并創(chuàng)建Tag。如下:
[root@git psi-probe]# ?touch psi-probe-web/src/main/webapp/a.html
[root@git psi-probe]# git add .
[root@git psi-probe]# ?git config --global user.email "13933914007@163.com"
[root@git psi-probe]# ?git config --global user.name "13933914007"
[root@git psi-probe]# git commit -m "a"
[master fbc21f1] a
?1 file changed, 0 insertions(+), 0 deletions(-)
?create mode 100644 psi-probe-web/src/main/webapp/a.html
7)創(chuàng)建標簽
[root@git psi-probe]# git tag 1.0.0
8)推送到Git 服務器。如下:
[root@git psi-probe]# ?git push origin 1.0.0
git@192.168.50.51's password:
2、配置 Docker服務器
1)安裝Docker,在所有主機上操作
[root@git psi-probe]# ?wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@git psi-probe]# ?yum -y install yum-utils device-mapper-persistent-data lvm2
[root@git psi-probe]# ?yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@git psi-probe]# ?yum -y install docker-ce
[root@git psi-probe]# ?systemctl start docker
[root@git psi-probe]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@git psi-probe]# docker version
2)git主機配置阿里云鏡像加速器
[root@git psi-probe]# ?cat << END > /etc/docker/daemon.json
> {
> ????????"registry-mirrors":[ "https://nyakyfun.mirror.aliyuncs.com" ]
> }
> END
[root@git psi-probe]# ?systemctl daemon-reload
[root@git psi-probe]# ?systemctl restart docker
3、部署鏡像倉庫
Docker Hub 作為 Docker 默認官方公共倉庫;用戶如果想自己搭建私有鏡像倉庫,官方提供了 registry 鏡像,使其搭建私有倉庫變的非常簡單。
1)在git部署docker私有倉庫
[root@git psi-probe]# docker run -d -v /opt/registry:/var/lib/registry -p 5000:5000 --restart=always --name registry registry
Unable to find image 'registry:latest' locally
latest: Pulling from library/registry
79e9f2f55bf5: Pull complete
0d96da54f60b: Pull complete
5b27040df4a2: Pull complete
e2ead8259a04: Pull complete
3790aef225b9: Pull complete
Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
Status: Downloaded newer image for registry:latest
fe35a0f4ddad47d4e4229e2e07be1adf1a54a89d4f1ef9dc972ae50c0d4033fc
[root@git psi-probe]# docker ps -l
CONTAINER ID ??IMAGE ?????COMMAND ??????????????????CREATED ????????STATUS ????????PORTS ??????????????????????????????????????NAMES
fe35a0f4ddad ??registry ??"/entrypoint.sh /etc…" ??4 seconds ago ??Up 3 seconds ??0.0.0.0:5000->5000/tcp, :::5000->5000/tcp ??registry
2)測試 registry 可用性
由于 Docker CLI 默認以 HTTPS 訪問,而部署的 registry 并未提供 HTTPS,因此,需要 在 pull 鏡像的 Docker 主機添加 HTTP 可信任。
Docker和jenkins主機上操作
[root@tomcat ~]# cat /etc/docker/daemon.json
{"insecure-registries":["192.168.50.51:5000"]}
[root@tomcat ~]# ?systemctl daemon-reload
[root@tomcat ~]# systemctl restart docker
3)打標簽并推送鏡像到 registry
[root@docker ~]# ?cat centos-7-x86_64.tar.gz | docker import - centos:7
sha256:f5ed290b86299517412d51da339bc680fa385788b396b510043da57447f83a90
[root@docker ~]# ?docker tag centos:7 192.168.50.51:5000/centos:7
[root@docker ~]# ?docker images
REPOSITORY ?????????????????TAG ??????IMAGE ID ??????CREATED ?????????SIZE
192.168.50.51:5000/centos ??7 ????????f5ed290b8629 ??21 seconds ago ??589MB
centos ?????????????????????7 ????????f5ed290b8629 ??21 seconds ago ??589MB
[root@docker ~]# docker push 192.168.50.51:5000/centos:7
The push refers to repository [192.168.50.51:5000/centos]
c664eb1dcbf0: Pushed
7: digest: sha256:877b17597fb597ea8a401f7e14b86ac5cf2d818dadfa9786f8f07dcd1fa93d26 size: 529
4)查看 registry 上傳的鏡像:
[root@docker ~]# curl http://192.168.50.51:5000/v2/_catalog
{"repositories":["centos"]}
[root@docker ~]# curl http://192.168.50.51:5000/v2/centos/tags/list
{"name":"centos","tags":["7"]}
5)從 registry 下載鏡像:
[root@docker ~]# docker rmi 192.168.50.51:5000/centos:7
Untagged: 192.168.50.51:5000/centos:7
Untagged: 192.168.50.51:5000/centos@sha256:877b17597fb597ea8a401f7e14b86ac5cf2d818dadfa9786f8f07dcd1fa93d26
[root@docker ~]# docker pull 192.168.50.51:5000/centos:7
7: Pulling from centos
Digest: sha256:877b17597fb597ea8a401f7e14b86ac5cf2d818dadfa9786f8f07dcd1fa93d26
Status: Downloaded newer image for 192.168.50.51:5000/centos:7
192.168.50.51:5000/centos:7
[root@docker ~]# docker images
REPOSITORY ?????????????????TAG ??????IMAGE ID ??????CREATED ????????SIZE
192.168.50.51:5000/centos ??7 ????????f5ed290b8629 ??4 minutes ago ??589MB
centos ?????????????????????7 ????????f5ed290b8629 ??4 minutes ago ??589MB
6)構建 Tomcat 基礎鏡像
在jenkins和docker主機上安裝JDK:? 如果是跟著上一章做的就不用安裝了
[root@docker ~]# tar xf jdk-8u191-linux-x64.tar.gz
[root@docker ~]# mv jdk1.8.0_191/ /usr/local/java[root@docker ~]# ls -l apache-tomcat-8.5.16.tar.gz
[root@docker ~]# cat Dockerfile
FROM centos:7
MAINTAINER crushlinux
ENV VERSION=8.5.40
ENV JAVA_HOME /usr/local/java
ADD ./apache-tomcat-${VERSION}.tar.gz /tmp
RUN cd /tmp && \
????mv apache-tomcat-${VERSION} /usr/local/tomcat && \
????rm -rf apache-tomcat-${VERSION}.tar.gz /usr/local/tomcat/webapps/* && \
????mkdir /usr/local/tomcat/webapps/ROOT
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
[root@docker ~]# docker build -t 192.168.50.51:5000/tomcat-85 ?.
[+] Building 0.9s (8/8) FINISHED docker:default
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 481B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 0.0s
=> [internal] load build context 0.1s
=> => transferring context: 9.69MB 0.1s
=> CACHED [1/3] FROM docker.io/library/centos:7 0.0s
=> [2/3] ADD ./apache-tomcat-8.5.40.tar.gz /tmp 0.2s
=> [3/3] RUN cd /tmp && mv apache-tomcat-8.5.40 /usr/local/tomcat && rm -rf apache-tomc 0.5s
=> exporting to image 0.1s
=> => exporting layers 0.1s
=> => writing image sha256:a80be73edf055650e653496c1429f2a290d8a566314e5d8a461f950cc7ab987f 0.0s
=> => naming to 192.168.50.51:5000/tomcat-85 0.0s
[root@docker ~]# docker images
REPOSITORY ????????????????????TAG ??????IMAGE ID ??????CREATED ????????SIZE
192.168.50.51:5000/tomcat-85 ??latest ???a80be73edf05 ??9 seconds ago ??612MB
centos ????????????????????????7 ????????f5ed290b8629 ??7 minutes ago ??589MB
192.168.50.51:5000/centos ?????7 ????????f5ed290b8629 ??7 minutes ago ??589MB
7)構建鏡像并上傳到registry:
[root@docker ~]# docker push 192.168.50.51:5000/tomcat-85
Using default tag: latest
The push refers to repository [192.168.50.51:5000/tomcat-85]
39c535c3d2d5: Pushed
1eb2af1ce749: Pushed
c664eb1dcbf0: Mounted from centos
latest: digest: sha256:a885cea844e2b611328a26eeed5b36b150795632534b0e4e75efc8c593ba6b83 size: 952
[root@docker ~]# docker ps -a
CONTAINER ID ??IMAGE ???????????????????????????COMMAND ??????????????????CREATED ????????STATUS ???????????????????????PORTS ??????????????????????????????????????NAMES
5926243c91f6 ??192.168.50.51:5000/probe:1.0.0 ??"/usr/local/tomcat/b…" ??2 minutes ago ??Up 2 minutes ?????????????????0.0.0.0:8090->8080/tcp, :::8090->8080/tcp ??probe
5、配置Jenkins 環(huán)境
Jenkins 是一個開源軟件項目,是基于 Java 開發(fā)的一種持續(xù)集成工具,用于代碼編譯、 部署、測試等工作。 Jenkins 也是一個跨平臺的集成工具,大多數(shù)主流的平臺都支持,而且安裝很簡單,這里將以部署 war 包方式安裝。
CSDN過程太多這里有詳細安裝基礎環(huán)境配置
1)修改jenkins運行用戶
[root@jemkins ~]# vim /usr/lib/systemd/system/jenkins.service
?34 User=root
?35 Group=root
一般在/etc/init.d/jenkins或/usr/lib/systemd/system/jenkins.service。根據(jù)不同的Linux發(fā)行版可能略有不同,請根據(jù)實際情況進行查找。
2)Jenkins 配置全局工具配置
配置 Maven、JDK、Git 環(huán)境
在 Jenkins 首頁中點擊“Manage Jenkins”->“Global Tool Configuration”->“JDK” ->新增“JDK”,設置 JDK 別名為”JDK17”。去掉“Install automatically”選項,設置 “JAVA_HOME”為本案例中 JDK 實際安裝路徑。
?在Jenkins安裝maven
[root@jemkins ~]#? tar xf apache-maven-3.5.0-bin.tar.gz
[root@jemkins ~]# mv apache-maven-3.5.0 /usr/local/maven-3.5.0
[root@jemkins ~]# vim /usr/local/maven-3.5.0/conf/settings.xml
146?? <mirrors>
147???? <mirror>
148???????? <id>nexus-aliyun</id>
149???????? <mirrorOf>central</mirrorOf>
150???????? <name>Nexus aliyun</name>
151???????? <url>http://maven.aliyun.com/nexus/content/groups/public</url>
152???? </mirror>
153?? </mirrors>
?剛剛界面中找到 Maven 配置選項,然后點擊“新增 Maven”并設置別名為“Maven3.5”。
以上全局基本配置完畢后,點擊保存即可完成。
配置SSH 插件
創(chuàng)建一個用于連接Docker主機的憑據(jù)。主頁面-> 憑據(jù)-> 系統(tǒng)-> 全局憑據(jù)-> 添加憑據(jù)。
?輸入連接Docker 主機的用戶名和密碼
?
第二步:添加SSH 遠程主機。Manage Jenkins-> Configure System。最下面
?
6、Jenkins 創(chuàng)建項目并發(fā)布測試
- 主頁面-> 新建Item-> 輸入任務名稱,構建一個Maven 項目
注意:如果沒有顯示“構建一個Maven 項目”選項,需要在管理插件里安裝“Maven
Integration”插件。
- 配置Git 參數(shù)化構建
?3)動態(tài)獲取Git 倉庫tag,與用戶交互選擇Tag 發(fā)布
?
4)指定項目Git 倉庫地址
5)修改*/master 為$Tag,Tag 是上面動態(tài)獲取的變量名,表示根據(jù)用戶選擇打代碼版本
?
6)設置maven 構建命令選項“clean package -Dmaven.test.skip=true”
?
7)利用pom.xml 文件構建項目。在Jenkins 本機鏡像構建與推送到鏡像倉庫,并SSH遠程連接到Docker 主機使用推送的鏡像創(chuàng)建容器
選擇Post Steps –>Add post-build step ->執(zhí)行shell,這樣會調出第一個輸入框
如果沒有就去下ssh插件
?
選擇Post Steps –>Execute shell script on remote host using ssh,這樣會調出第二個輸入框。
?
?第一個命令框內(nèi)容
REPOSITORY=192.168.50.51:5000/probe:${tag}
# 構建鏡像
cat > Dockerfile << EOF
FROM 192.168.50.51:5000/tomcat-85:latest
RUN rm -rf /usr/local/tomcat/webapps/ROOT
COPY psi-probe-web/target/*.war /usr/local/tomcat/webapps/ROOT.war
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
EOF
docker build -t $REPOSITORY ./
# 上傳鏡像
docker push $REPOSITORY
第二個命令框內(nèi)容
REPOSITORY=192.168.50.51:5000/probe:${tag}
# 部署
docker rm -f probe |true
docker images rm $REPOSITORY|true
docker container run -d --name probe -v /usr/local/java:/usr/local/java -p 8090:8080 $REPOSITORY
[root@docker ~]# vim /etc/docker/daemon.json
{"insecure-registries":["192.16850.51:5000"]}
[root@docker ~]# systemctl daemon-reload
[root@docker ~]# systemctl restart docker
注意:從部署命令當中可以看到最后一行容器名稱是probe,暴露宿主機端口8090,即使用宿主機IP:8090 就可以訪問probe項目。
8)probe項目已配置完成,開始構建
9)選擇tag,開始構建。
?
?10)在任務控制臺輸出構建日志的開始信息
在任務控制臺輸出構建日志的結束信息
?11)執(zhí)行成功后,瀏覽器訪問測試probe項目:http://192.168.50.54:8090。
7、版本回滾
回滾(Rollback)指的是程序或數(shù)據(jù)處理錯誤,將程序或數(shù)據(jù)恢復到上一次正確版本?;貪L包括程序回滾和數(shù)據(jù)回滾等類型。對于本章而言,如果當前代碼版本發(fā)布失敗,回滾到上一個正常版本,盡快恢復業(yè)務。
回滾思路:重新run 指定老版本鏡像。因為在每次發(fā)版過程中需要將代碼封裝到鏡像中,并打上Tag,以此用來區(qū)分不同的版本。比如在發(fā)版過程中出現(xiàn)代碼問題,需要運維工程師快速回滾到上一個版本,這樣可以做到快速恢復業(yè)務正?;M瑯舆@個過程該怎樣去實現(xiàn)呢?此時,可以重新創(chuàng)建一個”自由軟件項目風格任務“,通過參數(shù)化構建特定版本,也就是指定上一個正常版本鏡像,然后重新執(zhí)行創(chuàng)建容器命令即可回到之前正常的版本。
提交1.0.1版本代碼
[root@git psi-probe]# echo "test1" > psi-probe-web/src/main/webapp/test1.html
[root@git psi-probe]# git add .
[root@git psi-probe]# git commit -m "test1"
[master 61bc58b] test1
?1 file changed, 1 insertion(+)
?create mode 100644 psi-probe-web/src/main/webapp/test1.html
[root@git psi-probe]# git tag 1.0.1
[root@git psi-probe]# git push origin 1.0.1
git@192.168.50.51's password:
Counting objects: 12, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 575 bytes | 0 bytes/s, done.
Total 7 (delta 2), reused 0 (delta 0)
To git@192.168.50.51:/home/git/probe.git
?* [new tag] ????????1.0.1 -> 1.0.1
提交1.0.2版本代碼
[root@git psi-probe]# ?echo "test2" > psi-probe-web/src/main/webapp/test2.html
[root@git psi-probe]# git add .
[root@git psi-probe]# git commit -m "test2"
[master a588d47] test2
?1 file changed, 1 insertion(+)
?create mode 100644 psi-probe-web/src/main/webapp/test2.html
[root@git psi-probe]# git tag 1.0.2
[root@git psi-probe]# git push origin 1.0.2
git@192.168.50.51's password:
Counting objects: 12, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 576 bytes | 0 bytes/s, done.
Total 7 (delta 2), reused 0 (delta 0)
To git@192.168.50.51:/home/git/probe.git
?* [new tag] ????????1.0.2 -> 1.0.2
發(fā)布1.0.1版本
查看
[root@14af7248a99a /]# ?ls /usr/local/tomcat/webapps/ROOT
META-INF ?WEB-INF ?a.html ?css ?flags ?index.jsp ?js ?test1.html
[root@14af7248a99a /]# [root@docker psi-probe]#
1) 創(chuàng)建一個自由軟件項目風格任務
?
?
?
REPOSITORY=192.168.50.51:5000/probe:${tag}
# 部署
docker rm -f probe |true
docker container run -d --name probe -v /usr/local/java:/usr/local/java -p 8090:8080 $REPOSITORY
回滾到1.0.0版本
?
[root@docker psi-probe]# docker exec -it probe /bin/bash
查看
[root@afc103977891 /]# ?ls /usr/local/tomcat/webapps/ROOT
META-INF ?WEB-INF ?a.html ?css ?flags ?index.jsp ?js
[root@docker psi-probe]# ?docker ps -l
CONTAINER ID ??IMAGE ???????????????????????????COMMAND ??????????????????CREATED ?????????STATUS ?????????PORTS ??????????????????????????????????????NAMES
afc103977891 ??192.168.50.51:5000/probe:1.0.0 ??"/usr/local/tomcat/b…" ??36 seconds ago ??Up 35 seconds ??0.0.0.0:8090->8080/tcp, :::8090->8080/tcp ??probe
基于Jenkins+Git+Ansible 發(fā)布PHP 項目
1、部署PHP 運行環(huán)境
PHP 是一個動態(tài)程序,負責解析PHP-FPM 服務,而這個服務不支持靜態(tài)頁面處理,一般結合Nginx 解決這個問題。Nginx 本身是一個靜態(tài)Web 服務器,并不支持解析PHP 程序,但它支持了FastCGI 接口來調用動態(tài)服務來解析PHP 程序。
當客戶端請求PHP 頁面時,Nginx 通過fastcgi 接口轉發(fā)給本地9000 端口的PHP-FPM子進程處理,處理完成后返回Nginx。
1)安裝Nginx
配置Nginx 網(wǎng)絡源
[root@docker psi-probe]# ?vim /etc/yum.repos.d/nginx.repo
[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
~ ???????????
安裝并啟動
[root@docker bak]# ?yum -y install nginx
[root@docker bak]# ?systemctl start nginx
2)安裝PHP
安裝php 依賴的第三方庫,命令如下:
[root@docker bak]# yum -y install gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel gcc openssl-*
編譯安裝php
[root@docker ~]# ?tar xf php-5.6.39.tar.gz -C /usr/src/
[root@docker ~]# cd /usr/src/php-5.6.39/
[root@docker php-5.6.39]# ?./configure --prefix=/usr/local/php \
> --with-config-file-path=/usr/local/php/etc \
> --with-mysql --with-mysqli --with-openssl --with-zlib \
> --with-curl --with-gd --with-jpeg-dir --with-png-dir \
> --with-iconv --enable-fpm --enable-zip --enable-mbstring && make -j 2 && make install
配置php-fpm,命令如下:
[root@docker php-5.6.39]# ?cp php.ini-production /usr/local/php/etc/php.ini
[root@docker php-5.6.39]# ?cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@docker php-5.6.39]# ?vim /usr/local/php/etc/php-fpm.conf
149 user = nginx
150 group = nginx
[root@docker php-5.6.39]# cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
[root@docker php-5.6.39]# chmod +x /etc/rc.d/init.d/php-fpm
[root@docker php-5.6.39]# ?service php-fpm start
Starting php-fpm ?done
3)Nginx 代理PHP
添加虛擬主機配置如下:
[root@docker php-5.6.39]# ?vim /etc/nginx/conf.d/default.conf
server {
????listen ??????80;
????server_name ?localhost;
????#access_log ?/var/log/nginx/host.access.log ?main;
????location / {
????????root ??/usr/share/nginx/html;
????????index ?index.html index.htm index.php;
????}
????location ~ \.php$ {
????????root /usr/share/nginx/html;
????????fastcgi_pass 127.0.0.1:9000;
????????fastcgi_index index.php;
????????fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
????????include fastcgi_params;
}
[root@docker php-5.6.39]# systemctl restart nginx
2、安裝Ansible 插件
1)主頁面-> 系統(tǒng)管理->管理插件
?
?3) 點擊”直接安裝”按鈕。只安裝Ansible 插件是不行的,還要在Jenkins 主機上安裝ansible:
[root@jemkins ~]# yum -y install ansible
[root@jemkins yum.repos.d]# ?vim /etc/ansible/hosts
?20 ?[webservers]
?21 192.168.50.54
其中:?[webserver]表示W(wǎng)eb 主機分組名稱,該分組下可以寫多個主機,也可以寫多個分組區(qū)分不同角色服務器。
3、上傳PHP 項目代碼到Git倉庫
1)在Git 服務器創(chuàng)建wordpress 版本倉庫
[root@git ~]# ?su - git
上一次登錄:日 8月 27 18:31:05 CST 2023pts/0 上
[git@git ~]$ mkdir wordpress.git
[git@git ~]$ cd wordpress.git
[git@git wordpress.git]$ git --bare init
初始化空的 Git 版本庫于 /home/git/wordpress.git/
[git@git wordpress.git]$ exit
登出
2)下載開源PHP 博客系統(tǒng)wordpress
[root@git ~]# wget https://wordpress.org/latest.tar.gz
[root@git ~]# ?tar xf latest.tar.gz
[root@git ~]# cd wordpress/
3)提交到Git 倉庫
[root@git wordpress]# ?git init
初始化空的 Git 版本庫于 /root/wordpress/.git/
[root@git wordpress]# git remote add origin git@192.168.50.51:/home/git/wordpress.git
[root@git wordpress]# git add .
[root@git wordpress]# ?git commit -m "wp"
?[root@git wordpress]# git tag 1.0.0
[root@git wordpress]# git push origin 1.0.0
git@192.168.50.51's password:
Counting objects: 3171, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3091/3091), done.
Writing objects: 100% (3171/3171), 19.79 MiB | 11.52 MiB/s, done.
Total 3171 (delta 410), reused 0 (delta 0)
To git@192.168.50.51:/home/git/wordpress.git
?* [new tag] ????????1.0.0 -> 1.0.0
4、Jenkins 創(chuàng)建項目并發(fā)布測試
1)主頁面-> 新建任務-> 創(chuàng)建一個自由軟件項目風格任務
2)動態(tài)獲取Git 倉庫tag,與用戶交互選擇Tag 發(fā)布,?
?
?3)修改*/master 為$tag。tag 是上面動態(tài)獲取的變量名,指定項目Git 倉庫地址。
?
4)使用ansible 插件,選擇Invoke Ansible Ad-Hoc Command?
?
5)使用synchronize 模塊同步數(shù)據(jù)
src=${WORKSPACE} dest=/usr/share/nginx/html rsync_opts=--exclude=.git
創(chuàng)建兩個
?6)使用raw模塊校正項目權限
?還是做號ssh互通
[root@git wordpress]# ssh 192.168.50.54
Last login: Sun Aug 27 14:54:04 2023 from 192.168.50.1
[root@docker ~]# exit
登出
Connection to 192.168.50.54 closed.
7)主頁面-> 右擊blog-wordpress -> Build with Parameters
?
?
安裝rsync命令。因為Ansible synchronize模塊底層使用rsync命令來進行文件同步,所以需要在目標主機上安裝rsync?
?sudo yum ?update && sudo yum install -y rsync
?突然發(fā)現(xiàn)顯示版本過低? 不過不影響實驗完整度
?10)模擬實際生產(chǎn)環(huán)境提交代碼,作用是可以清楚看到兩次發(fā)版代碼的不同。
[root@git ~]#? cd wordpress/
[root@git wordpress]# echo "Hello Wordpress" > test.html
11)將修改的test.html 提交到Git 倉庫
[root@git wordpress]#? git add .
[root@git wordpress]# git commit -m "hw"
[master c3bc26e] hw
?1 file changed, 1 insertion(+)
?create mode 100644 test.html
[root@git wordpress]# git tag 1.0.1
[root@git wordpress]# git push origin 1.0.1
git@192.168.50.51's password:
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 270 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To git@192.168.50.51:/home/git/wordpress.git
?* [new tag]???????? 1.0.1 -> 1.0.1
?12)在Jenkins 執(zhí)行構建任務
發(fā)布成功后,訪問: http://192.168.50.54/blog-wordpress/test.html,頁面顯示“Hello Wordpress!”。說明剛修改的代碼已發(fā)布成功!
文章來源:http://www.zghlxwxcb.cn/news/detail-776279.html
?拜拜。。。。文章來源地址http://www.zghlxwxcb.cn/news/detail-776279.html
到了這里,關于基于Jenkins CICD的代碼發(fā)布與回滾 Jenkins+Git+Docker 發(fā)布 Java 項目 Jenkins+Git+Ansible 發(fā)布PHP 項目的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!