name: Deploy 
run-name: build and deploy app to server.
on:
  push:
    paths:
      - '**.cpp'
      - '**.h'
      - '**.conf'
jobs:
  Build:
    runs-on: [ubuntu-latest, ubuntu-24.04]
    container:
      image: registry.cn-shenzhen.aliyuncs.com/amass_toolset/ubuntu_dev:24.04
      credentials:
        username: ${{ secrets.DOCKER_USERNAME }}
        password: ${{ secrets.DOCKER_PASSWORD }}
    steps:
      - name: Set up SSH
        run: |
          mkdir -p ~/.ssh/
          echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519
          chmod 600 ~/.ssh/id_ed25519
          ssh-keyscan -t ed25519 -p 2022 gitea.amass.fun >> ~/.ssh/known_hosts
      - name: checkout repository
        uses: actions/checkout@v4
      - name: Notify-Start
        if: ${{ always() }}
        run: |
          echo "${{ github.repository }} 开始构建..." > notify.tpl
          echo "构建地址: https://gitea.amass.fun/${{ github.repository }}/actions/runs/${{ github.run_number }}">> notify.tpl
          echo "仓库地址: https://gitea.amass.fun/${{ github.repository }}">> notify.tpl
          echo "提交ID:   $(git rev-parse --short HEAD)">> notify.tpl
          echo -n "提交消息: ${{ github.event.head_commit.message }}">> notify.tpl
          cat notify.tpl | envsubst |  jq -sR . | xargs -0  -I {} curl -H "Content-Type: application/json" -X POST -d '{"type":"text","msg":{} }' https://amass.fun/api/v1/notify
      - run: resources/build.sh build
      - name: Copy files to server
        uses: appleboy/scp-action@v0.1.7
        with:
          host: ${{ vars.YUYUN_SERVER }}
          username: ${{ vars.YUYUN_USER }}
          password: ${{ secrets.SERVER_ROOT_PASSWORD }}
          overwrite: true
          strip_components: 2
          source: build/Server/HttpServer
          target: /tmp
      - name: Restart server
        uses: appleboy/ssh-action@v1.1.0
        with:
          host: ${{ vars.YUYUN_SERVER }}
          username: ${{ vars.YUYUN_USER }}
          password: ${{ secrets.SERVER_ROOT_PASSWORD }}
          script: |
            cd /root/Server
            source /etc/profile
            /root/Server/HttpServer --exit
            sleep 1
            cp /tmp/HttpServer /root/Server/HttpServer
            nohup /root/Server/HttpServer > /dev/null 2>&1 &
            exit 0
      - name: Copy openresty configuration files to server
        uses: appleboy/scp-action@v0.1.7
        with:
          host: ${{ vars.YUYUN_SERVER }}
          username: ${{ vars.YUYUN_USER }}
          password: ${{ secrets.SERVER_ROOT_PASSWORD }}
          overwrite: true
          strip_components: 1
          source: Server/conf
          target: /root/Server
      - name: Restart openresty
        uses: appleboy/ssh-action@v1.1.0
        with:
          host: ${{ vars.YUYUN_SERVER }}
          username: ${{ vars.YUYUN_USER }}
          password: ${{ secrets.SERVER_ROOT_PASSWORD }}
          script: |
            cd /root/Server
            openresty -p . -s reload
      - name: Notify-End
        if: ${{ always() }}
        run: |
          echo "${{ github.repository }} 构建结束" > notify.tpl
          echo "构建状态: ${{ job.status }}">> notify.tpl
          echo "构建地址: https://gitea.amass.fun/${{ github.repository }}/actions/runs/${{ github.run_number }}">> notify.tpl
          echo "仓库地址: https://gitea.amass.fun/${{ github.repository }}">> notify.tpl
          echo "提交ID:   $(git rev-parse --short HEAD)">> notify.tpl
          echo -n "提交消息: ${{ github.event.head_commit.message }}">> notify.tpl
          cat notify.tpl | envsubst |  jq -sR . | xargs -0  -I {} curl -H "Content-Type: application/json" -X POST -d '{"type":"text","msg":{} }' https://amass.fun/api/v1/notify
