How to Create a Continuous Integration Continuous Deployment (CI-CD) Solution for a Docker Project
Tags: docker, devops, github, webdev
Published: 2021-01-05 11:50:52

---
title: How to Create a Continuous Integration Continuous Deployment (CI-CD) Solution for a Docker Project
published: true
date: 2021-01-05 09:30:00 UTC
tags: docker,devops,github,webdev
canonical_url: http://www.frankysnotes.com/2021/01/how-to-create-continuous-integration.html
---

I'm not a Docker master, but I understand that it's very useful and I like to use it from time to time in some projects. Another thing I like is DevOps and automation and in a project I have, I was missing that. In the previous setup, the container was built and publish to DockerHub with the date as a tag. Nice but not very easy to now with versions are "stable" and wish one are "in progress".

This post is about how I build a continuous integration and continuous deployment solution for my docker project. All the code is on [GitHub](https://github.com/FBoucher/CloudBot) and [Docker Hub](https://hub.docker.com/repository/docker/fboucher/cloudbot). I sharing my journey so others can enjoy that automation and not spend a weekend building it.

[![](https://1.bp.blogspot.com/-YGhb-4fs3K8/X_OztuvNUOI/AAAAAAABcz4/dm04Sn9Dz8sl2qDbhjmbHS5CyeGIX_LjACLcBGAsYHQ/s320/chatbot-container.png)](https://1.bp.blogspot.com/-YGhb-4fs3K8/X_OztuvNUOI/AAAAAAABcz4/dm04Sn9Dz8sl2qDbhjmbHS5CyeGIX_LjACLcBGAsYHQ/s934/chatbot-container.png)

## The Goal

By the end of this build, there will be two GitHub Action to build and publish a different version of the application on Docker Hub.

**The release version:** every time a release is published on GitHub a container tag with the matching version number will be built and published. (ex: myapp:v1)

**The beta version:** At every push in my branch on GitHub a container will be published with a specific tag. The tag will be matching the draft release version number with `-beta`. (ex: myapp:v2-beta).

In this post, the application is a Node.js Twitch chatbot. The type of application is not important the post focus on the delivery.

## Publishing the release version

Every time a release is published on GitHub, the workflow will be triggered. It will first retrieve the "release version" then build and tag the container with it and finally publish (aka push) it to Docker hub. Because a "release" is also a "stable" version it will also update the container tag `latest`.

Let's look at the full YAML definition of the GitHub Action and I will break it down after.

```yaml
name: Release Docker Image CI

on:
  release:
    types: [published]
jobs:
  update:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Set outputs
      id: vars
      run: echo ::set-output name=RELEASE_VERSION::$(echo ${GITHUB_REF:10})
    - name: Publish to Registry
      uses: elgohr/Publish-Docker-Github-Action@master
      with:
        name: ${{secrets.DOCKER_USER}}/cloudbot
        username: ${{ secrets.DOCKER_USER }}
        password: ${{ secrets.DOCKER_PASSWORD }}
        tags: "latest,${{ steps.vars.outputs.RELEASE_VERSION }}"
```

To limit how many times the workflow is triggered I used `on: release` and the `type: published`, adjust as you like.

The next interesting part is the lines in the step **vars**.

```yaml
- name: Set outputs
    id: vars
    run: echo ::set-output name=RELEASE_VERSION::$(echo ${GITHUB_REF:10})
```

Here I use the environment variable `GITHUB_REF` (striped of the 10 first characters contains "refs/tags/") to initialize a local variable `RELEASE_VERSION`. The value is available from the outputs of that step, like on the last line of the YAML.

```yaml
tags: "latest,${{ steps.vars.outputs.RELEASE_VERSION }}"
```

From the **steps** identified by the id **vars** I retrieved from the **outputs** the value of `RELEASE_VERSION`.

In this GitHub Action, I used [elgohr/Publish-Docker-Github-Action@master](https://github.com/marketplace/actions/docker-publish-action) because it was simple and was doing what I need. You can execute docker commands directly if you prefer or use the `docker/github-actions`.

There are many options available from the [GitHub marketplace](https://github.com/marketplace?type=actions).

## Publishing the beta version

Every time a push is done on GitHub, the workflow will be triggered. It will first retrieve the "release version" of the most recent release **in draft mode**. The workflow will happen `-beta` to the retrieved version and use this to tag the container. Finally, publish (aka push) it to Docker hub.

Once more, here full YAML, I will break it down after.

```yaml
name: Build Docker Images
on: [push]
jobs:
  build:
    name: cloudbot-beta
    runs-on: ubuntu-latest
    steps:
    - id: last_release
      uses: InsonusK/get-latest-release@v1.0.1
      with:
          myToken: ${{ github.token }}
          exclude_types: "release, prerelease"
          view_top: 1  
    - uses: actions/checkout@v2
    - name: Publish to Registry
      uses: elgohr/Publish-Docker-Github-Action@master
      with:
        name: ${{secrets.DOCKER_USER}}/cloudbot
        username: ${{ secrets.DOCKER_USER }}
        password: ${{ secrets.DOCKER_PASSWORD }}
        tags: "${{ steps.last_release.outputs.tag_name }}-beta"
```

Here the difficulty was that I wanted to create a tag from a "future" version. I decided to use the Draft Releases because those are not visible by everyone, therefore they look like the future.

If your last release is version 1 (v1.0), to make this workflow possible you will need to create a new release and save it in **Draft**.

[![](https://1.bp.blogspot.com/-szP-X1DPaac/X--FP05xNKI/AAAAAAABcvY/MA7EcrQqGMglVlHSu2HNI3G57Z0ekZBLgCLcBGAsYHQ/w400-h343/draftRelease.png)](https://1.bp.blogspot.com/-szP-X1DPaac/X--FP05xNKI/AAAAAAABcvY/MA7EcrQqGMglVlHSu2HNI3G57Z0ekZBLgCLcBGAsYHQ/s874/draftRelease.png)
  

Like in the Release workflow, I need to retrieve the version. Because drafts are only visible to some people we will need to get access. This is easily done by using a `github.token`. Those are created automatically when the GitHub Action starts.

Then using the step [InsonusK/get-latest-release](https://github.com/marketplace/actions/get-latest-release-of-repository) we will retrieve the version.

```yaml
- id: last_release
    uses: InsonusK/get-latest-release@v1.0.1
    with:
        myToken: ${{ github.token }}
        exclude_types: "release, prerelease"
        view_top: 1   
```

This time when passing the value for the tag we will concatenate "-beta" to it.

```yaml
tags: "${{ steps.last_release.outputs.tag_name }}-beta"
```
  
 ![](https://1.bp.blogspot.com/-6KJ4grKM2jo/X_O1JtZz5hI/AAAAAAABc0E/hkYoDB2e6oM8bP6TvuxMon4oboaEAbs4QCLcBGAsYHQ/w400-h299/final.png) 
## Wrapping Up

And voila, a very simple and easy to implement ci-cd for a container project. There are many different options, looking forward to learning how you did yours?