Skip to main content

Command Palette

Search for a command to run...

Getting Started with CI/CD Using GitHub Actions and Docker

Updated
7 min read
M

Mohamad's interest is in Programming (Mobile, Web, Database and Machine Learning). He is studying at the Center For Artificial Intelligence Technology (CAIT), Universiti Kebangsaan Malaysia (UKM).

Introduction

Continuous Integration and Continuous Deployment (CI/CD) are widely used practices in modern software development and cloud administration. Instead of manually building, testing, and deploying applications, organizations use automation tools to perform these tasks whenever code changes occur.

One of the easiest ways to explore CI/CD is by using GitHub Actions. GitHub Actions is a built-in automation platform provided by GitHub that allows workflows to run automatically when events occur within a repository.

In this tutorial, readers will build a simple web application, create a Docker image, and configure GitHub Actions to automatically build, test, and publish the image whenever code is pushed to GitHub.

The lab environment consists of:

  • Windows 11 Host

  • VirtualBox

  • Xubuntu 20.04 Virtual Machine

  • Docker Engine

  • Git

  • GitHub Account

  • Docker Hub Account

By the end of this tutorial, readers will have a working CI/CD pipeline that automatically builds and publishes Docker images.


Learning Objectives

After completing this tutorial, readers should be able to:

  • Create a GitHub repository

  • Build Docker images

  • Create GitHub Actions workflows

  • Understand CI/CD pipelines

  • Configure GitHub secrets

  • Publish Docker images automatically

  • Understand automated build and deployment processes


Understanding the CI/CD Workflow

Before starting, it is useful to understand the workflow that will be implemented.

Developer Updates Code
            ↓
Git Push
            ↓
GitHub Repository
            ↓
GitHub Actions
            ↓
Build Docker Image
            ↓
Test Docker Image
            ↓
Push Image to Docker Hub

Whenever code is pushed to GitHub, the workflow runs automatically.

No manual deployment steps are required.


Prerequisites

Ensure the following accounts are available:

GitHub Account

Create a free GitHub account if necessary.

Docker Hub Account

Create a free Docker Hub account.

Readers will need:

  • Docker Hub Username

  • Docker Hub Access Token


Activity 1 – Creating the Project

Create a project directory:

mkdir github-cicd-demo
cd github-cicd-demo

Create a web page:

mousepad index.html &

Add:

<!DOCTYPE html>
<html>
<head>
    <title>CI/CD Demo</title>
</head>
<body>

<h1>Hello from GitHub Actions</h1>

<p>This page was deployed using a CI/CD pipeline.</p>

</body>
</html>

Save the file.


Activity 2 – Creating a Docker Image

Create:

mousepad Dockerfile &

Add:

FROM nginx

COPY index.html /usr/share/nginx/html/index.html

Save the file.

Build the image:

docker build -t cicd-demo:v1 .

Verify:

docker images

Example output:

REPOSITORY    TAG
cicd-demo     v1

Activity 3 – Testing the Container

Run the image:

docker run -d \
  --name cicd-demo \
  -p 8080:80 \
  cicd-demo:v1

Open Firefox:

http://localhost:8080

Readers should see the custom web page.

Stop the container:

docker rm -f cicd-demo

Activity 4 – Creating a GitHub Repository

Create a new repository on GitHub.

Suggested repository name:

github-cicd-demo

Do not initialize the repository with:

  • README

  • .gitignore

  • License

Create the repository.

GitHub will display instructions for connecting a local repository.


Activity 5 – Initializing Git

Inside the project directory:

git init

Configure identity:

git config --global user.name "Your Name"
git config --global user.email "you@example.com"

Add files:

git add .

Commit:

git commit -m "Initial project"

Connect to GitHub:

git remote add origin https://github.com/USERNAME/github-cicd-demo.git

Push:

git branch -M main
git push -u origin main

Refresh GitHub.

The project files should now appear online.


Activity 6 – Creating the Workflow Directory

Create:

mkdir -p .github/workflows

Verify:

tree

Expected structure:

.github
└── workflows

GitHub automatically detects workflow files stored in this location.


Activity 7 – Creating the CI/CD Workflow

Create:

mousepad .github/workflows/docker-ci-cd.yml &

Add:

name: Docker CI CD Demo

on:
  push:
    branches:
      - main

jobs:

  build-test-push:

    runs-on: ubuntu-latest

    steps:

      - name: Checkout Repository
        uses: actions/checkout@v4

      - name: Build Docker Image
        run: |
          docker build \
          -t YOUR_DOCKERHUB_USERNAME/cicd-demo:latest .

      - name: Test Docker Image
        run: |
          docker run --rm \
          YOUR_DOCKERHUB_USERNAME/cicd-demo:latest \
          nginx -t

      - name: Login to Docker Hub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Push Docker Image
        run: |
          docker push \
          YOUR_DOCKERHUB_USERNAME/cicd-demo:latest

Replace:

YOUR_DOCKERHUB_USERNAME

with the actual Docker Hub username.

Save the file.


Understanding the Workflow

The workflow consists of several stages.

Trigger

on:
  push:
    branches:
      - main

The workflow runs whenever code is pushed to the main branch.


Build Stage

docker build

Creates a Docker image.


Test Stage

docker run

Starts the image and validates the Nginx configuration.


Authentication Stage

docker/login-action

Logs into Docker Hub securely.


Deployment Stage

docker push

Publishes the image.


Activity 8 – Creating GitHub Secrets

Open the repository on GitHub.

Navigate to:

Settings
→ Secrets and Variables
→ Actions

Create:

DOCKERHUB_USERNAME

Value:

your-dockerhub-username

Create:

DOCKERHUB_TOKEN

Value:

dockerhub-access-token

These values are encrypted and hidden from the workflow logs.


Creating a Docker Hub Access Token

Open Docker Hub.

Navigate to:

Account Settings
→ Security
→ Access Tokens

Create:

GitHub Actions Token

Copy the generated token.

Store the token in:

DOCKERHUB_TOKEN

inside GitHub Secrets.


Activity 9 – Triggering the Pipeline

Add the workflow file:

git add .

Commit:

git commit -m "Added GitHub Actions workflow"

Push:

git push

Activity 10 – Monitoring the Workflow

Open GitHub.

Navigate to:

Repository
→ Actions

The workflow should begin automatically.

Readers should see stages such as:

Checkout Repository
Build Docker Image
Test Docker Image
Login to Docker Hub
Push Docker Image

A green check mark indicates success.


Activity 11 – Verifying Deployment

Open Docker Hub.

Navigate to:

Repositories

Locate:

cicd-demo

The image should now appear.

The image was automatically built and published by GitHub Actions.

No manual Docker commands were executed on Docker Hub.


Testing the Pipeline Again

Modify:

index.html

Change:

<h1>Hello from GitHub Actions</h1>

to:

<h1>Version 2</h1>

Commit:

git add .
git commit -m "Updated website"
git push

Return to:

GitHub
→ Actions

A new workflow execution should start automatically.

After completion, Docker Hub receives the updated image.


Understanding Continuous Integration

Continuous Integration focuses on automatically validating code changes.

Examples include:

  • Building applications

  • Running tests

  • Checking syntax

  • Performing security scans

In this tutorial:

Git Push
     ↓
Build
     ↓
Test

represents Continuous Integration.


Understanding Continuous Deployment

Continuous Deployment focuses on automatically publishing successful builds.

In this tutorial:

Successful Build
          ↓
Docker Push
          ↓
Docker Hub

represents Continuous Deployment.


Benefits of CI/CD

CI/CD provides several advantages.

Faster Delivery

Changes reach deployment targets quickly.

Consistency

Every build follows the same process.

Reduced Errors

Manual deployment mistakes are minimized.

Improved Quality

Automated testing catches issues early.

Better Collaboration

Multiple contributors can work efficiently.


Real-World Examples

Organizations commonly use CI/CD pipelines to:

  • Build container images

  • Deploy web applications

  • Deploy cloud infrastructure

  • Run automated testing

  • Perform security scanning

  • Publish software releases

Popular platforms include:

  • GitHub Actions

  • Jenkins

  • GitLab CI/CD

  • Azure DevOps

  • AWS CodePipeline


Conclusion

In this tutorial, readers created a complete CI/CD pipeline using GitHub Actions and Docker.

The workflow automatically:

  1. Detects code changes.

  2. Builds a Docker image.

  3. Tests the image.

  4. Authenticates to Docker Hub.

  5. Publishes the image.

Although the project was intentionally simple, it demonstrates the same workflow used by many organizations to automate software delivery and cloud deployments. By combining Git, Docker, GitHub Actions, and Docker Hub, readers gain practical experience with technologies that form the foundation of modern DevOps practices.

5 views