name: Deploy CI4 API to AWS Lightsail

on:
  push:
    branches:
      - main  # Change this if your branch is different

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3

      - name: Set up SSH Key
        run: |
          mkdir -p ~/.ssh
          echo "${{ secrets.MPM_LIGHTSAIL_SSH_KEY }}" > ~/.ssh/lightsail_key
          chmod 600 ~/.ssh/lightsail_key
          ssh-keyscan -H ${{ secrets.MPM_LIGHTSAIL_HOST }} >> ~/.ssh/known_hosts

      - name: Deploy Code to AWS Lightsail API Folder
        run: |
          rsync -avz -e "ssh -i ~/.ssh/lightsail_key" ./ ${{ secrets.MPM_LIGHTSAIL_USER }}@${{ secrets.MPM_LIGHTSAIL_HOST }}:/opt/bitnami/apache/htdocs/mpm-web/

      - name: Set Permissions & Restart Apache
        run: |
          ssh -i ~/.ssh/lightsail_key ${{ secrets.MPM_LIGHTSAIL_USER }}@${{ secrets.MPM_LIGHTSAIL_HOST }} << 'EOF'
          sudo chown -R bitnami:daemon /opt/bitnami/apache/htdocs/members/mpm-web
          sudo chmod -R 775 /opt/bitnami/apache/htdocs/members/mpm-web
          sudo /opt/bitnami/ctlscript.sh restart apache
          EOF
