Home DevOpsBuild a CI/CD Pipeline with GitHub Actions to Deploy a Docker App

Build a CI/CD Pipeline with GitHub Actions to Deploy a Docker App

by Knowledge Base Linux
Build a CI CD Pipeline with — Build a CI/CD Pipeline with GitHub Actions to Deploy a Docker App

Pushing code to GitHub, then manually building a Docker image, pushing it, and restarting containers on the server is slow and error-prone. A github actions cicd docker pipeline automates the entire flow: every push to main builds your Docker image, runs your tests, pushes the image to Docker Hub, and deploys the new version to your server — with zero manual steps. In this guide you will build that pipeline from scratch using a small Node.js demo app, and by the end you will have production-style continuous deployment running in minutes. This article covers Build a CI CD Pipeline with in detail.

Building and pushing Docker images with a github actions cicd docker workflow

Prerequisites – Build a CI CD Pipeline with

Before you start, make sure you have:

  • A GitHub account and a repository containing your application code.
  • A Docker Hub account (free at hub.docker.com) to host your images.
  • A Linux server with Docker installed and SSH access, for the deploy target. If you need one, see our guide on how to install Docker on Ubuntu 22.04.
  • Basic familiarity with the terminal and YAML syntax.

Step 1: Prepare the Dockerized Application – Build a CI CD Pipeline with

Your pipeline needs something to build. Create a simple Node.js app. First, the package.json:

{
  "name": "cicd-demo-app",
  "version": "1.0.0",
  "scripts": {
    "start": "node server.js",
    "test": "node test.js"
  },
  "dependencies": {
    "express": "^4.19.2"
  }
}

Then a minimal server.js:

const express = require('express');
const app = express();
const PORT = 3000;

app.get('/', (req, res) => {
  res.send('Hello from the CI/CD pipeline! Version 1.0.0');
});

app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

And a trivial test.js so the pipeline has a test stage:

// Simple smoke test for the pipeline
if (typeof express === 'undefined') {
  const express = require('express');
}
console.log('Smoke test passed: all modules load correctly.');
process.exit(0);

Now write a lean, production-ready Dockerfile:

FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

Commit all files and push the repository to GitHub. The github actions cicd docker pipeline will read this Dockerfile on every run.

Step 2: Create a Docker Hub Access Token

Never put your Docker Hub password in a workflow. Use an access token instead:

  1. Log in to Docker Hub, open your account menu and go to Account Settings > Personal access tokens.
  2. Click Generate new token, give it a description like github-actions, and grant it Read, Write permissions.
  3. Copy the token — it is shown only once.

Step 3: Store Secrets in the GitHub Repository

Your workflow needs credentials, and they belong in GitHub Secrets, not in code. In your repository, go to Settings > Secrets and variables > Actions > New repository secret and add:

  • DOCKERHUB_USERNAME — your Docker Hub username.
  • DOCKERHUB_TOKEN — the access token from Step 2.
  • SSH_HOST — the IP address or hostname of your deployment server.
  • SSH_USER — the SSH username on that server (e.g. deploy).
  • SSH_PRIVATE_KEY — the private key matching the public key installed in the server’s authorized_keys. If you need a refresher on connecting, read our guide to SSH into a Linux server.

Step 4: Write the GitHub Actions Workflow

Create .github/workflows/docker-cicd.yml in your repository. This is the heart of the github actions cicd docker pipeline — two jobs: build-and-push (CI) and deploy (CD).

name: CI/CD Docker Pipeline

on:
  push:
    branches: [ main ]

jobs:
  build-and-push:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3

      - name: Extract metadata (tags, labels)
        id: meta
        uses: docker/metadata-action@v5
        with:
          images: ${{ secrets.DOCKERHUB_USERNAME }}/cicd-demo-app
          tags: |
            type=raw,value=latest
            type=sha

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

      - name: Run tests inside the container
        run: |
          docker build -t cicd-demo-app:test .
          docker run --rm cicd-demo-app:test npm test

      - name: Build and push image
        uses: docker/build-push-action@v6
        with:
          context: .
          push: true
          tags: ${{ steps.meta.outputs.tags }}
          labels: ${{ steps.meta.outputs.labels }}

  deploy:
    runs-on: ubuntu-latest
    needs: build-and-push
    steps:
      - name: Deploy to server via SSH
        uses: appleboy/ssh-action@v1
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USER }}
          key: ${{ secrets.SSH_PRIVATE_KEY }}
          script: |
            docker pull ${{ secrets.DOCKERHUB_USERNAME }}/cicd-demo-app:latest
            docker stop cicd-demo-app || true
            docker rm cicd-demo-app || true
            docker run -d --name cicd-demo-app -p 80:3000 --restart unless-stopped \
              ${{ secrets.DOCKERHUB_USERNAME }}/cicd-demo-app:latest

Key points: the test step runs before the push, so a broken build never ships. The deploy job only runs when build-and-push succeeds (needs:), and it pulls the fresh image and restarts the container with zero downtime logic via --restart unless-stopped.

Step 5: Trigger the Pipeline and Watch It Run

Commit and push the workflow file:

git add .github/workflows/docker-cicd.yml
git commit -m "Add CI/CD pipeline"
git push origin main

Open your repository on GitHub, click the Actions tab, and watch the run. A green checkmark means your image was built, tested, pushed, and deployed. Verify on the server:

docker ps --filter name=cicd-demo-app
curl -s http://localhost/ | head -c 200

Step 6: Confirm the Deployment End-to-End

Make a small change — for example, edit the version string in server.js — commit, and push. Within a few minutes the new text should appear on your server without you touching it. That is continuous deployment working: code push becomes live service automatically. For more advanced topics, the official GitHub Actions documentation covers environments, approvals, and deployment protection rules.

Troubleshooting

1. “denied: requested access to the resource is denied” during push

This means the Docker Hub login failed or the repository does not exist. Double-check DOCKERHUB_USERNAME and DOCKERHUB_TOKEN in repository secrets. Docker Hub usernames are lowercase — an uppercase character will silently fail. Also create the cicd-demo-app repository on Docker Hub first (or make the token permissions Read/Write so the action can create it).

2. Workflow fails with “buildx failed” or out-of-memory errors

GitHub-hosted runners have limited resources. If your build is heavy (e.g. compiling native dependencies), split npm ci layers as shown in the Dockerfile above so layers are cached between runs, and add cache-from: type=gha / cache-to: type=gha,mode=max to the build-push-action step to reuse the GitHub Actions cache.

3. Deploy job fails with SSH authentication errors

appleboy/ssh-action needs the private key, including the -----BEGIN OPENSSH PRIVATE KEY----- header and footer, pasted as one secret. Also verify the server’s firewall allows inbound SSH from GitHub’s runner IPs, and that the public key is present in ~/.ssh/authorized_keys of SSH_USER.

4. New image deployed but the app still shows the old version

You pulled the new image but the browser shows the old page — this is usually browser caching, not a deployment problem. Hard-refresh (Ctrl+Shift+R). If the container itself is stale, confirm the running image digest: docker inspect cicd-demo-app --format='{{.Image}}' should match the digest shown in the Actions log after the push step.

Conclusion

You now have a working github actions cicd docker pipeline: push code, and GitHub Actions builds, tests, publishes, and deploys your Docker app automatically. From here you can grow it in several directions — add an environment with required reviewers before production deploys, extend the workflow to multiple architectures with docker/setup-qemu-action, or swap Docker Hub for Amazon ECR or GitHub Container Registry. The pattern stays the same: build once, test automatically, deploy on green.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.