基于github+travis自动部署vue项目到远端服务器

前期准备

  • github帐号一个
  • 一个vue的项目
  • 一台linux服务器

travis中添加项目

travis是基于github的,全部只有github的帐号能够登陆travis,开发者必须有一个github的帐号,登陆后,点击加号,开始添加项目html

clipboard.png

点击Manage repositories on GitHub,前往github中选择项目
clipboard.pngvue

这里github提供的两种模式,第一种添加全部项目,第二种添加指定项目,通常咱们选择第二种添加须要添加的项目
clipboard.pngnode

Github生成访问令牌

github的我的中心,https://github.com/settings/tokens/路径下,生成一个访问令牌,添加到travis中,给travis操做仓库的权限
clipboard.pnglinux

复制令牌内容,进入travis中的中,找到SSH key的菜单,将令牌内容贴入,点击Add
clipboard.pnggit

travis配置文件

在项目根目录下,新建.travis.yml文件。travis要执行的自动化步骤,都须要在该文件中配置,这里是一个最简单的配置文件,当travis检测到master分支代码发生变化时,自动执行npm install和npm run buildgithub

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的更多配置,参考阮老师的教程:npm

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

travis免密登陆远端服务器

部署dist目录到服务器

开发者在本地执行npm run build,编译后生成dist目录,服务器须要的也是dist目录。travis要作的就是把dist目录自动发送到服务器服务器

先说思路,当travis执行build生成目标文件后,travis把dist目录提交到仓库的指定分支,好比叫deploy分支,经过ssh自动登陆服务器,执行git pull,把deploy分支拉下来ssh

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 your-name@xx.xx.xx.xx 'cd /your-path && git fetch --all && git reset --hard origin/deploy && git pull'

部署成功

配置文件上的操做执行成功后,你的项目就多了这个高大上的标志fetch

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
相关文章
相关标签/搜索