Here’s how you can set up a simple CI/CD pipeline for a Node.js project using GitHub Actions on Windows.
Prerequisites
- GitHub account.
- Node.js and npm installed.
- Git installed on your system.
- GitHub repository for your Node.js project.
Step 1: Create a Node.js Project
- Create a new folder for your project:
mkdir my-node-app
cd my-node-app
npm init -y
- Install any dependencies, such as Express (for a basic server):
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 with GitHub Actions!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
- Define a
test
script inpackage.json
(we'll add proper tests later):
"scripts": {
"start": "node server.js",
"test": "echo \"No test specified\" && exit 0"
}
Step 2: Set Up GitHub Repository
- Push your Node.js project to GitHub:
git init
git remote add origin https://github.com/yourusername/your-node-app.git
git add .
git commit -m "Initial commit"
git push -u origin master
Step 3: Set Up GitHub Actions
- Go to your GitHub repository and click on the Actions tab.
- Click Set up a workflow yourself or choose the Node.js workflow template.
- Create a new file named
.github/workflows/nodejs.yml
in your repository with the following content:
name: Node.js CI
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14, 16]
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
Explanation:
-
on
: Triggers the workflow when code is pushed or a pull request is made to themaster
branch. -
jobs
: Defines thebuild
job.-
runs-on
: Uses an Ubuntu machine for the job. -
strategy.matrix.node-version
: Runs the job for multiple versions of Node.js (14 and 16 in this case). -
steps
: Defines the steps for the pipeline: - Checkout the repository.
- Set up Node.js using the specified versions.
-
Install dependencies by running
npm install
. -
Run tests by executing
npm test
.
-
Step 4: Commit and Push Changes
Commit the new .github/workflows/nodejs.yml
file and push it to GitHub:
git add .github/workflows/nodejs.yml
git commit -m "Add GitHub Actions workflow"
git push origin master
Step 5: View the Pipeline on GitHub
- Go to your GitHub repository.
- Click on the Actions tab, and you’ll see the pipeline running.
- It will test the code and install dependencies on Node.js versions 14 and 16.
Step 6: (Optional) Add Tests
To make the pipeline more useful, you can add actual tests using Mocha or Jest.
Example with Mocha:
- Install Mocha and Chai for testing:
npm install mocha chai --save-dev
- Create a
test
folder with atest.js
file:
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
:
"scripts": {
"start": "node server.js",
"test": "mocha"
}
Now, when the pipeline runs, it will execute the test suite using Mocha.
Step 7: (Optional) Deployment to Heroku (Simple Example)
You can automate deployment to services like Heroku in the deploy
step after your tests pass.
To deploy to Heroku, follow these steps:
- Install the Heroku CLI and log in:
npm install -g heroku
heroku login
- In your workflow, add a deployment step after the tests:
- name: Deploy to Heroku
run: |
heroku git:remote -a your-heroku-app
git push heroku master
env:
HEROKU_API_KEY: ${{ secrets.HEROKU_API_KEY }}
You’ll need to add your Heroku API Key to your GitHub repository’s secrets. Go to Settings > Secrets > Actions and add the HEROKU_API_KEY
.
Conclusion
You’ve now set up a simple CI/CD pipeline using GitHub Actions for a Node.js app. This workflow installs dependencies, runs tests, and can be extended to deploy your app to platforms like Heroku, AWS, or Netlify.