<p>I have setup a kubernetes cluster on AWS using <code>kops</code>.</p>
<p>I am trying to automate deployment with github actions.</p>
<pre><code>name: Build and Deploy

on:
  push:
    branches:
      - develop

jobs:
  build_docker_image:
    - uses: actions/checkout@v2
    - name: Build the tagged Docker image
      run: docker build --target dev -t org/customer-service-backend:la
  push_docker_image_to_github-packages:
    - uses: docker/build-push-action@v2
      with:
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
        registry: docker.pkg.github.com
        repository: org/customer-service-backend:latest
        tag_with_ref: true
  deploy_to_kubernetes_cluster:
    ... what to do here?
</code></pre>
<p>I am able to built the image and push to gihub packages.</p>
<p>I have created <code>deployment.yml</code> in the root directory of the repository.</p>
<p><em>How can I deploy to kubernetes cluster?</em></p>
<p><em>Also, I am tagging the images with <code>latest</code>. Is it fine or I need to use <code>GITHUB_REF</code> for tagging?</em></p>
<p><em>Update</em></p>
<p>I am able to configure all the things. I only need to get <code>kubeconfig</code> to authenticate to existing cluster.</p>
<pre><code>name: Build and Deploy

on:
  push:
    branches:
      - develop

jobs:

  build_docker_image:
    - uses: actions/checkout@v2
    - name: Build the tagged Docker image
      run: docker build --target dev -t org/customer-service-backend:${{ github.sha }}

  push_docker_image_to_github_packages:
    needs: build_docker_image
    - uses: docker/build-push-action@v2
      with:
        username: ${{ github.actor }}
        password: ${{ secrets.GITHUB_TOKEN }}
        registry: docker.pkg.github.com
        repository: org/customer-service-backend:${{ github.sha }}
        tag_with_ref: true

  deploy_to_kubernetes_cluster:
    needs: push_docker_image_to_github_packages
    name: Set Kubernetes Context
    uses: azure/k8s-set-context@v1
      with:
        method: kubeconfig
        kubeconfig: ${{ secrets.KUBE_CONFIG }} # Use secret (https://developer.github.com/actions/managing-workflows/storing-secrets/)
    run: |
      sed -i'' -e 's/IMAGE_LABEL/${{ github.sha }}/g' deployment.yml
      kubectl apply -f deployment.yml



</code></pre>

<p>By looking your workflow config file, all the jobs running parallelly.</p>
<p>But, probably it's not what you want.</p>
<p>Pushing image needs a built image and deployment job needs an updated built image.</p>
<p>On Access kubernetes cluster, just access into your cluster and do,</p>
<pre><code>cat $HOME/.kube/config
</code></pre>
<p>and copy the output.</p>
<p>Now, create a secret in <code>github</code> with <code>KUBE_CONFIG</code> as environment variable.</p>
<p><em>Notes - this is one method to access <code>kubernetes</code> cluster, there are other methods as well, choose one that suits your need</em></p>
<pre><code>name: Build and Deploy

on:
  push:
    branches:
      - develop

jobs:

  build_docker_image:
      name: Build Docker Image
      runs-on: ubuntu-latest
      steps:
        - name: Checkout Repo
          uses: actions/checkout@v2
        - name: Build the tagged Docker image
          run: docker build --target dev -t your_org/customer-service-backend:${{ github.sha }} .

  push_docker_image_to_github_packages:
    name: Push Docker Image to Github Packages
    needs: build_docker_image
    runs-on: ubuntu-latest
    steps:
      - name: Push Docker Image
        uses: docker/build-push-action@v2
        with:
          username: ${{ github.actor }}
          password: ${{ secrets.GITHUB_TOKEN }}
          registry: docker.pkg.github.com
          repository: your_org/customer-service-backend:${{ github.sha }}

  deploy_to_kubernetes_cluster:
    name: Deploy to Kubernetes Cluster
    needs: push_docker_image_to_github_packages
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v2
      - name: Set Kubernetes Context
        uses: azure/k8s-set-context@v1
        with:
          method: kubeconfig
          kubeconfig: ${{ secrets.KUBE_CONFIG }} # Use secret (https://developer.github.com/actions/managing-workflows/storing-secrets/)
      - name: Deploy to Cluster
        run: |
          sed -i'' -e 's/IMAGE_LABEL/${{ github.sha }}/g' deployment.yml
          kubectl apply -f deployment.yml

</code></pre>
