前端推送倉庫后,觸發(fā)jenkins部署到云服務器
文件如下
pipeline {
agent any
stages {
stage('檢出') {
steps {
checkout([
$class: 'GitSCM',
branches: [[name: GIT_BUILD_REF]],
userRemoteConfigs: [[
url: GIT_REPO_URL,
credentialsId: CREDENTIALS_ID
]]])
}
}
stage('打包') {
steps {
echo '打包壓縮開始'
sh '''npm i
npm run build
cd ./dist
tar -zcvf dist.tar.gz *
echo 已生成壓縮包'''
}
}
stage('傳輸文件到服務器') {
steps {
echo '上傳...'
script {
def remote = [:]
remote.name = 'web-server'
remote.allowAnyHosts = true
remote.host = '121.5.111.188'
remote.port = 22
remote.user = 'root'
// 把「CODING 憑據(jù)管理」中的「憑據(jù) ID」填入 credentialsId,而 id_rsa 無需修改
withCredentials([sshUserPrivateKey(credentialsId: "4c7da146-84", keyFileVariable: 'id_rsa')]) {
remote.identityFile = id_rsa
// 刪除文件
sshCommand remote: remote, command: "rm -rf /zyw/web/vision_admin/*"
// SSH 上傳文件到遠端服務器
sshPut remote: remote, from: './dist/dist.tar.gz', into: '/zyw/web/vision_admin/'
// 解壓縮
sshCommand remote: remote, command: "tar -zxvf /zyw/web/vision_admin/dist.tar.gz -C /zyw/web/vision_admin/"
sshCommand remote: remote, command: "rm -rf /zyw/web/vision_admin/dist.tar.gz"
}
}
echo '部署完成'
}
}
}
}
后端代碼推送到倉庫后,觸發(fā)coding構建,通過ssh登陸遠程云服務器,觸發(fā)shell執(zhí)行
def remote = [:]
remote.name = 'server'
remote.host = '121.5.111.188'
remote.user = 'root'
remote.password = 'z6*'
remote.allowAnyHosts = true
pipeline{
agent any
stages{
stage("任務申請"){
steps{
script{
sshCommand remote: remote, command: "/zyw/web/egg/auto.sh"
}
}
}
}
}
下面是踩坑經(jīng)歷,執(zhí)行shell讀不到環(huán)境變量問題
1.不認識node環(huán)境,需要把node路徑軟連接到/usr/sbin/
whereis node
/opt/node/bin/node
ln -s /opt/node/bin/node /usr/sbin/
2.不認識pm2命令,需要把pm2路徑軟連接到/usr/sbin/
ln -s /opt/node/bin/pm2 /usr/sbin/
- auto.sh拒絕執(zhí)行,權限不夠,需要給它加權限
chmod 777 -R auto.sh
下面是一些積淀
jenkins執(zhí)行shell讀不到/etc/profile以及用戶下.bash_profile設置的環(huán)境變量
是因為執(zhí)行機制造成的
什么是交互式shell和非交互式shell
交互式的shell會有一個輸入提示符,并且它的標準輸入、輸出和錯誤輸出都會顯示在控制臺上。這種模式也是大多數(shù)用戶非常熟悉的:登錄、執(zhí)行一些命令、退出。當你退出后,shell也終止了。
非交互式shell是bash script.sh這類的shell。在這種模式下,shell不與你進行交互,而是讀取存放在文件中的命令,并且執(zhí)行它們。當它讀到文件的結尾EOF,shell也就終止了。
什么是登錄式shell和非登陸式shell
需要輸入用戶名和密碼的shell就是登陸式shell。因此通常不管以何種方式登陸機器后用戶獲得的第一個shell就是login shell。不輸入密碼的ssh是公鑰打通的,某種意義上說也是輸入密碼的。
非登陸式的就是在登陸后啟動bash等,即不是遠程登陸到主機這種
如果一個bash是交互式登錄Shell或者使用--login參數(shù)的非交互式Shell,首先會執(zhí)行/etc/profile文件。然后按順序查找~/.bash_profile, /.bash_login,/.profile,這三個文件誰存在并且有讀權限就執(zhí)行誰,然后后面的就不會再執(zhí)行。
可以通過指定--noprofile參數(shù)來禁止這種默認行為。
當?shù)卿汼hell退出之后,bash會讀取~/.bash_logout文件并執(zhí)行。
如果 ~/.bash_profile文件存在的話,一般還會執(zhí)行 ~/.bashrc文件。
通過上面的分析,對于常用環(huán)境變量設置文件,整理出如下加載情況表:

jenkins默認情況下是屬于non-login + non-interactive
eggjs的npm run stop會停用所有eggjs服務,所以服務器上部署了多個egg服務的話,可以一次停用,多次啟用
cd /data/xiaokang/xiaokang-server
npm run stop
git checkout .
git pull
npm install
npm run start
cd /data/vision/egg
git checkout .
git pull
npm install
npm run start