name: Deploy to EC2

on:
  push:
    branches:
      - main
      - staging

jobs:
  deploy:
    runs-on: ubuntu-latest
    
    env:
      SLACK_WEBHOOK_URL: https://hooks.slack.com/services/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
    
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
      
      - name: Set up Python
        uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      
      - name: Install dependencies
        run: |
          pip install -r requirements.txt
      
      - name: Set deployment environment
        id: deployment
        run: |
          if [ "${{ github.ref }}" == "refs/heads/main" ]; then
            echo "EC2_HOST=18.223.231.19" >> $GITHUB_ENV
            echo "EC2_USERNAME=ubuntu" >> $GITHUB_ENV
            echo "EC2_APP_PATH=/var/www/OTM/injectionProject" >> $GITHUB_ENV
            echo "SSH_KEY_SECRET=SPARK_STAGING" >> $GITHUB_ENV
            echo "BRANCH=main" >> $GITHUB_ENV
            echo "PM2_APP=5" >> $GITHUB_ENV
          elif [ "${{ github.ref }}" == "refs/heads/staging" ]; then
            echo "EC2_HOST=3.141.227.135" >> $GITHUB_ENV
            echo "EC2_USERNAME=ubuntu" >> $GITHUB_ENV
            echo "EC2_APP_PATH=/var/www/OTM_Staging/injection_stg" >> $GITHUB_ENV
            echo "SSH_KEY_SECRET=CASPER" >> $GITHUB_ENV
            echo "BRANCH=staging" >> $GITHUB_ENV
            echo "PM2_APP=15" >> $GITHUB_ENV
          fi
      
      - name: Get commit message
        id: commit
        run: |
          echo "message=$(git log -1 --pretty=%B | head -n 1)" >> $GITHUB_OUTPUT
      
      - name: Notify Slack - Build Started
        env:
          BUILD_STATUS: started
          COMMIT_MESSAGE: ${{ steps.commit.outputs.message }}
        run: python slack_notify.py
        continue-on-error: true
      
      - name: Deploy via Git Pull
        uses: appleboy/ssh-action@v1.0.0
        with:
          host: ${{ env.EC2_HOST }}
          username: ${{ env.EC2_USERNAME }}
          key: ${{ secrets[env.SSH_KEY_SECRET] }}
          port: 22
          script: |
            cd ${{ env.EC2_APP_PATH }}
            sudo git pull origin ${{ env.BRANCH }}

      - name: Restart app on EC2
        uses: appleboy/ssh-action@v1.0.0
        with:
          host: ${{ env.EC2_HOST }}
          username: ${{ env.EC2_USERNAME }}
          key: ${{ secrets[env.SSH_KEY_SECRET] }}
          port: 22
          script: |
            cd ${{ env.EC2_APP_PATH }}
            sudo npm i
            sudo pm2 stop ${{ env.PM2_APP }}
            sudo pm2 start ${{ env.PM2_APP }}
      
      - name: Notify Slack - Build Success
        if: success()
        env:
          BUILD_STATUS: success
          COMMIT_MESSAGE: ${{ steps.commit.outputs.message }}
        run: python slack_notify.py
        continue-on-error: true
      
      - name: Notify Slack - Build Failed
        if: failure()
        env:
          BUILD_STATUS: failed
          COMMIT_MESSAGE: ${{ steps.commit.outputs.message }}
        run: python slack_notify.py
        continue-on-error: true
