Skip to content

saadcnx/docker-cicd-github-actions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Docker CI/CD β€” Automated Builds with GitHub Actions

A complete CI/CD pipeline that automatically builds, tests, and publishes a Docker image to Docker Hub on every push β€” powered by GitHub Actions and Docker Compose.


πŸ“¦ Tech Stack

Tool Purpose
Node.js + Express Web application backend
Docker Container runtime
Docker Compose Multi-service local orchestration
GitHub Actions CI/CD automation
Docker Hub Container image registry
Nginx Reverse proxy

πŸ—οΈ Pipeline Architecture

 Developer pushes code
         β”‚
         β–Ό
  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
  β”‚     GitHub Actions Workflow      β”‚
  β”‚                                  β”‚
  β”‚  Job 1: build-and-test           β”‚
  β”‚  β”œβ”€β”€ Install dependencies        β”‚
  β”‚  β”œβ”€β”€ Run unit tests              β”‚
  β”‚  β”œβ”€β”€ Build Docker image          β”‚
  β”‚  β”œβ”€β”€ Test Docker container       β”‚
  β”‚  └── Docker Compose integration  β”‚
  β”‚         tests                    β”‚
  β”‚                                  β”‚
  β”‚  Job 2: build-and-push           β”‚
  β”‚  (only on main branch)           β”‚
  β”‚  β”œβ”€β”€ Login to Docker Hub         β”‚
  β”‚  β”œβ”€β”€ Build & tag image           β”‚
  β”‚  β”œβ”€β”€ Push to Docker Hub          β”‚
  β”‚  └── Verify pushed image         β”‚
  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚
         β–Ό
   Docker Hub Registry
   └── saadcnx/docker-cicd-github-action:latest
   └── saadcnx/docker-cicd-github-action:main-49356c6

πŸ“ Project Structure

.
β”œβ”€β”€ .github/
β”‚   └── workflows/
β”‚       └── docker.yml          # GitHub Actions CI/CD workflow
β”œβ”€β”€ scripts/
β”‚   └── integration-test.sh     # Integration test script
β”œβ”€β”€ app.js                      # Express web server
β”œβ”€β”€ test.js                     # Health check test
β”œβ”€β”€ package.json                # Node.js dependencies
β”œβ”€β”€ Dockerfile                  # Container image definition
β”œβ”€β”€ docker-compose.yml          # Local development stack
β”œβ”€β”€ docker-compose.test.yml     # CI/CD integration test stack
└── nginx.conf                  # Nginx reverse proxy config

πŸš€ Getting Started

Prerequisites

Run Locally

git clone https://github.com/saadcnx/docker-cicd-github-actions.git
cd docker-cicd-github-actions
# Start the full stack
docker-compose up -d

# Run integration tests
./scripts/integration-test.sh

# Tear down
docker-compose down

App will be at http://localhost:3009 and via Nginx at http://localhost:8080


πŸ”— API Endpoints

Method Endpoint Description
GET / Returns app info and version
GET /health Health check + uptime

Example

curl http://localhost:3009/
# {"message":"Hello from Docker CI/CD Demo!","timestamp":"...","version":"1.0.0"}

curl http://localhost:3009/health
# {"status":"healthy","uptime":42.3}

βš™οΈ CI/CD Setup

1. Create Docker Hub Repository

Go to hub.docker.com β†’ Create Repository β†’ name it docker-cicd-github-action.

2. Generate a Docker Hub Access Token

Docker Hub β†’ Account Settings β†’ Security β†’ New Access Token

Use an access token instead of your password β€” it can be revoked independently.

3. Add GitHub Secrets

In your GitHub repo β†’ Settings β†’ Secrets and variables β†’ Actions:

Secret Name Value
DOCKER_USERNAME Your Docker Hub username
DOCKER_PASSWORD Your Docker Hub access token

4. Push to Trigger the Pipeline

git add .
git commit -m "trigger: ci/cd pipeline"
git push origin main

Watch it run under the Actions tab in your GitHub repo.


πŸ”„ Workflow Jobs

build-and-test β€” runs on every push & PR

  • Installs Node.js dependencies
  • Runs unit tests against live server
  • Builds Docker image
  • Spins up container and validates /health
  • Runs full Docker Compose stack + integration tests
  • Dumps service logs on failure (always)
  • Cleans up all containers and volumes

build-and-push β€” runs on main branch only

  • Requires build-and-test to pass
  • Authenticates with Docker Hub
  • Builds and pushes image with tags:
    • latest
    • main-<commit-sha>
    • branch name
  • Pulls and smoke-tests the pushed image

🏷️ Image Tags on Docker Hub

Tag When created
latest Every push to main
main-<sha> Every push to main (commit-pinned)
<branch-name> Any branch push

Pull the latest image:

docker pull saadcnx/docker-cicd-github-action:latest
docker run -p 3009:3000 saadcnx/docker-cicd-github-action

πŸ§ͺ Running Tests

Unit Test

npm install
npm start &
npm test

Integration Tests (Docker Compose)

docker-compose up -d
sleep 15
./scripts/integration-test.sh
docker-compose down

The integration script validates:

  • App health endpoint responds correctly
  • App returns expected JSON payload
  • Nginx proxy forwards requests properly
  • 10-request load test completes without errors

πŸ› Troubleshooting

Docker Hub auth fails in pipeline

  • Confirm secret names are exactly DOCKER_USERNAME and DOCKER_PASSWORD
  • Use an access token, not your account password
  • Ensure the Docker Hub repo exists before the first push

Services don't start in CI

  • Increase sleep durations in workflow steps
  • Check health check intervals in docker-compose.yml
  • Review logs in the workflow's "Check service logs" step

Tests pass locally but fail in CI

  • Services may need more startup time in CI environments
  • Verify no port conflicts in the runner
  • Check that all files referenced in docker-compose.yml are committed

Image build fails

  • Run docker build . locally to reproduce the error
  • Ensure .dockerignore doesn't exclude required files
  • Verify the base image tag is valid

SCREENSHOTS

github_prof docker_hub

πŸ”’ Security Practices

  • No secrets or credentials committed to the repository
  • Docker Hub credentials stored exclusively as GitHub Secrets
  • Container runs as a non-root user (nodejs uid 1001)
  • Access tokens used instead of account passwords
  • Images tagged with commit SHAs for full traceability

πŸ“„ License

MIT β€” free to use, modify, and distribute.

About

Production-grade GitHub Actions workflow for automated Docker CI/CD. Includes build & test jobs, multi-architecture builds, Docker Compose integration testing, health checks, and automated Docker Hub registry publishing with semantic versioning.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors