confusedWarrior
confusedWarrior

Reputation: 1400

Deploy to kubernetes cluster with github workflow

I have setup a kubernetes cluster on AWS using kops.

I am trying to automate deployment with github actions.

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?

I am able to built the image and push to gihub packages.

I have created deployment.yml in the root directory of the repository.

How can I deploy to kubernetes cluster?

Also, I am tagging the images with latest. Is it fine or I need to use GITHUB_REF for tagging?

Update

I am able to configure all the things. I only need to get kubeconfig to authenticate to existing cluster.

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



Upvotes: 3

Views: 4764

Answers (1)

Sujeet
Sujeet

Reputation: 3810

By looking your workflow config file, all the jobs running parallelly.

But, probably it's not what you want.

Pushing image needs a built image and deployment job needs an updated built image.

On Access kubernetes cluster, just access into your cluster and do,

cat $HOME/.kube/config

and copy the output.

Now, create a secret in github with KUBE_CONFIG as environment variable.

Notes - this is one method to access kubernetes cluster, there are other methods as well, choose one that suits your need

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

Upvotes: 7

Related Questions