name: CI

# Controls when the action will run. Triggers the workflow on push to master or development 
# with a tag like v1.0.0 or v1.0.0-dev
on:
  push:
    tags: 
      - v[0-9]+.[0-9]+.[0-9]+
      - v[0-9]+.[0-9]+.[0-9]+-[a-zA-Z]+

jobs:
  build:
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - uses: actions/checkout@v2
      
      - name: Get Version
        id: get_version
        uses: battila7/get-version-action@v2.0.0
      
      - name: install buildx
        id: buildx
        uses: crazy-max/ghaction-docker-buildx@v1
        with:
          version: latest

      - name: Docker Login
        # You may pin to the exact commit or the version.
        # uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
        uses: docker/login-action@v1.10.0
        with:
          registry: ${{ secrets.DR_URL }}
          # Username used to log against the Docker registry
          username: ${{ secrets.DH_USERNAME }}
          # Password or personal access token used to log against the Docker registry
          password: ${{ secrets.DH_PASSWORD }}
          # Log out from the Docker registry at the end of a job
          logout: true

      - name: Docker Build & Push
        env:
          IMAGE_TAG: ${{ steps.get_version.outputs.version-without-v }}
        run: |
          docker buildx build --push \
          --tag ${{ secrets.DR_URL }}/goff:$IMAGE_TAG \
          --platform linux/amd64,linux/arm/v7,linux/arm64 . 

      - name: Update deployment file
        run: TAG=${{ steps.get_version.outputs.version-without-v }} && sed -i 's|<IMAGE>|${{ secrets.DR_URL }}/goff:'${TAG}'|' $GITHUB_WORKSPACE/deployment.yml

      - uses: azure/k8s-set-context@v1
        with:
          method: kubeconfig
          kubeconfig: ${{ secrets.KUBE_CONFIG }}
        id: setcontext

      - name: Deploy to Kubernetes
        run: kubectl apply -f $GITHUB_WORKSPACE/deployment.yml

      - name: Verify deployment
        run: kubectl rollout status deployment/goff -n discord-bots
