name: TESTING Build and Push Docker Image to ECR.

on:
 push:
   branches:
     - mypropertyqr-test

env:
  ECR_REPOSITORY: fmb-refixing-v2-test
  EKS_CLUSTER_NAME: mypropertyqr-backend-eks 
  AWS_REGION: ap-south-1

jobs:
  
  build:
    
    name: test-mypropertyqr-fmb-refixing-v2-Deployment
    runs-on: ubuntu-latest

    steps:

    - name: Set short git commit SHA
      id: commit
      uses: prompt/actions-commit-hash@v2

    - name: Check out code
      uses: actions/checkout@v2
    
    - name: Configure AWS credentials
      uses: aws-actions/configure-aws-credentials@v1
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: ${{env.AWS_REGION}}

    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/amazon-ecr-login@v1

    - name: Log in to AWS ECR
      run: |
         aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.TESTING_ECR_REPOSITORY }}

    - name: Build Docker image
      run: |
         docker build -t ${{ secrets.TESTING_ECR_REPOSITORY }}:latest .

    - name: Tag Docker image
      run: |
         docker tag ${{ secrets.TESTING_ECR_REPOSITORY }}:latest ${{ secrets.TESTING_ECR_REPOSITORY }}:latest

    - name: Push Docker image to AWS ECR
      run: |
         docker push ${{ secrets.TESTING_ECR_REPOSITORY }}:latest
         
    - name: Update kube config
      run: aws eks update-kubeconfig --name $EKS_CLUSTER_NAME --region $AWS_REGION

    - name: Deploy to EKS.
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}        
        IMAGE_TAG: ${{ steps.commit.outputs.short }}
      run: |
        kubectl get pods -A
        # kubectl delete -f manifests/test-mypropertyqr-fmb-refixing-v2-deployment.yaml
        # kubectl apply -f manifests/test-mypropertyqr-fmb-refixing-v2-deployment.yaml
        kubectl rollout restart deployment test-mypropertyqr-fmb-refixing-v2-deploymentt -n mypropertyqr-test
        kubectl get pods -A
        echo "https://test-api.sreeragu.com/api/v2/test/dbtestor" AWS EKS TESTING application sucessfully deployed. 
        
