Skip to content

使用GithubAction自动部署

Published: at 17:48

1.在服务器生成ssh密钥对 教程

2.将密钥添加到仓库的Secrets中 代码仓库 - Settings - Secrets - Actions 添加完成后就可以在action中使用了

3.部署代码示例

name: Build and Deploy

on:
  push:
    branches:
      - main

permissions:
  contents: read

jobs:
  build:
    runs-on: ubuntu-latest
    environment: production
    
    steps:
    - name: Checkout
      uses: actions/checkout@v3
    # 缓存依赖
    - name: Cache nodemodules
      uses: actions/cache@v1
      env:
          cache-name: cache-node-modules
      with:
          # 需要缓存的文件的路径
          path: ./node_modules 
          # 对缓存的文件指定的唯一标识
          key: ${{ runner.os }}-dist-${{ env.cache-name }}-${{ hashFiles('./package.json') }} 
          # 用于没有再找目标key的缓存的backup选项
          restore-keys: |
                      ${{ runner.os }}-dist-${{ env.cache-name }}-
                      ${{ runner.os }}-dist-
                      ${{ runner.os }}-
    # 装依赖
    - name: Install
      run: yarn
    # 打包
    - name: Build
      run: yarn build
    # 上传打包资源
    - name: Deploy
      uses: AEnterprise/rsync-deploy@v1.0
      env:
          DEPLOY_KEY: ${{ secrets.SERVER_PRIVATE_KEY }}
          ARGS: '-avz --delete'
          FOLDER: 'dist/'
          SERVER_PORT: ${{ secrets.SERVER_PORT }}
          SERVER_IP: ${{ secrets.SERVER_HOST }}
          USERNAME: ${{ secrets.USER_NAME }}
          SERVER_DESTINATION: ${{secrets.SERVER_DESTINATION}} # 服务器目标路径