Trigger a Kubernetes Deployment from a Docker Hub 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 Docker Hub Push event using a Dockerhub registry trigger.
Our example has two pipelines: one for packaging code (CI), and the second for deploying code (CD).
Prerequisites
- A Codefresh account
- A Docker Hub registry connected to your Codefresh account
- A Kubernetes cluster connected to your Codefresh account
- A service for your application deployed to your cluster
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.
The pipeline has three 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 through a Git clone step.
- Builds a docker image tagged with the Application version through a build step.
- Pushes the Docker image through a push step to the Docker Hub registry you have integrated with Codefresh.
Create the CD Pipeline
This pipeline contains one stage/step, for deploying.
Note that for the trigger mechanism to take place, you will need to add a Docker Hub 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:
- Deploys the image to Kubernetes through a deploy step. The deploy step uses a Registry trigger to kick off the pipeline when the updated image is pushed to the registry.
Related articles
CD pipeline examples
Codefresh YAML for pipeline definitions
Creating pipelines
How Codefresh pipelines work
Triggers in pipelines