name: Ghaymah CI/CD Pipeline

on:
  push:
    branches:
      - "release/**"
      - main
  workflow_dispatch:

jobs:
  build:
    name: Build Docker Image
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Source
        uses: actions/checkout@v5

      - name: Build Docker Image
        run: |
          docker build -t ghaymah-sre-api:${{ github.sha }} .

  deploy-staging:
    name: Deploy to Staging
    needs: build

    if: startsWith(github.ref, 'refs/heads/release/')

    environment: staging

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v5

      - name: Install Ghaymah CLI
        run: curl -sSL https://cli.ghaymah.systems/install.sh | bash

      - name: Login to Ghaymah
        run: |
          $HOME/ghaymah/bin/gy auth login \
            --email "${{ secrets.GHAYMAH_EMAIL }}" \
            --password "${{ secrets.GHAYMAH_PW }}"

      - name: Deploy to Ghaymah Staging
        run: |
          $HOME/ghaymah/bin/gy resource app launch

  deploy-production:
    name: Deploy to Production

    needs: build

    if: github.ref == 'refs/heads/main'

    environment:
      name: production

    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v5

      - name: Install Ghaymah CLI
        run: curl -sSL https://cli.ghaymah.systems/install.sh | bash

      - name: Login to Ghaymah
        run: |
          $HOME/ghaymah/bin/gy auth login \
            --email "${{ secrets.GHAYMAH_EMAIL }}" \
            --password "${{ secrets.GHAYMAH_PW }}"

      - name: Deploy to Ghaymah Production
        run: |
          $HOME/ghaymah/bin/gy resource app launch
