1. 程式人生 > 其它 >coding自動構建前端專案,推送遠端伺服器

coding自動構建前端專案,推送遠端伺服器

coding當中有自動構建前端專案,然後推送的騰訊的物件儲存上面的模板,這個就不多說了,只需要填寫相關引數就可以構建成功了

在coding當中,先進入專案,然後左側選單裡面選擇 構建計劃 ,最後選擇 自定義構建過程 ,然後選擇程式碼倉庫

該方式需要伺服器先支援ssh訪問

 

 看以上截圖框起來的地方,這個地方是自定義構建過程的地方

然後進入文字編輯器,如下

 

 然後把stage修改為 ,後面我會解釋具體的用法

   stage('Node.js') {
      steps {
        sh 'rm -rf /usr/lib/node_modules/npm/'
        dir
('/root/.cache/downloads') { sh 'wget -nc "https://coding-public-generic.pkg.coding.net/public/downloads/node-linux-x64.tar.xz?version=v14.18.2" -O node-v14.18.2-linux-x64.tar.xz | true' sh 'tar -xf node-v14.18.2-linux-x64.tar.xz -C /usr --strip-components 1' } sh 'node -v' } } stage(
'安裝依賴') { steps { sh 'npm install' } } stage('編譯') { steps { sh 'npm run build' } } stage('構建') { steps { echo '構建中...' sh 'pwd' sh 'tar -zcf /tmp/tmp.tar.gz -C /root/workspace/mltr .' echo '構建完成.' } } stage(
'部署') { steps { echo '部署中...' script { def remote = [:] remote.name = 'CenOs' remote.allowAnyHosts = true remote.host = 'xxx.xx.xx.xxx' remote.port = 22 remote.user = 'root' // 把「CODING 憑據管理」中的「憑據 ID」填入 credentialsId,而 id_rsa 無需修改 withCredentials([sshUserPrivateKey(credentialsId: "5ad2bb53-b46c-4e4e-a0a2-6c065453c06d", keyFileVariable: 'id_rsa')]) { remote.identityFile = id_rsa // SSH 上傳檔案到遠端伺服器 sshPut remote: remote, from: '/tmp/tmp.tar.gz', into: '/tmp/' // 解壓縮 sshCommand remote: remote, sudo: true, command: "mkdir -p /docker/nginx/www/xxx" sshCommand remote: remote, command: "tar -zxf /tmp/tmp.tar.gz -C /docker/nginx/www/xxx" } } echo '部署完成' } }

1、stage nodejs是先設定node版本為前端指定的執行版本,如果不需要可以刪除該步驟

    如果改版本沒有的話可以下載其他版本,具體的可以檢視coding使用幫助,或者可以試一下把後面的下載地址和名字都改成node官網的地址,估計也可以不過我沒有進行測試

2、至於 安裝依賴 和 編譯 兩步驟就不用多說了,這個就看你自己前端專案的命令了

   如果需要使用淘寶進行的話,只需要 新增設定淘寶映象的命令即可,至於淘寶映象的命令就自行百度吧

3、最後的構建,其中xxx一般是指build的資料夾,如果npm build之後的檔案家是build的話

shpwd//其實是linux命令,顯示當前所在的資料夾,用於除錯

sh 'tar -zcf /tmp/tmp.tar.gz -C /root/workspace/xxx .'  //壓縮xxx資料夾下的指定目錄的所有檔案到tmp壓縮包,注意後面有一個點

4、部署中

  更改host、port、user為自己伺服器的,當然伺服器要支援ssh登入,至於 credentialsId 需要在螢幕左下角 專案設定 裡面 開發者選項->憑證管理 新建ssh填寫自己伺服器的祕鑰

  由於我用的是騰訊雲,所以進入控制檯-應用伺服器-祕鑰新建,會讓你下載祕鑰到本地,然後儲存起來後面會有用

  進入伺服器管理介面,有一個金鑰對,新增繫結

  在coding裡面新建的憑證id貼上到上面的jenkins命令裡面即可。

  在使用已上命令時,一定要注意解壓的位置,具體要看你想部署到伺服器的那個位置

 sshCommand remote: remote, sudo: true, command: "mkdir -p /docker/nginx/www/xxx"     #在docker/nginx/www下建立xxx目錄,由於我的nginx配置到這個目錄下,所以就解壓到該目錄下
 sshCommand remote: remote, command: "tar -zxf /tmp/tmp.tar.gz -C /docker/nginx/www/xxx"    #然後把tmp壓縮檔案解壓到xxx目錄

 

最後我是通過Xshell客戶端然後使用ssh方式登入伺服器的,由於伺服器綁定了ssh祕鑰,所以需要使用剛剛下載的祕鑰登入才可以

 

如果需要docker部署nginx的教程,可以看我其他的帖子