π οΈ Step 1: Fork a Node.js Repository
Choose a Node.js repository β Select a sample Node.js project. Look for a basic app or a CRUD application that is suitable for CI/CD.
Fork the Repository β Create a fork of the selected repository. This forked repository will hold your code for the CI/CD setup.
π Step 2: Set Up GitHub Integration with Jenkins
Create a Jenkins Job β In Jenkins, create a new pipeline or freestyle project job for your CI/CD pipeline.
Connect GitHub to Jenkins:
Go to the Jenkins job settings and select the "Source Code Management" section.
Add your forked GitHub repository URL.
Enable GitHub WebHooks β In GitHub, go to the repository settings and set up WebHooks to connect it to your Jenkins server.
- This allows Jenkins to automatically detect changes in your repository and trigger builds.
π Step 3: Configure Your Jenkins Pipeline
Pipeline Script or Freestyle Job β If using a freestyle job, add build steps under the βBuildβ section. For a pipeline job, use a
Jenkinsfile
.Shell Commands in Jenkins β In the "Execute Shell" section, use Docker Compose commands to build and run your application:
docker-compose up -d --build
- This command will start your app using Docker Compose, creating and running containers for each service defined in your
docker-compose.yml
.
- This command will start your app using Docker Compose, creating and running containers for each service defined in your
π³ Step 4: Create a Docker Compose File
Dockerfile β In the projectβs root, ensure a Dockerfile is present. It should install dependencies, copy the code, and expose necessary ports. Example
Dockerfile
:FROM node:14 WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["npm", "start"]
docker-compose.yml β Create a
docker-compose.yml
file that defines the services needed. Exampledocker-compose.yml
:version: '3' services: app: build: . ports: - "3000:3000" environment: NODE_ENV: production
- This file specifies the application service and any necessary configurations.
π Step 5: Run the Jenkins Pipeline
Trigger the Build β Push changes to the forked repository or trigger the build manually in Jenkins.
Watch Jenkins Logs β Check the Jenkins console output to view the build, test, and deployment logs.
Verify Deployment β Once Jenkins completes the build, your Node.js app should be running in a Docker container on the specified port.