DEV Community

Pranav Bakare
Pranav Bakare

Posted on

CI/CD pipeline using Gitlab CI

Building your first CI/CD pipeline for a Node.js project on Windows involves several steps, utilizing Git, GitLab CI (or GitHub Actions), and tools like Jenkins. Here’s a simple guide using GitLab CI for automating testing and deployment of a Node.js app.

Prerequisites

  1. GitLab account (or GitHub for GitHub Actions)
  2. Node.js and npm installed on your system.
  3. Git installed and initialized in your project.
  4. Basic knowledge of Node.js and Git.

Step 1: Set Up a Node.js Project

  1. Create a folder for your project and initialize a Node.js app:
   mkdir my-node-app
   cd my-node-app
   npm init -y
Enter fullscreen mode Exit fullscreen mode
  1. Install any necessary dependencies. For example, if you want to use Express:
   npm install express --save
Enter fullscreen mode Exit fullscreen mode
  1. Create a basic server.js:
   const express = require('express');
   const app = express();

   app.get('/', (req, res) => {
     res.send('Hello, CI/CD Pipeline!');
   });

   app.listen(3000, () => {
     console.log('Server is running on port 3000');
   });
Enter fullscreen mode Exit fullscreen mode
  1. Create a test script in your package.json:
   "scripts": {
     "start": "node server.js",
     "test": "echo \"Error: no test specified\" && exit 1"
   }
Enter fullscreen mode Exit fullscreen mode

Step 2: Set Up GitLab Repository

  1. Push your Node.js project to GitLab:
   git init
   git remote add origin https://gitlab.com/yourusername/your-node-app.git
   git add .
   git commit -m "Initial commit"
   git push -u origin master
Enter fullscreen mode Exit fullscreen mode

Step 3: Configure GitLab CI/CD Pipeline

  1. In your project root, create a .gitlab-ci.yml file. This file defines the stages of your pipeline:
   stages:
     - test
     - deploy

   test:
     stage: test
     image: node:14
     script:
       - npm install
       - npm test

   deploy:
     stage: deploy
     script:
       - echo "Deploying the app!"
Enter fullscreen mode Exit fullscreen mode
  • test stage: Runs npm install and npm test to ensure the app and tests are working.
  • deploy stage: For demonstration, this will just echo a message, but in a real scenario, you'd deploy to your server or cloud service.

Step 4: Commit and Push Changes

After creating the .gitlab-ci.yml file, commit and push your changes:

git add .gitlab-ci.yml
git commit -m "Add CI/CD pipeline"
git push origin master
Enter fullscreen mode Exit fullscreen mode

Step 5: View the Pipeline on GitLab

  1. Go to your project on GitLab and navigate to CI/CD > Pipelines.
  2. You should see the pipeline running through the test and deploy stages. If everything is set up correctly, the pipeline will pass.

Step 6: (Optional) Add Tests

To enhance the pipeline, you can write actual tests using Mocha or Jest. Install a testing library, write tests, and modify the pipeline to run them.

Example with Mocha:

  1. Install Mocha and add a test script:
   npm install mocha chai --save-dev
Enter fullscreen mode Exit fullscreen mode
  1. Add a test.js in the test/ directory:
   const chai = require('chai');
   const expect = chai.expect;

   describe('Sample Test', () => {
     it('should return true', () => {
       expect(true).to.be.true;
     });
   });
Enter fullscreen mode Exit fullscreen mode
  1. Update the test script in package.json:
   "test": "mocha"
Enter fullscreen mode Exit fullscreen mode

Now the test stage in the pipeline will run the Mocha tests.

Step 7: Automate Deployment (Advanced)

Once tests pass, you can automate deployment to services like Heroku, AWS, or your own server. Modify the deploy stage to include deployment commands.
.
.
.

Conclusion

This simple CI/CD pipeline automatically installs dependencies, runs tests, and can deploy the application when code is pushed. For a more advanced pipeline, you can integrate linting, security checks, or multi-environment deployments.

Top comments (0)