|NO.Z.00040|——————————|CloudNative|——|CI/CD&Jenkins_pipeline操作.V14|-------------------------------------------|Jenkins_pipeline自動化釋出PHP專案|
阿新 • • 發佈:2022-03-28
[CloudNative:CI/CD&Jenkins_pipeline操作.V14] [Applications.CloudNative] [|Jenkins pipeline持續部署PHP專案|環境為lnmp環境|]
一、Jenkins pipeline自動化釋出PHP專案
### --- 環境說明 ~~~ ——>環境為:lnmp ~~~ ——>PHP專案:wordpress(此處下載一個wordpress的原始碼將其模擬為我們的程式碼上傳到我們的git倉庫)
### --- 在git伺服器建立儲存庫
[git@git ~]$ mkdir -p repos/wordpress.git
[git@git repos]$ cd repos/wordpress.git/
[git@git wordpress.git]$ git --bare init
[git@git wordpress.git]$ ls -al
### --- 在jenkins伺服器端配置 [root@jenkins ~]# git clone [email protected]:/home/git/repos/wordpress.git [root@jenkins ~]# tar -zxvf wordpress-5.3.2.tar.gz
[root@jenkins ~]# cd wordpress/
[root@jenkins wordpress]# mv /root/wordpress/wordpress/* .
[root@jenkins wordpress]# git add .
[root@jenkins wordpress]# git commit -m 'wordpress'
[root@jenkins wordpress]# git push -u origin master
二、部署node節點:### --- 輔助:clone wordpress到本地伺服器 [root@jenkins ~]# git config --global user.name "Administrator" [root@jenkins ~]# git config --global user.email '[email protected]' [root@jenkins ~]# git status [root@jenkins ~]# git clone [email protected]:plat-sp/wordpress.git
### --- 部署node節點:node節點需要在系統管理中配置
~~~ 建立job
~~~ 引數化構建
~~~ 配置pipeline指令碼 (直接配置或者git獲取)
~~~ 儲存
三、建立Jenkinsfile
### --- 接下里編寫jenkinsfile檔案:jenkinsfile-PHP:原始碼檔案:
node ("jenkins-slave2") {
stage('git checkout') { checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: '[email protected]:/home/git/repos/wordpress.git']]])
}
swtage('code copy') {
sh '''rm -rf ${WORKSPACE}/.git
mkdir -p /data/backup/web-$(date +"%F")
mv /home/wwwroot/default/* /data/backup/web-$(date +"%F")
cp -rf ${WORKSPACE}/* /home/wwwroot/default/'''
}
stage('test') {
sh "curl http://10.10.10.12/status.html"
}
}
node ("slave01-10.10.10.11") {
stage('git checkout') {
checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: '[email protected]:/home/git/repos/wordpress.git']]])
}
stage('code copy') {
sh '''rm -rf ${WORKSPACE}/.git
mv /usr/share/nginx/html/wp.com /data/backup/wp.com-$(date +"%F_%T")
cp -rf ${WORKSPACE} /usr/share/nginx/html/wp.com'''
}
stage('test') {
sh "curl http://10.10.10.11/status.html"
}
}
### --- Jenkinsfile.php配置檔案說明
node ("slave01-172.16.1.2") { # 繫結到該節點去執行
stage('git checkout') { # 拉程式碼
checkout([$class: 'GitSCM', branches: [[name: '${branch}']], doGenerateSubmoduleConfigurations: false, extensions: [], su
bmoduleCfg: [], userRemoteConfigs: [[url: '[email protected]:/home/git/repos/wordpress']]])
}
# $class: 'GitSCM' git外掛獲取程式碼的工具
# branches 分支名稱
# doGenerateSubmoduleConfigurations 是否包含子模組配置
# extensions 擴充套件功能
# submoduleCfg 子模組配置
# userRemoteConfigs 遠端使用者配置(倉庫地址)
stage('code copy') { # 複製程式碼
sh '''rm -rf ${WORKSPACE}/.git # 刪除拉下來的專案的.git敏感檔案
mv /usr/share/nginx/html/wp.com /data/backup/wp.com-$(date +"%F_%T") # 備份舊檔案
cp -rf ${WORKSPACE} /usr/share/nginx/html# 新檔案複製到站點目錄
}
stage('test') { # 測試
sh "curl http://wp.test.com/status.html"
}
}
四、執行構建PHP專案### --- 執行構建PHP專案
~~~ ——>構建概覽:
~~~ ——>控制檯輸出詳情:
~~~ ——>可以看到每一步的執行詳情,最後也是成功的!!!!!!
~~~ ——>此外我們可以檢視伺服器及網頁實際體驗效果進行驗證。
===============================END===============================
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart ——W.S.Landor
來自為知筆記(Wiz)