
name: WebhookListenerAutodDeployment

on:
  push:
    branches:
    - autod-feature/*

env:
  FULL_IMAGE_NAME: gcr.io/arborknot-dev/webhook-listener
  IMAGE_NAME: webhook-listener

jobs:
  CreateFeatureDockerImagePushAndDeploy:
    runs-on: ubuntu-latest
    permissions:
      id-token: 'write'
      contents: 'read'
    steps:
    - name: Checkout
      uses: actions/checkout@v3
    - name: 'Authenticate to Google Cloud'
      id: auth
      uses: 'google-github-actions/auth@v0.7.0'
      with:
        token_format: 'access_token'
        workload_identity_provider: '${{ secrets.GCP_DEV_WORKLOAD_IDENTITY_ID }}'
        service_account: '${{ secrets.GCP_DEV_SERVICE_ACCOUNT }}'
    - name: Login To Docker
      uses: docker/login-action@v1
      with:
        registry: gcr.io
        username: oauth2accesstoken
        password: '${{ steps.auth.outputs.access_token }}'
    - name: Update Image Name
      run: |
        FULL_BRANCH_NAME=${GITHUB_REF_NAME}
        echo "IMAGE_NAME=${{ env.IMAGE_NAME }}-${FULL_BRANCH_NAME#*/}" >> $GITHUB_ENV
        echo "FULL_IMAGE_NAME=${{ env.FULL_IMAGE_NAME }}-${FULL_BRANCH_NAME#*/}" >> $GITHUB_ENV
    - name: Build And Push Dev Image
      uses: docker/build-push-action@v2
      with:
        push: true
        context: .
        tags: ${{ env.FULL_IMAGE_NAME }}:${{ github.sha }}
        cache-from: |
          type=registry,ref=${{ env.FULL_IMAGE_NAME }}-builder:${{ github.event.before }}
          type=registry,ref=${{ env.FULL_IMAGE_NAME }}:${{ github.event.before }}
        build-args: "NPM_TOKEN=${{ secrets.NPM_TOKEN }}"
    
    - name: Trigger Autod Deployment Workflow
      uses: benc-uk/workflow-dispatch@v1
      with: 
        workflow: AutodDeploymentWorkflow
        token: ${{ secrets.DEV_ENV_PIPELINE_TOKEN }}
        inputs: '{ "branch": "${{ github.ref_name }}", "image": "${{ env.IMAGE_NAME }}", "tag": "${{ github.sha }}" }'
        repo: "Arborknot/AK-Devops"
        ref: "main"
    
  UpdateCache:
    runs-on: ubuntu-latest
    permissions:
      id-token: 'write'
      contents: 'read'
    needs: CreateFeatureDockerImagePushAndDeploy
    steps:
    - name: Checkout Code
      uses: actions/checkout@v3
    - name: 'Authenticate to Google Cloud'
      id: auth
      uses: 'google-github-actions/auth@v0.7.0'
      with:
        token_format: 'access_token'
        workload_identity_provider: '${{ secrets.GCP_DEV_WORKLOAD_IDENTITY_ID }}'
        service_account: '${{ secrets.GCP_DEV_SERVICE_ACCOUNT }}'
    - name: Login To Docker
      uses: docker/login-action@v1
      with:
        registry: gcr.io
        username: oauth2accesstoken
        password: '${{ steps.auth.outputs.access_token }}'
    - name: Build And Push Dev Image
      uses: docker/build-push-action@v2
      with:
        push: true
        context: .
        tags: ${{ env.FULL_IMAGE_NAME}}-builder:${{ github.sha }}
        cache-from: |
          type=registry,ref=${{ env.FULL_IMAGE_NAME }}-builder:${{ github.event.before }}
        build-args: "NPM_TOKEN=${{ secrets.NPM_TOKEN }}"
        target: builder
