name: 自动部署

on:
  push:
    branches:
      - master
    paths-ignore:
      - .gitignore
      - .vscode/**
      - README.md

env:
  docker_host: ${{ secrets.DOCKER_HOST }}
  docker_name: ${{ secrets.DOCKER_HOST }}/${{ gitea.repository }}
  docker_user: ${{ secrets.DOCKER_USER }}
  docker_pass: ${{ secrets.DOCKER_PASS }}
  deploy_host: ${{ secrets.DEPLOY_HOST }}
  deploy_port: ${{ secrets.DEPLOY_PORT }}
  deploy_user: ${{ secrets.DEPLOY_USER }}
  deploy_pass: ${{ secrets.DEPLOY_PASS }}
  deploy_name: demo_server

jobs:
  build:
    runs-on: ubuntu-latest
    container:
      image: catthehacker/ubuntu:act-latest
    steps:
      - name: 检出代码
        id: checkout
        uses: https://git.app.juetan.cn/mirror/checkout@v3

      - name: 构建镜像
        run: |
          docker build -t ${{ env.docker_name }}:latest .

      - name: 登陆镜像
        run: |
          docker login -u "${{env.docker_user}}" -p "${{env.docker_pass}}" ${{env.docker_host}}

      - name: 推送镜像
        shell: bash
        run: |
          docker push ${{ env.docker_name }}:latest

      - name: 更新服务
        uses: https://git.app.juetan.cn/mirror/ssh-action@v1.0.0
        if: false
        with:
          host: ${{ env.deploy_host }}
          port: ${{ env.deploy_port }}
          username: ${{ env.deploy_user }}
          password: ${{ env.deploy_pass }}
          script: |
            docker service ls | grep -q ${{ env.deploy_name }} || exit 0
            docker service update --image ${{ env.docker_name }}:latest ${{ env.deploy_name }}
