The user wants a CI/CD pipeline for an Angular application using GitHub Actions and Docker.
Key components needed:
Angular Application: I should assume a standard Angular project structure (or provide a brief skeleton/description of what's expected).
Docker: I need a Dockerfile to containerize the Angular app (multi-stage build is best practice for size).
GitHub Actions (CI/CD): I need a workflow YAML file (.github/workflows/main.yml or similar).
CI (Continuous Integration): Install dependencies, lint, test, build.
CD (Continuous Deployment): Build the Docker image, push it to a registry (Docker Hub is the standard default example), and potentially deploy it (though deployment targets vary wildly, I'll stick to pushing the image as the "delivery" part or maybe provide a placeholder for deployment).
Stage 1 (Build): Use a Node image. Copy package.json, install npm packages, copy source, run ng build.
Stage 2 (Serve): Use an Nginx image (Alpine version is good). Copy the build artifacts from Stage 1 to the Nginx html folder. Copy a custom Nginx config (crucial for Angular routing to handle 404s by falling back to index.html).
Nginx Configuration: Create a simple nginx.conf file.
GitHub Actions Workflow:
Triggers: On push to main / master.
Jobs:
Job 1: Build and Test (CI): Checkout code -> Setup Node -> npm ci -> npm run lint/test/build.
Job 2: Docker Build and Push (CD): Needs Job 1. Login to Docker Hub (using secrets) -> Setup Docker Buildx -> Build and push image.
Secrets: Explain how to set up DOCKER_USERNAME and DOCKER_PASSWORD in GitHub repository secrets.
Drafting the Content:
Dockerfile:
Base: node:18-alpine (or latest LTS).
Workdir: /app.
Commands: npm ci, npm run build -- --configuration production.
Final stage: nginx:alpine.
Copy: dist/project-name/browser (Angular 17+ uses this structure) -> /usr/share/nginx/html.
Config: nginx.conf.
Nginx config:
Standard server block.
try_files $uri $uri/ /index.html; (This is vital for SPA).
Steps: Checkout, Login to Docker Hub (docker/login-action), Build and push (docker/build-push-action).
Refining the details:
Angular versions change directory structures. Angular 17+ puts output in dist/app-name/browser. Older versions just dist/app-name. I should write the Dockerfile to handle the standard outputPath which is usually dist/app-name. Self-correction: If using the default ng new, the output is dist/project-name. The new builder might use dist/project-name/browser. I will stick to the default behavior but add a note about the output path.
Testing in Angular requires Chrome. Running Chrome in a CI environment (headless) requires specific flags (--no-sandbox, --headless). I'll provide the command for that.
Structure of the Answer:
Project Structure Overview (Brief).
Step 1: Nginx Configuration (needed for routing).
Step 2: Dockerfile (Multi-stage build).
Step 3: GitHub Actions Workflow (The main logic).
Step 4: Configuration & Secrets (How to make it work).
Let's write the code blocks.
Nginx: Simple.
Dockerfile:
dockerfile
FROM node:20-alpine AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build -- --configuration production
FROM nginx:alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY --from=build /app/dist/your-app-name /usr/share/nginx/html
EXPOSE 80
Workflow:
Use actions/checkout@v4.
Use actions/setup-node@v4.
Use docker/setup-buildx-action.
Use docker/login-action.
Use docker/build-push-action.
This looks solid. I will generate the response based on this plan.
Here is a complete guide to setting up a CI/CD pipeline for an Angular application using GitHub Actions and Docker.
This approach uses a Multi-stage Docker build. This is best practice because it uses a heavy Node.js image to compile the Angular app, and then copies only the lightweight static files (HTML/CSS/JS) into a lightweight Nginx image for production.
Prerequisites
An Angular project pushed to a GitHub repository.
A Docker Hub account (or GitHub Container Registry).
Step 1: Configure Nginx
Dockerized Angular apps usually use Nginx as the web server. You need a configuration file to handle Angular routing (so refreshing a page like /dashboard doesn't give a 404 error).
Create a file named nginx.conf in the root of your project:
Create a file named Dockerfile (no extension) in the root of your project. This defines the build and deployment environment.
dockerfile
# Stage 1: Build the Angular application
FROM node:20-alpine AS build
WORKDIR /app
# Copy package files first to leverage Docker cache
COPY package*.json ./
# Install dependencies
RUN npm ci
# Copy the rest of the application code
COPY . .
# Build the application for production
# Replace 'your-app-name' with your actual project name if different from default
RUN npm run build -- --configuration production
# Stage 2: Serve the application with Nginx
FROM nginx:alpine
# Copy the custom Nginx config
COPY nginx.conf /etc/nginx/conf.d/default.conf
# Copy the build output from the 'build' stage to Nginx html folder
# Note: Angular outputs to dist/<project_name>/browser in newer versions
# Check your dist folder structure after building locally to confirm path
COPY --from=build /app/dist/my-angular-app/browser /usr/share/nginx/html
# Expose port 80
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Important: Check your angular.json file or run ng build locally to see exactly where your output files are generated. It is usually dist/your-project-name or dist/your-project-name/browser.
Step 3: Set up GitHub Secrets
To push the image to Docker Hub securely, you need to store your credentials in GitHub.
Go to your GitHub Repository.
Click Settings > Secrets and variables > Actions.
Click New repository secret.
Add the following two secrets:
Name:DOCKER_USERNAME → Value: Your Docker Hub username.
Name:DOCKER_PASSWORD → Value: Your Docker Hub Access Token (Do not use your password; generate a token in Docker Hub Security settings).
Step 4: Create the GitHub Actions Workflow
Create a directory .github/workflows in your root folder and add a file named ci-cd.yml.
This pipeline triggers on every push to the main branch. It runs tests, builds the Docker image, pushes it to Docker Hub, and (optionally) deploys it.
yaml
name: Angular CI/CD Pipeline
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build-and-push:
runs-on: ubuntu-latest
steps:
# 1. Checkout the code
- name: Checkout Repository
uses: actions/checkout@v4
# 2. Set up Node.js (Optional: useful if you want to run tests/lint before Docker build)
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
# 3. Install dependencies and Run Tests
- name: Install Dependencies
run: npm ci
- name: Run Tests
run: npm test -- --no-watch --browsers=ChromeHeadless
# 4. Log in to Docker Hub
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
# 5. Set up Docker Buildx (for optimized builds)
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
# 6. Build and Push Docker Image
# We use github.sha to tag the image with a unique ID based on the git commit
- name: Build and Push Docker Image
uses: docker/build-push-action@v5
with:
context: .
file: ./Dockerfile
push: true
tags: |
${{ secrets.DOCKER_USERNAME }}/my-angular-app:latest
${{ secrets.DOCKER_USERNAME }}/my-angular-app:${{ github.sha }}
# Optional: Deployment Job
# This requires a self-hosted runner or specific cloud actions (AWS, Azure, etc.)
# deploy:
# needs: build-and-push
# runs-on: ubuntu-latest
# steps:
# - name: Deploy to Server
# run: echo "Add your deployment script here (e.g., SSH into server and pull new image)"
Summary of What Happens
Code Push: You push code to GitHub.
CI Phase: GitHub Actions spins up a VM, installs Node, installs npm packages, and runs ng test to ensure unit tests pass.
Build Phase: Docker builds the image. It uses the Node stage to compile your Angular app into static files, then wraps them in an Nginx image.
CD Phase: The finished Docker image is pushed to your Docker Hub repository with two tags: latest and the specific Git commit hash.
How to Run This Locally
Before pushing, ensure your Docker setup works locally:
bash
# Build the image
docker build -t my-angular-test .
# Run the container
docker run -p 8080:80 my-angular-test
Open http://localhost:8080 in your browser to verify the app loads.