基於github+travis自動部署vue項目到遠端服務器

travis中添加項目

travis是基於github的,所有隻有github的賬號可以登錄travis,開發者必須有一個github的賬號,登錄後,點擊加號,開始添加項目

clipboard.png

點擊Manage repositories on GitHub,前往github中選擇項目
clipboard.png

這裏github提供的兩種模式,第一種添加所有項目,第二種添加指定項目,一般我們選擇第二種添加需要添加的項目
clipboard.png

travis配置文件

在項目根目錄下,新建.travis.yml文件。travis要執行的自動化步驟,都需要在該文件中配置,這裏是一個最簡單的配置文件,當travis檢測到master分支代碼發生變化時,自動執行npm install和npm run build

language: node_js
node_js:
- 10.16.0
cache:
  directories:
  - node_modules
install:
- npm install
before_script: 

script:
- npm run build

after_script: 

branches:
  only:
  - master

關於travis的更多配置,參考阮老師的教程:

http://www.ruanyifeng.com/blo...

travis免密登錄遠端服務器

部署dist目錄到服務器

開發者在本地執行npm run build,編譯後生成dist目錄,服務器需要的也是dist目錄。travis要做的就是把dist目錄自動發送到服務器

先說思路,當travis執行build生成目標文件後,travis把dist目錄提交到倉庫的指定分支,比如叫deploy分支,通過ssh自動登錄服務器,執行git pull,把deploy分支拉下來

after_script: 
  - cd ./dist
  - git init
  - git config user.name "your name"
  - git config user.email "your email"
  - git add .
  - git commit -m "Travis CI Auto Builder"
  - git push --force --quiet "https://${GH_TOKEN}@${GH_REF}" master:deploy
  - ssh [email protected] 'cd /your-path && git fetch --all && git reset --hard origin/deploy && git pull'

部署成功

配置文件上的操作執行成功後,你的項目就多了這個高大上的標誌

clipboard.png

一些報錯

Travis-CI解密證書時報錯

報錯提示一般是這樣的:

openssl aes-256-cbc -K $encrypted_d3c25c1810a6_key -iv $encrypted_d3c25c1810a6_iv -in id_rsa.enc -out ~\/.ssh/id_rsa -d
~/.ssh/id_rsa: No such file or directory
The command "openssl aes-256-cbc -K $encrypted_d3c25c1810a6_key -iv $encrypted_d3c25c1810a6_iv -in id_rsa.enc -out ~\/.ssh/id_rsa -d" failed and exited with 1 during .

這個報錯情況的出現,有可能是因爲在使用travis encrypt-file ~/.ssh/id_rsa --add命令生成加密密鑰時,自動增加了轉義字符串,需要手動刪除轉義字符

生成的密鑰是這樣的:

- openssl aes-256-cbc -K $encrypted_d3c25c1810a6_key -iv $encrypted_d3c25c1810a6_iv -in id_rsa.enc -out ~\/.ssh/id_rsa -d

刪除轉義字符後是這樣的:

- openssl aes-256-cbc -K $encrypted_d3c25c1810a6_key -iv $encrypted_d3c25c1810a6_iv -in id_rsa.enc -out ~/.ssh/id_rsa -d
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章