Trigger a Kubernetes Deployment from a Dockerhub Push Event
Learn how to trigger a Kubernetes deployment when an image is updated
In this example, we will cover how to trigger a Kubernetes deployment from a Dockerhub Push event using a Dockerhub registry trigger.
Our example will have two pipelines. One that is responsible for packaging code (CI), and the other will be responsible for deploying code (CD).
Prerequisites
- A free Codefresh account
- A DockerHub registry connected to your Codefresh account
- A Kubernetes cluster connected to your Codefresh account
- A service for your application deployed to your cluster
The Example Project
You can see the example project on GitHub. The repository contains a simple Hello World NodeJs app as well as 2 pipelines.
Create the CI Pipeline
As mentioned before, our first pipeline will handle the CI process. There will be 3 stages:
- A stage for cloning
- A stage for building the image
- A stage for pushing the image to DockerHub
codefresh-CI-pipeline.yml
version: '1.0'
stages:
- checkout
- build
- push
steps:
clone:
title: Cloning main repository...
type: git-clone
stage: checkout
arguments:
repo: 'codefresh-contrib/registry-trigger-sample-app'
revision: 'master'
git: github
build_my_app:
title: Building image...
type: build
stage: build
arguments:
image_name: registry-trigger-sample-app
working_directory: ${{clone}}
tag: 'master'
dockerfile: Dockerfile
push_to_my_registry:
stage: 'push'
type: push
title: Pushing to Dockerhub...
arguments:
candidate: ${{build_my_app}}
tag: 'latest'
registry: dockerhub
image_name: annabaker/registry-trigger-sample-app
This pipeline does the following:
- Clones the source code with a Git clone step
- Builds a docker image tagged with the Application version using a build step
- Pushes the Docker image using a Push step to the DockerHub registry you have integrated with Codefresh.
Create the CD Pipeline
This pipeline will only contain one stage/step, for deploying.
Note that for the trigger mechanism to take place, you will need to add a DockerHub registry trigger to the pipeline.
codefresh-CD-pipeline.yml
version: "1.0"
stages:
- "deploy"
steps:
deploy_to_k8s:
title: Running Deploy Script...
type: deploy
kind: kubernetes
arguments:
cluster: anna-demo@FirstKubernetes
namespace: default
service: registry-trigger-sample-app
candidate:
image: annabaker/registry-trigger-sample-app:latest
registry: 'dockerhub'
This pipeline does the following:
- Uses a Deploy step to deploy the image to Kubernetes. The deploy step uses a Registry trigger to kick off the pipeline when the updated image is pushed to the registry.