coding持續(xù)集成配置

前端推送倉庫后,觸發(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/

  1. 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)境變量設置文件,整理出如下加載情況表:


1.png

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
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • 用戶的創(chuàng)建流程: 1.useradd創(chuàng)建用戶時,系統(tǒng)會以/etc/login.defs、/etc/defaults...
    散作滿河星y閱讀 349評論 0 0
  • 1.如何為用戶設定密碼,又如何修改密碼(root才能執(zhí)行) 普通用戶只允許變更自己的密碼,無法修改其他人密碼,并且...
    生活一場戲而已_b526閱讀 293評論 0 0
  • 1、為用戶添加密碼(root用戶才能執(zhí)行) 創(chuàng)建用戶后,如果需要使用該用戶登錄系統(tǒng)則需要為用戶設定密碼。 ...
    青衫憶笙666閱讀 448評論 0 0
  • 添加用戶密碼(只有root才能執(zhí)行) Passwd設定密碼 [root@oldboy ~]# useradd w...
    Lifeisjustafarc閱讀 385評論 0 0
  • 問題1: 在創(chuàng)建用戶密碼的時候,會一直提示,若不想提示的時候,可以將這個它放進黑洞里。 問題2:如果我在刪除一個用...
    范小雷閱讀 283評論 0 0

友情鏈接更多精彩內(nèi)容