diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml new file mode 100644 index 0000000..4d2c5a2 --- /dev/null +++ b/.gitea/workflows/deploy.yml @@ -0,0 +1,76 @@ +# .gitea/workflows/deploy.yml +name: Deploy Xmirg + +on: + push: + branches: [main] + workflow_dispatch: + +env: + APP_NAME: xmirg + DEPLOY_PATH: /opt/xmirg + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Go + uses: actions/setup-go@v5 + with: + go-version: '1.22' + cache: true + + - name: Test + run: go test ./... + + - name: Build + run: | + CGO_ENABLED=0 GOOS=linux go build \ + -ldflags="-s -w -X main.version=${{ github.sha }}" \ + -o dist/${{ env.APP_NAME }} ./cmd/${{ env.APP_NAME }} + + - name: Upload artifact + uses: actions/upload-artifact@v3 + with: + name: ${{ env.APP_NAME }} + path: dist/${{ env.APP_NAME }} + + deploy: + needs: build + runs-on: ubuntu-latest + if: github.ref == 'refs/heads/main' + steps: + - name: Download artifact + uses: actions/download-artifact@v3 + with: + name: ${{ env.APP_NAME }} + path: dist + + - name: Setup SSH + run: | + mkdir -p ~/.ssh + echo "${{ secrets.SSH_PRIVATE_KEY }}" > ~/.ssh/id_ed25519 + chmod 600 ~/.ssh/id_ed25519 + ssh-keyscan -H ${{ secrets.DEPLOY_HOST }} >> ~/.ssh/known_hosts + + - name: Copy binary + run: | + chmod +x dist/${{ env.APP_NAME }} + scp dist/${{ env.APP_NAME }} \ + ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }}:${{ env.DEPLOY_PATH }}/${{ env.APP_NAME }}.new + + - name: Restart service + run: | + ssh ${{ secrets.DEPLOY_USER }}@${{ secrets.DEPLOY_HOST }} bash -s <<'EOF' + set -e + cd /opt/xmirg + cp xmirg xmirg.bak 2>/dev/null || true + mv xmirg.new xmirg + chmod +x xmirg + sudo systemctl restart xmirg + sleep 2 + systemctl is-active --quiet xmirg || { sudo mv xmirg.bak xmirg; sudo systemctl restart xmirg; exit 1; } + EOF \ No newline at end of file