Overview

The project does not currently have a CI/CD setup implemented. To establish Continuous Integration and Continuous Deployment (CI/CD) for the benhall/express-demo, the following steps can be taken to integrate automation scripts and environment configurations.

Next Steps to Implement CI/CD

  1. Select a CI/CD Tool: Choose a CI/CD platform such as GitHub Actions, Travis CI, CircleCI, or Jenkins. For multistage Docker builds, GitHub Actions or GitLab CI might be particularly user-friendly.

  2. Create Configuration Files: Depending on the chosen CI/CD tool, create appropriate configuration files:

    • For GitHub Actions, create a .github/workflows/ci.yml file.
    • For Travis CI, add .travis.yml.
  3. Write CI/CD Pipeline Scripts: Define build, test, and deploy steps within the configuration file. Below are examples outlined for GitHub Actions.

Example GitHub Actions Workflow Configuration

Creating Your Workflow File

Create a file at .github/workflows/ci.yml. Below is an example configuration:

name: CI/CD

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

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
    - name: Check out code
      uses: actions/checkout@v2
      
    - name: Set up Node.js
      uses: actions/setup-node@v2
      with:
        node-version: '20.11.0'

    - name: Install Dependencies
      run: npm ci
      
    - name: Build Application
      run: npm run build
      
    - name: Run Tests
      run: npm test
      
    - name: Build Docker Image
      run: docker build -t myapp:latest .

    - name: Push Docker Image
      run: |
        echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
        docker push myapp:latest

Explanation of the Workflow Steps

  • Check out code: Uses the GitHub Actions checkout to retrieve the code from the repository.

  • Set up Node.js: Utilizes the actions/setup-node action to specify the Node.js version.

  • Install Dependencies: Runs npm ci to install the dependencies listed in package-lock.json.

  • Build Application: Executes the build step previously defined in package.json.

  • Run Tests: Conducts tests as per the configurations in the package.json.

  • Build Docker Image: The command docker build -t myapp:latest . builds the Docker image based on the Dockerfile.

  • Push Docker Image: This includes logging into a Docker registry using secrets configured in the repository settings and pushing the built image.

Sample Dockerfile

The existing Dockerfile in the repository utilizes multistage builds to optimize the image size and enhance performance. Below is a relevant part of the Dockerfile showing the exposed port and general structure.

# Create app directory and define defaults
WORKDIR /usr/src/app
EXPOSE 3000
CMD [ "./bin/www" ]
ENV ADBLOCK=1

# Install app dependencies
COPY package*.json ./

RUN npm ci --production

# Copy application source
COPY --from=0 /usr/src/app/public public
COPY --from=0 /usr/src/app/webpack.version.json .
COPY . .

Key Elements of the Dockerfile

  • Multistage Builds: This design allows for a clean separation of build and runtime environments, which can minimize the final image size.

  • Expose Port: The EXPOSE 3000 directive informs the container that it listens on the specified network port during runtime.

Conclusion

By following the outlined steps and utilizing the workflow example, the benhall/express-demo can easily set up CI/CD automation. This will facilitate the automatic building, testing, and deployment of applications, streamlining the development process.

If there are any new developments in the CI/CD initiatives or additional scripts are added, this documentation should be updated accordingly to reflect those changes.