name: CI/CD - Ghaymah Cloud

on:
  push:
    branches: [main, staging]
  pull_request:
    branches: [main, staging]

env:
  DOCKERHUB_REPOSITORY: ${{ secrets.DOCKERHUB_REPOSITORY }}

jobs:
  build-and-push:
    name: Build and Push to Docker Hub
    runs-on: ubuntu-latest
    if: github.event_name == 'push'
    outputs:
      environment: ${{ steps.env.outputs.environment }}
      image_uri: ${{ steps.image.outputs.image_uri }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v5.0.0

        
      - name: Set deployment environment
        id: env
        run: |
          if [ "${{ github.ref_name }}" = "main" ]; then
            echo "environment=production" >> $GITHUB_OUTPUT
          else
            echo "environment=staging" >> $GITHUB_OUTPUT
          fi

      - name: Login to Docker Hub
        uses: docker/login-action@v3.6.0
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Build and push Docker image
        uses: docker/build-push-action@v6
        with:
          context: ./nodeapp
          push: true
          tags: |
            docker.io/${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKERHUB_REPOSITORY }}:production
            docker.io/${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKERHUB_REPOSITORY }}:staging
            docker.io/${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKERHUB_REPOSITORY }}:${{ github.sha }}

      - name: Set deployment image
        id: image
        run: |
          IMAGE_BASE="docker.io/${{ secrets.DOCKERHUB_USERNAME }}/${{ env.DOCKERHUB_REPOSITORY }}"
          if [ "${{ steps.env.outputs.environment }}" = "production" ]; then
            echo "image_uri=${IMAGE_BASE}:production" >> $GITHUB_OUTPUT
          else
            echo "image_uri=${IMAGE_BASE}:staging" >> $GITHUB_OUTPUT
          fi

  build:
    name: Build Docker Image
    runs-on: ubuntu-latest
    if: github.event_name == 'pull_request'
    steps:
      - name: Checkout code
        uses: actions/checkout@v5.0.0

      - name: Build Docker image
        run: docker build ./nodeapp

  deploy-staging:
    name: Deploy to Staging
    runs-on: ubuntu-latest
    needs: build-and-push
    if: github.event_name == 'push' && github.ref_name == 'staging'
    environment:
      name: staging
    steps:
      - name: Checkout code
        uses: actions/checkout@v5.0.0

      - 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: Create Ghaymah configuration
        working-directory: nodeapp
        env:
          IMAGE_URI: ${{ needs.build-and-push.outputs.image_uri }}
          PROJECT_ID: ${{ secrets.GHAYMAH_PROJECT_ID }}
          APP_ID: ${{ secrets.GHAYMAH_APP_ID_STAGING }}
          PULL_SECRET: ${{ secrets.DOCKERHUB_PULL_SECRET_NAME }}
        run: |
          jq -n \
            --arg id "${APP_ID}" \
            --arg name "nodeapp-staging" \
            --arg projectId "${PROJECT_ID}" \
            --arg image "${IMAGE_URI}" \
            --arg pullSecret "${PULL_SECRET}" \
            '{
              id: $id,
              name: $name,
              projectId: $projectId,
              ports: [{ expose: true, number: 5000 }],
              publicAccess: {
                enabled: true,
                domain: "auto"
              },
              resourceTier: "t1",
              container: {
                image: $image,
                pullSecretName: $pullSecret
              }
            }' > .ghaymah.json

          cat .ghaymah.json

      - name: Deploy to Ghaymah
        working-directory: nodeapp
        run: $HOME/ghaymah/bin/gy resource app launch --env staging

  deploy-production:
    name: Deploy to Production
    runs-on: ubuntu-latest
    needs: build-and-push
    if: github.event_name == 'push' && github.ref_name == 'main'
    environment:
      name: production
    steps:
      - name: Checkout code
        uses: actions/checkout@v5.0.0

      - 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: Create Ghaymah configuration
        working-directory: nodeapp
        env:
          IMAGE_URI: ${{ needs.build-and-push.outputs.image_uri }}
          PROJECT_ID: ${{ secrets.GHAYMAH_PROJECT_ID }}
          APP_ID: ${{ secrets.GHAYMAH_APP_ID_PRODUCTION }}
          PULL_SECRET: ${{ secrets.DOCKERHUB_PULL_SECRET_NAME }}
        run: |
          jq -n \
            --arg id "${APP_ID}" \
            --arg name "nodeapp-production" \
            --arg projectId "${PROJECT_ID}" \
            --arg image "${IMAGE_URI}" \
            --arg pullSecret "${PULL_SECRET}" \
            '{
              id: $id,
              name: $name,
              projectId: $projectId,
              ports: [{ expose: true, number: 5000 }],
              publicAccess: {
                enabled: true,
                domain: "auto"
              },
              resourceTier: "t1",
              container: {
                image: $image,
                pullSecretName: $pullSecret
              }
            }' > .ghaymah.json

          cat .ghaymah.json

      - name: Deploy to Ghaymah
        working-directory: nodeapp
        run: $HOME/ghaymah/bin/gy resource app launch --env production
