I'm currently focusing on the core building blocks needed for this CI/CD pipeline. I've begun visualizing the workflow: code commits trigger automated testing, Docker image creation, and deployment. The goal is to keep things simple and efficient, leveraging the power of GitHub Actions. Next I need to decide between different testing strategies and deployment targets.
Deconstructing the Requirements
I'm now carefully breaking down the provided requirements, starting with the goal of a CI/CD pipeline for the Angular application. I've pinpointed key components like GitHub for source control and GitHub Actions for automation. My focus now is on defining each stage of the pipeline: the CI phase, including code checkout, Node.js setup, dependency installation, linting, testing, and production builds, followed by the CD phase.
Evaluating Design Tradeoffs
I'm now contemplating design choices. I'm torn between approaches for handling artifacts. Is it better to build in the Node.js job and copy artifacts, or let Docker handle it? Each method carries its own complexity, and I'm weighing the pros and cons. I'm leaning toward a standard approach for simplicity, while also keeping in mind Docker contexts.
Finalizing Pipeline Configuration
I'm now fully immersed in the details. I've chosen the approach that leverages Docker's multi-stage builds. I'm focusing on defining the precise steps within the GitHub Actions workflow file. I'm also ensuring the Dockerfile contains the appropriate nginx.conf and handling secrets. I've finished the Dockerfile, and am finalizing the GitHub Actions workflow, taking care to check the Angular application's dist/ directory path.
Optimizing Test Environment
I'm now focused on the testing configuration within the CI pipeline. I realize I need to ensure the Angular tests run smoothly in a headless environment. I'm checking the headless Chrome setup in the npm test script. I'm also preparing for the potential issues with browser compatibility that can arise with different CI runners.
Evaluating Docker Image Versioning
I'm now determining how to effectively tag and version the Docker images. I'll utilize the latest tag alongside the commit SHA for each build. This approach guarantees easy access to both the latest version and a specific, immutable version of the deployed image. This will enable both rapid deployment and the ability to roll back.
This guide will set up a complete pipeline that:
CI (Continuous Integration): Lints and tests your Angular code on every push.
CD (Continuous Delivery): Builds a multi-stage Docker image and pushes it to Docker Hub (only on pushes to the main branch).
Secrets: Go to your GitHub Repo -> Settings -> Secrets and variables -> Actions and add:
DOCKER_USERNAME: Your Docker Hub username.
DOCKER_PASSWORD: Your Docker Hub access token (or password).
Step 1: Create Nginx Configuration
Angular is a Single Page Application (SPA). To serve it correctly specifically to handle routing (so refreshing a page doesn't give a 404), we need a custom Nginx config.
Create a file named nginx.conf in the root of your project:
Create a Dockerfile in the root of your project. We use a multi-stage build to keep the final image small (containing only Nginx and the compiled HTML/JS, not Node.js).
dockerfile
# STAGE 1: Build
FROM node:20-alpine AS build
WORKDIR /app
# Copy package files and install dependencies
COPY package*.json ./
RUN npm ci
# Copy the rest of the application code
COPY . .
# Build the application (check your package.json for the build script)
RUN npm run build -- --configuration=production
# STAGE 2: Run
FROM nginx:alpine
# Copy the build output from Stage 1
# NOTE: Replace 'your-app-name' with the name found in angular.json output path
COPY --from=build /app/dist/your-app-name/browser /usr/share/nginx/html
# Copy our custom nginx config
COPY nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
Note: Check your angular.json to confirm the outputPath. In Angular 17+, it is often dist/project-name/browser.
Step 3: Configure Angular for CI Testing
To run tests in GitHub Actions (a headless environment), you need to configure Karma to use Headless Chrome.
Open karma.conf.js and modify the browsers and customLaunchers sections: