AWS Builder Center
Building a Simple CI/CD Pipeline with GitHub Actions and Docker

Building a Simple CI/CD Pipeline with GitHub Actions and Docker

Modern applications need a reliable way to build, test, and deploy code whenever changes are pushed. CI/CD helps automate this process and reduces the amount of manual work required from developers and DevOps engineers.

Building a Simple CI/CD Pipeline with GitHub Actions and Docker

Modern applications need a reliable way to build, test, and deploy code whenever changes are pushed. CI/CD helps automate this process and reduces the amount of manual work required from developers and DevOps engineers.
In this article, we will build a simple CI/CD workflow using GitHub Actions and Docker. The goal is to understand the basic flow rather than create a production-grade pipeline.

What We Are Building

Our pipeline will follow this flow:
Developer
|
v
Git Push
|
v
GitHub Repository
|
v
GitHub Actions
|
+----> Install dependencies
|
+----> Run tests
|
+----> Build Docker image
|
v
Docker Image

The important idea is that every code change can automatically go through the same validation process.

Why CI/CD?

Without CI/CD, a typical deployment process might look like this:
  1. Make changes locally.
  1. Run tests manually.
  1. Build the application.
  1. Build a Docker image.
  1. Push the image.
  1. Deploy the application.
As the project grows, repeating these steps manually becomes error-prone.
A CI/CD pipeline can automate many of these tasks. Continuous Integration (CI) focuses on automatically building and testing changes, while Continuous Delivery or Deployment (CD) extends the process toward releasing those changes.

Prerequisites

For this example, you need:
  • A GitHub repository
  • A Dockerfile
  • An application that can be tested
  • GitHub Actions enabled for the repository
You don't need to run a GitHub Actions runner yourself. GitHub provides hosted runners that can execute workflow jobs.

Step 1: Create a Dockerfile

Let's assume we have a simple Node.js application.
A basic Dockerfile could look like this:
FROM node:22-alpine

WORKDIR /app

COPY package*.json ./

RUN npm ci

COPY . .

EXPOSE 3000

CMD ["npm", "start"]

This Dockerfile creates an image containing the application and its dependencies.
Using a Docker image gives us a consistent environment for running the application.

Step 2: Create the GitHub Actions Workflow

Create the following file in your repository:
.github/workflows/ci.yml

A simple workflow can look like this:
name: CI

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
build-and-test:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: 22
cache: npm

- name: Install dependencies
run: npm ci

- name: Run tests
run: npm test

- name: Build Docker image
run: docker build -t my-app:${{ github.sha }} .

Let's break this down.

Triggering the Workflow

on:
push:
branches:
- main
pull_request:
branches:
- main

This tells GitHub Actions to run the workflow when:
  • Code is pushed to the  main  branch.
  • A pull request targets the  main  branch.
For larger teams, running CI on pull requests is particularly useful because problems can be detected before changes are merged.

Running the Job

jobs:
build-and-test:
runs-on: ubuntu-latest

The job runs on a GitHub-hosted Ubuntu runner.
The runner provides an environment where GitHub Actions can execute our commands.

Checking Out the Code

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

The workflow runner starts with a clean environment, so we need to check out the repository before working with the application files.

Installing Dependencies

- name: Install dependencies
run: npm ci

 npm ci  installs dependencies based on the lock file and is generally appropriate for automated CI environments.
Using a lock file also helps make dependency installation more predictable.

Running Tests

- name: Run tests
run: npm test

This is one of the most important stages of CI.
If the tests fail, the workflow stops and the change can be investigated before it reaches a deployment environment.

Building the Docker Image

- name: Build Docker image
run: docker build -t my-app:${{ github.sha }} .

The final step builds the Docker image.
The  ${{ github.sha }}  expression gives the image a tag based on the commit that triggered the workflow.
This is better than always using a generic tag such as  latest  because the image can be associated with a specific source-code version.

What Happens When We Push Code?

Suppose we make a change and run:
git add .
git commit -m "Update application"
git push origin main

GitHub receives the commit and starts the workflow.
The pipeline then:
  1. Checks out the repository.
  1. Sets up Node.js.
  1. Installs dependencies.
  1. Runs tests.
  1. Builds the Docker image.
If any step fails, GitHub Actions marks the workflow as failed.
If all steps succeed, we know that the application passed the automated checks and that its Docker image can be built successfully.

Taking the Pipeline Further

The workflow above is intentionally simple. A production CI/CD pipeline would normally include additional stages.
For example:
Code Push
|
v
Lint
|
v
Unit Tests
|
v
Security Scan
|
v
Docker Build
|
v
Push Image to Registry
|
v
Deploy
|
v
Monitor

A container image could be pushed to a registry such as Amazon Elastic Container Registry (Amazon ECR).
The deployment could then target services such as Amazon ECS, Amazon EKS, or another compute platform.
Security scanning can also be introduced before an image is deployed. This helps identify vulnerable dependencies or container packages earlier in the delivery process.

Important CI/CD Practices

A few practices make CI/CD pipelines more reliable:

Keep credentials out of the repository

Never hard-code passwords, API keys, cloud credentials, or other secrets in workflow files.
Use GitHub Actions secrets, environment-specific secret management, or an appropriate cloud secret-management service.

Make builds reproducible

Use dependency lock files and predictable build steps so that the same commit produces consistent results.

Fail early

Run inexpensive checks such as linting and unit tests before expensive build or deployment stages.

Use meaningful image tags

Instead of relying only on  latest , consider immutable identifiers such as commit SHA, release version, or another unique build identifier.

Add security checks

Container and dependency scanning should be part of the software delivery lifecycle rather than something performed only after deployment.

Conclusion

CI/CD doesn't have to start with a complicated multi-stage platform.
A small pipeline that automatically checks code, runs tests, and builds a Docker image already provides significant value.
Once the basic workflow is working, additional capabilities can be introduced gradually:
  • Push images to Amazon ECR.
  • Deploy containers to Amazon ECS or Amazon EKS.
  • Add security scanning.
  • Add infrastructure provisioning with Terraform.
  • Introduce monitoring with Prometheus and Grafana.
  • Implement progressive deployment strategies such as blue/green or canary deployments.
The key is to automate the repetitive parts of software delivery while keeping every stage observable and reproducible.
For someone learning DevOps, building a small pipeline like this is a good starting point because the same concepts scale into much larger cloud-native environments.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article