# 模板

name: Deploy Backend
on:
  push:
    branches:
      - 'master'
env:
  TZ: Asia/Shanghai
jobs:
  build:
    runs-on: ubuntu-latest

    # 如果需要 docker 镜像,可以在这里添加

    steps:
      - uses: actions/checkout@v1

      - name: xxx
        run: |
        xxx
        xxx

# 编译应用

# Maven

      - name: Set up JDK
        uses: actions/setup-java@v1
        with:
          java-version: 16

      - run: mvn clean install

# npm

    - name: Use Node.js
      uses: actions/setup-node@v1
      with:
        node-version: "16.x"
        cache: "npm"  # or "yarn"
    - run: npm ci  # or yarn install --frozen-lockfile
    - run: npm run build

# yarn

    - name: Use Node.js
      uses: actions/setup-node@v1
      with:
        node-version: "16.x"
        cache: "yarn"
    - run: yarn install --frozen-lockfile
    - run: yarn build

# Python

    - name: Set up Python 3.9
      uses: actions/setup-python@v2
      with:
        python-version: 3.9
    - name: Install Dependencies
      run: |
        python3 -m pip install --upgrade pip
        pip3 install -r requirements.txt

# 测试应用

# 配置测试 MySQL 数据库

    services:
      mysql:
        # docker hub image
        image: mysql:8
        env:
            MYSQL_ROOT_PASSWORD: testtest
            MYSQL_DATABASE: uestcmsc_webapp
        ports:
            - 3306:3306

顺便一提,GitHub Actions 也支持整个 workflow 直接在 docker container 中运行,如下:

jobs:
  container-job:
    runs-on: ubuntu-latest
    # Docker Hub image that `container-job` executes in
    container: node:10.18-jessie

# Python Django 测试

    strategy:
      max-parallel: 3
      matrix:
        python-version: [3.7, 3.8, 3.9]

    steps:
    - name: Git Checkout
      uses: actions/checkout@v2
    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v2
      with:
        python-version: ${{ matrix.python-version }}
    - name: Install Dependencies
      run: |
        cp config.template.py config.py
        python3 -m pip install --upgrade pip
        pip3 install -r requirements.txt
    - name: Run Tests
      run: |
        python3 manage.py makemigrations accounts activities \
                activities_files activities_photos activities_links activities_comments \
                cloud users --noinput
        python3 manage.py migrate --noinput
        python3 manage.py createcachetable
        coverage run --source=./ manage.py test --noinput
    - name: Coverage Report
      run: |
        coverage report -m

# 配置远程服务器

# 配置 ssh

      - name: Setup SSH env
        env:
          ACTION_DEPLOY_KEY: ${{ secrets.ACTION_DEPLOY_KEY }}
        run: |
          mkdir -p ~/.ssh/
          echo "$ACTION_DEPLOY_KEY" | tr -d '\r' > ~/.ssh/id_rsa
          chmod 600 ~/.ssh/id_rsa
          ssh-keyscan github.com >> ~/.ssh/known_hosts
          ssh-keyscan lyh543.cn >> ~/.ssh/known_hosts
          
          # git config --global user.name 'lyh543' # 换成你自己的邮箱和名字
          # git config --global user.email 'lyh543@outlook.com'

# ssh 发送文件

      - name: Send 🚀 To Server
        run: |
          scp \
          target/Aurora-DriveSyncer.jar \
          lyh543@lyh543.cn:~/Aurora-DriveSyncer.jar

# rsync 增量同步文件夹

--delete 参数会同步在 source 中已经被删除的文件。

    - name: Deploy 🚀 To Server
      run: |
        rsync --archive --verbose --human-readable --delete \
          dist/* \
          lyh543@lyh543.cn:/etc/caddy/html/blog/