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).


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 UI CI Pipeline View

Codefresh UI CI Pipeline View


version: '1.0'

- checkout
- build
- push

    title: Cloning main repository...
    type: git-clone
    stage: checkout
      repo: 'codefresh-contrib/registry-trigger-sample-app'
      revision: 'master'
      git: github
    title: Building image...
    type: build
    stage: build
      image_name: registry-trigger-sample-app
      working_directory: ${{clone}}
      tag: 'master'
      dockerfile: Dockerfile
    stage: 'push'
    type: push
    title: Pushing to Dockerhub...
      candidate: ${{build_my_app}}
      tag: 'latest'
      registry: dockerhub
      image_name: annabaker/registry-trigger-sample-app

This pipeline does the following:

  1. Clones the source code with a Git clone step
  2. Builds a docker image tagged with the Application version using a build step
  3. 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.

Codefresh UI CD Pipeline View

Codefresh UI CD Pipeline View

Note that for the trigger mechanism to take place, you will need to add a DockerHub registry trigger to the pipeline.


version: "1.0"

  - "deploy"

    title: Running Deploy Script...
    type: deploy
    kind: kubernetes
      cluster: anna-demo@FirstKubernetes
      namespace: default
      service: registry-trigger-sample-app
        image: annabaker/registry-trigger-sample-app:latest
        registry: 'dockerhub'

This pipeline does the following:

  1. 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.