一、原始的nodejs項目發布
1、scp、winscp上傳文件到服務器 - 把代碼拷到服務器即可
scp 命令:scp -r .\dist\ root@116.62.229.124:/usr/local/nginx/html
2、git拉取分支
通過分支控制拉取代碼拷貝到服務器即可
centOS 安裝git:yum install -y git
二、gulp腳本發布項目
一個自動話構建工具,可以使用它在項目開發過程中自動執行常見任務
基於Nodejs構建的,利用Nodejs流的威力,可以快速構建項目並減少頻繁的IO操作
通過配置gulp-ssh執行上傳任務即可
1 /** 2 * gulpfile.js 3 * 定義任務 4 * 1、刪除dist rm dist 5 * 2、上傳dist upload 6 */ 7 const fs = require('fs') 8 const gulp = require('gulp') 9 const gulpSSH = require('gulp-ssh') 10 11 const filePath = '/usr/local/nginx/html/dist' 12 const command = `rm -rf ${filePath}` 13 14 const config = { 15 host: '116.62.229.124', 16 port: 22, 17 username: 'root', 18 // privateKey: fs.readFileSync('C:/Users/pf184/.ssh/id_rsa') 19 password: '#服務器密碼#' 20 } 21 22 // eslint-disable-next-line new-cap 23 const gulpSsh = new gulpSSH({ 24 ignoreErrors: false, 25 sshConfig: config 26 }) 27 28 // 登錄遠程服務器,刪除dist文件 29 gulp.task('del', (done) => { 30 console.log(command) 31 gulpSsh.shell([command]) 32 done() 33 }) 34 35 // 上傳文件 36 // * 只能上傳一級目錄,不能長傳子文件夾 37 // ** 文件夾下所有內容 38 gulp.task('upload', (done) => { 39 gulp.src('./dist/**') 40 .pipe(gulpSsh.dest(filePath)) // 傳到服務器 41 done() 42 })
// 運行腳本 "build:dist": "npm run build:stage && npm run del && npm run upload"
1 // node 項目 2 gulpSsh.shell(['cd /root/test', 'cnpm install', 'pm2 delete all', 'pm2 start /root/test/app.js']) 3 4 // 運行命令 5 "node": "gulp node"
三、自動化構建gitlab、gitlab-runner、jenkins
0、准備
- 一個雲服務器
- 一個GitLab倉庫
1、gttlab-runner
首先仍是簡單說一下什么是gitlab-runner,官網是這么描述的:git
GitLab Runner is the open source project that is used to run your jobs and send the results back to GitLab. It is used in conjunction with GitLab CI, the open-source continuous integration service included with GitLab that coordinates the jobs.docker
也就是說gitlab-runner是配合gitlab-ci進行使用的。
通常地,gitlab里面的每個工程都會定義一個屬於這個工程的軟件集成腳本,用來自動化地完成一些軟件集成工做。
當這個工程的倉庫代碼發生變更時,好比有人push了代碼或者分支合並,gitlab就會將這個變更通知gitlab-ci。這時gitlab-ci會找出與這個工程相關聯的runner,並通知這些runner把代碼更新到本地並執行預約義好的執行腳本。
GitLab-CI 就是一套配合gitLab使用的持續集成系統
使用GitLab-CI我們可以使用 GitLab-Runner
每個項目配置一個自動化的runner,當項目發生變化(比如push代碼),當然這個變化行為是可定義的,
我們的GitLab-CI就會找到於這個項目相關的runner,通知runner把代碼更新到本地,並且執行定義好的執行腳本(gitlab-ci.yml)
太難了,來來回回搗鼓好幾遍,好像有點懂了,,雖然還沒成功,,目前報錯 bash: line 118: npm: command not found,記錄下過程吧
參考文件:https://segmentfault.com/a/1190000023117085?utm_source=sf-related
1、編寫CI/CD腳本,CI/CD編輯器,可選模版Docker,我的配置如下
1 # docker-build: 2 # Use the official docker image. 3 image: node:10 4 stages: 5 - install_deps 6 # services: 7 # - docker:dind 8 before_script: 9 - echo "before_script 開始構建" 10 after_script: 11 - echo "after_script 構建結束" 12 install_deps: 13 stage: install_deps 14 tags: 15 - master 16 only: 17 - master 18 script: 19 - cd /usr/local/nginx/html/front-vue 20 - users 21 - pwd 22 - ls -al 23 - git remote set-url origin git@gitlab.com:pfSlight/front-vue.git 24 - git pull origin master 25 - rm -rf ./dist 26 - rm -rf ./node_modules 27 - npm config set registry https://register.npm.taobao.prg 28 - npm install -g cnpm --registry=https://registry.npm.taobao.org 29 - cnpm install 30 - npm run build:stage
2、需要配置CI/CD Runner,設置 - CI/CD - Runner
配置前,長這樣 | 配置后,就這樣 | 過程是真的難,,, |
![]() |
![]() |
1、點擊按鈕, 要安裝的插件,配置的賬號,都很清楚,照着做就可以了 2、查看運行狀態 gitlab-runner status |
3、運行流水線 CI/CD - 流水線 - Run pipeline
失敗,查看日志,,報錯就是最上面說的 npm: command not found ,,然后參照上面連接的文章進行處理
1)切換到gitlab-runner用戶,重新安裝 nvm ,命令:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash (git不穩,可能需要多次嘗試)
2)配置環境變量,命令:source ~/.bashrc (有問題,參照前面的隨筆)
3)安裝node, 命令:nvm install 12.18.4
4)得,npm有了,別的錯又來了,如下:,接着改腳本,改成https//
5)上結果,,完美,,,欸,就是個玩兒
7)這是后補的,因為我過了一段時間去操作的時候,練習着多建幾個runner,沒想到,怎么做都不好使,,
建了有4個runner,一直懷疑是操作有問題,浪費了兩個下去
直到今天周末有時間,百度了很多,其實一開始就有搜到可能是因為git版本過低導致,但是不願意相信,因為第一次建好的時間,多次操作都能成功,
實在不行,就試試吧,嘗試去升級git版本,,參考這個文件 https://www.cnblogs.com/kevingrace/p/8252517.html ,,可以,,又見到了上面的succeeded。
哎,以后不能軸了,多角度嘛,干嘛不相信自己的配置。
8)npm run build報錯 :building for production...Killed
按照他人的說法是,服務器內存不夠用了,這樣就給他配置一個單獨的內存出來就解決了
sudo /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024 sudo /sbin/mkswap /var/swap.1 sudo /sbin/swapon /var/swap.1 |
9)修改用戶權限 https://www.cnblogs.com/suyufei/p/11941385.html
2、jenkins
開源CI/CD軟件領導者,提供超過1000個插件來支持構建、部署、自動化、滿足任何項目需要。
優點:簡單安裝、