DEV Community

Cover image for Deploy to Kubernetes using Github Actions (including Slack notification)

Deploy to Kubernetes using Github Actions (including Slack notification)

Leandro Proença on April 14, 2021

In this guide we'll cover the full cycle of deploying to Kubernetes using Github Actions. Batteries included: Kubernetes cluster running in AWS E...
Collapse
 
cpavlatos profile image
Xtos

This is great! Can we add a revert/rollback in the event of a failure after deployment ?

Collapse
 
leandronsp profile image
Leandro Proença

yup, that’s perfectly possible

Collapse
 
cpavlatos profile image
Xtos

I’d love to see how it will fetch the rollback sha(one previous to the failed deployment), and the previous ECR image. Would this be added in a new post, or to the current? :-)

Thread Thread
 
leandronsp profile image
Leandro Proença

A Kubernetes deployment keeps N deploy versions, aka docker images. So a rollout rollback is simply switching back to the previous image already present in the cluster. Is that you’re looking for or I’m missing some point?

Thread Thread
 
cpavlatos profile image
Xtos

Yes, very similar, just trying to incorporate to what you have developed a rollback option :-)

As a final step, i tweaked the GitHub action step with:

helm upgrade my-application -f helm/values-${{ env.ENV_NAME }}.yaml --set image.tag="${{ env.SHA }}" helm/ -n ${{ env.ENV_NAME }}-apps -v 20

I like your step to "Verify Kubernetes deployment", based on that output, i'm trying to see how i can rollback in the event of a failed deployment.

for example a GitHub action to use kubectl rollout history or kubectl set image but the later uses --record which will be deprecated.

Much appreciated...BTW, been enjoying all your articles, and looking forward to see your statefulsets on k8s!

Thread Thread
 
leandronsp profile image
Leandro Proença

Okay, I got it. Today later I’ll check your good insights and how to fit it in this example, thanks!

Collapse
 
amin_djawadi_842f8e3f8ced profile image
Amin Djawadi

Thank you Leandro, It was really helpful.

Collapse
 
jijothottungal profile image
jijothottungal

I tried these steps but failing at build satge even after trying some solutions from the internet. Could you please help?

==

0s
Run actions/cache@v2
with:
path: /tmp/.buildx-cache
key: Linux-single-buildx-a0e66f3b0efdba7f889770a006153d36b421b028
restore-keys: Linux-single-buildx

env:
RELEASE_REVISION: pr--
AWS_ACCESS_KEY_ID: ***
AWS_SECRET_ACCESS_KEY: ***
AWS_REGION: ***
KUBE_CONFIG_DATA: ***
KUBE_NAMESPACE: default
ECR_REPOSITORY: app-api
SLACK_WEBHOOK_URL: ***
AWS_DEFAULT_REGION: ***
Cache not found for input keys: Linux-single-buildx-a0e66f3b0efdba7f889770a006153d36b421b028, Linux-single-buildx
2s
Run docker buildx create --use
docker buildx create --use
docker buildx build \

--cache-from=type=local,src=/tmp/.buildx-cache \

--cache-to=type=local,dest=/tmp/.buildx-cache-new \
--tag .dkr.ecr..amazonaws.com/app-api:pr-- \

--target build-stage \

--push \

.

  rm -rf /tmp/.buildx-cache
  mv /tmp/.buildx-cache-new /tmp/.buildx-cache
Enter fullscreen mode Exit fullscreen mode

shell: /usr/bin/bash -e {0}
env:
RELEASE_REVISION: pr--
AWS_ACCESS_KEY_ID: ***
AWS_SECRET_ACCESS_KEY: ***
AWS_REGION: ***
KUBE_CONFIG_DATA: ***
KUBE_NAMESPACE: default
ECR_REPOSITORY: app-api
SLACK_WEBHOOK_URL: ***
AWS_DEFAULT_REGION: ***
ECR_REGISTRY: .dkr.ecr..amazonaws.com
RELEASE_IMAGE: .dkr.ecr..amazonaws.com/cleva-api:pr--
nice_benz
time="2021-11-09T17:04:14Z" level=warning msg="No output specified for docker-container driver. Build result will only remain in the build cache. To push result image into registry use --push or to load image into docker use --load"

1 [internal] booting buildkit

1 pulling image moby/buildkit:buildx-stable-1

1 pulling image moby/buildkit:buildx-stable-1 0.8s done

1 creating container buildx_buildkit_nice_benz0

1 creating container buildx_buildkit_nice_benz0 0.7s done

1 DONE 1.5s

error: unable to prepare context: path " " not found
Error: Process completed with exit code 1.

Collapse
 
thearvindnarayan profile image
Arvind Narayan • Edited

This should help

- name: Set up Docker Buildx
        id: buildx
        uses: docker/setup-buildx-action@master
 - name: Docker cache layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-single-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-single-buildx
  - name: Build 
        uses: docker/build-push-action@v2
        with: 
          context: .
          push: true
          tags: ${{ steps.login-ecr.outputs.registry }}/${{ env.ECR_REPOSITORY }}:${{ github.sha }}
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache-new
  - name: Move cache
        run: |
          rm -rf /tmp/.buildx-cache
          mv /tmp/.buildx-cache-new /tmp/.buildx-cache
Enter fullscreen mode Exit fullscreen mode
Collapse
 
leandrosilva profile image
Leandro Silva

You're putting in great content, bud.
Keep going, I'll be back.

Collapse
 
leandronsp profile image
Leandro Proença

I enjoy and learn with your writings! Yes, do it more 💪🏻

Collapse
 
leandrosilva profile image
Leandro Silva • Edited

Oh man, I really need to. It's been 3 months since my last post. Meh.
And btw, I'm entertaining the idea of writing in English. Let's see.

Collapse
 
vitalykarasik profile image
Vitaly Karasik

Thank you for your post!
BTW, is it possible to run a few commands inside one step, like "set image... ; get rollout status..."?

Collapse
 
ahmedappout08 profile image
ahmedappout08

i got this error from deploying the image on k8s :
The connection to the server localhost:8080 was refused - did you specify the right host or port?
How can i fix it .. thanks in advance

Collapse
 
tiagovbarreto profile image
tiagovbarreto

Hi Leandro, nice article! Do you know how to deploy an image from docker hub into aws eks? Thks!

Collapse
 
devsam profile image
Devsam

In "Deploy to Kubernetes cluster" action, I'm getting below error:

Error from server (NotFound): deployments.apps "my-pod" not found