name: Build Mission PBO

on:
  push:
    branches: [main, master]
    tags: ["v*"]
  workflow_dispatch:

env:
  PBO_NAME: "KPLib-Ruha.ruha.pbo"

jobs:
  pack:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - run: mkdir build

      - name: Pack mission folder into PBO
        uses: docker://arwynfr/armake2:debian
        with:
          args: pack src "build/${{ env.PBO_NAME }}"

      - name: Verify PBO integrity
        uses: docker://arwynfr/armake2:debian
        with:
          args: inspect "build/${{ env.PBO_NAME }}"

      - name: Upload PBO as artifact
        uses: actions/upload-artifact@v3
        with:
          name: ${{ env.PBO_NAME }}
          path: build/${{ env.PBO_NAME }}
          if-no-files-found: error

  deploy:
    needs: [pack]
    runs-on: ubuntu-latest
    steps:
      - name: Download PBO artifact
        uses: actions/download-artifact@v3
        with:
          name: ${{ env.PBO_NAME }}

      - name: Deploy to game server via SCP
        uses: appleboy/scp-action@v1
        with:
          host: ${{ secrets.SERVER_HOST }}
          username: ${{ secrets.SERVER_USER }}
          password: ${{ secrets.SERVER_PASSWORD }}
          # key: ${{ secrets.SSH_PRIVATE_KEY }}
          source: ${{ env.PBO_NAME }}
          target: /home/arma3server/serverfiles/mpmissions/
