# This workflow will build and push a new container image to Amazon ECR
#
# To use this workflow, you will need to complete the following set-up steps:
#
# 1. Create an ECR repository to store your images.
#    For example: `aws ecr create-repository --repository-name my-ecr-repo --region us-east-2`.
#    Replace the value of `ECR_REPOSITORY` in the workflow below with your repository's name.
#    Replace the value of `aws-region` in the workflow below with your repository's region.

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

env:
  ECR_REGISTRY: xxxxx.dkr.ecr.ap-southeast-1.amazonaws.com
  ECR_REPOSITORY: demoapp1

name: APP DEMO CI/CD EKS

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

    steps:
    - name: Checkout
      uses: actions/[email protected]

    - name: Make envfile
      uses: SpicyPizza/[email protected]
      with:
        envkey_NODE_ENV: production
        envkey_SERVER_PORT: 3000
        envkey_AUTH_API_URL: ${{ secrets.AUTH_API_URL }}
        envkey_API_KEY: ${{ secrets.API_KEY }}
        directory: .
        file_name: .env.local

    - name: Configure AWS credentials
      uses: aws-actions/[email protected]
      with:
        aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
        aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
        aws-region: ap-southeast-1

    - name: Login to Amazon ECR
      id: login-ecr
      uses: aws-actions/[email protected]

    - name: Build, tag, and push image to Amazon ECR
      id: build-image
      env:
        ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
        ECR_REPOSITORY: demoapp1
      run: |
        SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`
        ls -la
        echo "Cat .env.local"
        cat .env.local
        echo "Build a docker container and push it to ECR" 
        docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:master-$SHORT_SHA .
        echo "Pushing image to ECR..."
        docker push $ECR_REGISTRY/$ECR_REPOSITORY:master-$SHORT_SHA
        echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:master-$SHORT_SHA"

  deploy:
    name: Deploy
    runs-on: self-hosted
    needs: build

    steps:
    - name: Check out code
      uses: actions/[email protected]

    - name: Setup Kustomize
      uses: imranismail/[email protected]
      with:
        kustomize-version: "3.8.8"

    - name: Update Kubernetes resources
      run: |
        cd ~/k8s-eks-manifest
        git pull
        git config --global user.email "[email protected]"
        git config --global user.name "devops"
        git checkout master

        echo "Update newimage with kustomize"
        SHORT_SHA=`echo ${GITHUB_SHA} | cut -c1-8`
        cd kustomize/demoapp1
        kustomize edit set image $ECR_REGISTRY/$ECR_REPOSITORY:master-$SHORT_SHA
        cat kustomization.yaml
        git commit -am "[${{ github.repository }}] $GITHUB_ACTOR - ${{ github.event.head_commit.message }}"
        git push -u origin master

    - name: Sync ArgoCD App
      run: |
        argocd --insecure app sync ${{ secrets.ARGOCD_APP_PRODUCTION }}