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
- GitLab account (or GitHub for GitHub Actions)
- Node.js and npm installed on your system.
- Git installed and initialized in your project.
- Basic knowledge of Node.js and Git.
Step 1: Set Up a Node.js Project
- Create a folder for your project and initialize a Node.js app:
mkdir my-node-app
cd my-node-app
npm init -y
- Install any necessary dependencies. For example, if you want to use Express:
npm install express --save
- 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');
});
- Create a
test
script in yourpackage.json
:
"scripts": {
"start": "node server.js",
"test": "echo \"Error: no test specified\" && exit 1"
}
Step 2: Set Up GitLab Repository
- 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
Step 3: Configure GitLab CI/CD Pipeline
- 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!"
-
test
stage: Runsnpm install
andnpm 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
Step 5: View the Pipeline on GitLab
- Go to your project on GitLab and navigate to CI/CD > Pipelines.
- You should see the pipeline running through the
test
anddeploy
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:
- Install Mocha and add a test script:
npm install mocha chai --save-dev
- Add a
test.js
in thetest/
directory:
const chai = require('chai');
const expect = chai.expect;
describe('Sample Test', () => {
it('should return true', () => {
expect(true).to.be.true;
});
});
- Update the
test
script inpackage.json
:
"test": "mocha"
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)