name: Deploy

on:
  push:
    branches: [main, v2]

jobs:
  deploy:
    runs-on: aliyun
    steps:
      - name: Deploy
        run: |
          sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
          apk add --no-cache rsync docker-cli docker-cli-compose
          GIT_URL="http://8.161.227.145:3000/XEngineers/CamTalk.git"
          if [ -d /root/camtalk/.git ]; then
            cd /root/camtalk
            git fetch "$GIT_URL" ${GITHUB_REF_NAME} --depth=1
            git reset --hard FETCH_HEAD
          else
            rm -rf /tmp/camtalk-deploy
            git clone --depth=1 --branch ${GITHUB_REF_NAME} \
              http://8.161.227.145:3000/XEngineers/CamTalk.git /tmp/camtalk-deploy
            mkdir -p /root/camtalk
            rsync -a --delete \
              --exclude='.env' \
              --exclude='pgdata' \
              --exclude='redisdata' \
              /tmp/camtalk-deploy/ /root/camtalk/
            rm -rf /tmp/camtalk-deploy
          fi

          chmod +x /root/camtalk/deploy.sh
          /root/camtalk/deploy.sh build
          /root/camtalk/deploy.sh restart