视频地址:https://www.bilibili.com/video/av91796463?t=43

1.生成vue项目demo

  1. vue create my-project

2.上传vue项目到github

  1. git remote add origin https://github.com/ynzy/deployment.git
  2. git push -u origin master

3.本地生成公钥和私钥

  1. ssh-keygen -t rsa -C autodeployment -f deployment
  2. -------------
  3. Generating public/private rsa key pair.
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in deployment. // 私钥
  7. Your public key has been saved in deployment.pub. // 公钥
  8. The key fingerprint is:
  9. SHA256:egPfdeoMwy8eaCMnA07kZp1cgL7i/2PZmGWAI3eD0y8 autodeployment
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. | .. |
  13. | . . |
  14. | ..+ . |
  15. | .oBo=o |
  16. | o*==+S . . |
  17. | .=..E+++ . o |
  18. | . .. =OO * . |
  19. | . *B.o.B |
  20. | ..o.. ...+ |
  21. +----[SHA256]-----+
  1. 查看所有文件 ```javascript ls -la

drwxr-xr-x 1 Administrator 197121 0 5月 1 2019 .ssh/ -rw-r—r— 1 Administrator 197121 1679 2月 27 14:51 deployment -rw-r—r— 1 Administrator 197121 397 2月 27 14:51 deployment.pub

  1. 2. 进入`.ssh`目录将`deployment`拷贝进去
  2. ```javascript
  3. // 拷贝 要拷贝的文件目录 拷贝到的文件目录,.代表当前目录
  4. cp ~/deployment .

4.返回根目录,把公钥拷贝到远端服务器

  1. // 拷贝到远端 拷贝的文件 服务器用户名@服务器地址:当前目录
  2. scp deployment.pub root@47.95.119.112:.
  1. 把公钥写入远端服务器.ssh目录下
  • 进入.ssh目录查看有没有authorized_keys文件,没有则创建一个 ```javascript ls -la

total 8 drwx——— 2 root root 4096 Dec 25 03:27 . dr-xr-x—-. 5 root root 4096 Feb 27 15:05 .. -rw———- 1 root root 0 Feb 16 08:05 authorized_keys

  1. - 返回上一级执行写入命令
  2. ```javascript
  3. cat deployment.pub >> ~/.ssh/authorized_keys

5.拷贝本地私钥到github

  • 找到私钥文件,复制到github
  • github项目地址->settings->Secrets
  • 添加私钥

    1. name:ALYUN
    2. value:私钥

    6. 开启workflow自动工作流

  • 项目中找到actions -> node.jsactions.png

nodejs.png

  • 点击之后会生成一个文件 ```yml name: Node.js CI # 自动部署名字

on: [push] # 发生push时,执行git push命令会触发这里

jobs: build:

  1. runs-on: ubuntu-latest //运行的系统
  2. strategy:
  3. matrix:
  4. node-version: [8.x, 10.x, 12.x] # nodejs版本,写几个版本号测试几次,我们用12.x就可以了
  5. steps:
  6. - uses: actions/checkout@v2
  7. - name: Use Node.js ${{ matrix.node-version }}
  8. uses: actions/setup-node@v1
  9. with:
  10. node-version: ${{ matrix.node-version }}
  11. - run: npm install
  12. - run: npm run build --if-present
  13. - run: npm test
  14. env:
  15. CI: true

需要写的一些配置

  1. - name: Deploy //当前插件名字
  2. uses: easingthemes/ssh-deploy@v2.0.7 //使用这个人开发的一个插件
  3. env:
  4. SSH_PRIVATE_KEY: ${{ secrets.ALYUN }} # 秘钥名字
  5. ARGS: "-rltgoDzvO --delete" # 固定配置
  6. SOURCE: "dist/" # 项目打包之后的目录
  7. REMOTE_HOST: "47.95.119.112" # 服务器地址
  8. REMOTE_USER: "root" # 服务器用户
  9. TARGET: "/www/autodeploy" # 指定服务器目录

```

  • 在右侧可以搜索插件

sshdeploy.png

  • 创建自动部署脚本

创建自动部署脚本.png