Part 1: GitHub Actions → Docker Hub — Versioning → Continous Integrations

suyog shinde
nonstopio
Published in
4 min readJul 18, 2022

--

In this article, we will see how to build and push a docker image and push it to our docker repository (DOCKER HUB). [Continous integration]

Let’s start with creating a simple application code where we will create an index.html in the first version and some new files in the next version of the docker image.

Basic understanding of the tools we are using.

Github Actions

Github actions help us to create a custom software development lifecycle workflow. GitHub actions integrate with Github to fully perform tasks.
These tasks are performed by actions by means of events or different conditions such as push, merge, etc. It basically helps us to create a Continues Integration (CI) and continuous deployment (CD) in the repository itself.

Docker

Docker is a set of the platform as service products that use OS-level virtualization to deliver software in packages called containers. Containers are isolated from one another and bundle their own software, libraries, and configuration files.
Here we are going to use docker to build and push the image to the Docker registry.

Docker Hub

Docker hub is a platform for storing docker images and version controlling for the stored images.

Pre-requisites:
1. Docker hub account (Id and password) and a repository in it.

2. GitHub account

Step 1 →

Log in to GitHub and create a repository.

Link to my repository: docker-actions

We have a web.html in the wed_dir folder. A Simple Docker file that will build the image.

Note: .Github/workflows directory will be created after the second step.

Step 2 →
2. Next we will create workflows and secrets

A workflow is a configurable automated process that will run one or more jobs. Workflows are defined by a YAML file checked into your repository and will run when triggered by an event in your repository, or they can be triggered manually, or at a defined schedule.

Workflows are defined in the .github/workflows directory in a repository, and a repository can have multiple workflows, each of which can perform a different set of tasks.

For example in our case, you can have one workflow to build and push the docker image to the registry.

Above you can see how I created a workflow.
Repository → actions

Now you can see the .Github/workflows directory will be created

Let’s write the logic in the workflow file according to our needs.

Steps we need to add in the workflow file .Github/workflows/docker-image.yml :

name: Docker Image CI

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:

build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: docker login
env:
DOCKER_USER: ${{secrets.DOCKER_HUB_USER}}
DOCKER_PASSWORD: ${{secrets.DOCKERHUB_PASS}}
REPO_NAME: ${{secrets.REPO_NAME}}
run: |
docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
- name: Get current date # get the date of the build
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d--%M-%S')"
- name: Build the Docker image
run: docker build . --file Dockerfile --tag ${{secrets.DOCKER_HUB_USER}}/${{secrets.REPO_NAME}}:${{steps.date.outputs.date}}

- name: Push_Image
run: docker push ${{secrets.DOCKER_HUB_USER}}/${{secrets.REPO_NAME}}:${{steps.date.outputs.date}}

There are three main steps in the above workflow:

1.docker login
2.docker build
3.docker push
  1. Above in the workflow file, you can see some keywords such as ${{secrets.DOCKER_HUB_USER}}, ${{secrets.DOCKERHUB_PASS}} , ${{secrets.REPO_NAME}}.

Secrets are encrypted environment variables that you create in an organization, repository, or repository environment. The secrets that you create are available to use in GitHub Actions workflows.

These are secrets, which we need to store in GitHub secrets to authenticate with the docker hub. We can directly show it in the workflows but this is a bad practice.

Create Secrets for the workflows.

We have 3 secrets

1. DOCKER_HUB_USER

2. DOCKERHUB_PASS

3. REPO_NAME

Commit the changes made in the workflow file and the workflow will be triggered.

If anyone changes something on the main branch the actions are triggered by default but that can be changed to any branch, manual, or multiple conditions.

Let’s make some changes to the repository:

Check if the image is pushed to the docker hub and versioning is also handled using tags(Time-date format).

We have successfully completed PART 1.

Now in the next blog, PART-2:- I’ll be showing how to do Continuous Delivery with Github actions.

Do read my other blogs on various topics related to DevOps

  1. How-to-set-up-code-ready-containers-on-ubuntu

2. Setup GitLab runner on AWS-ec2

Thank you

Here to make the community stronger by sharing our knowledge. Follow me and my team to stay updated on the latest and greatest in web & mobile tech. world 🙂

--

--