name: Build and Deploy Pipeline

on:
  push:
    branches:
      - main

env:
  IMAGE_NAME: my-app

jobs:
  build-and-push:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Login to Ghaymah Container Registry
        uses: docker/login-action@v3
        with:
          registry: ${{ secrets.GHAYMAH_REGISTRY }}
          username: ${{ secrets.GHAYMAH_USERNAME }}
          password: ${{ secrets.GHAYMAH_TOKEN }}

      - name: Build Docker Image
        run: |
          docker build -t ${{ secrets.GHAYMAH_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }} .

      - name: Push Docker Image
        run: |
          docker push ${{ secrets.GHAYMAH_REGISTRY }}/${{ env.IMAGE_NAME }}:${{ github.sha }}

  deploy:
    needs: build-and-push

    runs-on: ubuntu-latest

    environment:
      name: production

    steps:
      - name: Deploy Application
        run: echo "Deploying application to production"