基于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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章