Introduction: Why CI/CD is Essential for JavaScript Projects
Modern web development is unthinkable without automation. Every day, JavaScript (Node.js) developers face routine tasks: running tests, checking code with a linter, building and deploying the application. Performing these operations manually not only takes time but is also prone to errors. This is where CI/CD (Continuous Integration / Continuous Deployment) comes to the rescue — a methodology that allows you to automate all stages of the software lifecycle.
GitHub Actions is a built-in automation tool on the GitHub platform, ideal for setting up CI/CD pipelines. In this article, we will break down how to set up a full automation cycle for a JavaScript (Node.js) project from scratch: from running tests to deploying to a server. You will learn how to create a workflow, write configuration files, and avoid common mistakes.
GitHub Actions Basics: Structure and Components
Before writing code, let's understand the key concepts of GitHub Actions. The entire pipeline is described in YAML files stored in the .github/workflows directory of your repository.
Key Elements of a Workflow
- Workflow — an automated process consisting of one or more Jobs.
- Job — a set of Steps executed on a single Runner (virtual machine).
- Step — an individual command or Action.
- Event — a trigger that starts the workflow (e.g., push, pull request).
For JavaScript projects, a standard workflow looks like this:
name: CI/CD for Node.js
on: push: branches: [ main ] pull_request: branches: [ main ]
jobs: build-and-test: runs-on: ubuntu-latest strategy: matrix: node-version: [18.x, 20.x] steps: - uses: actions/checkout@v4 - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - name: Install dependencies run: npm ci - name: Linting run: npm run lint - name: Tests run: npm testNote the strategy.matrix — this allows testing the code on multiple Node.js versions simultaneously, which is critical for libraries and packages.
Step-by-Step CI/CD Setup for a Node.js Project
Let's look at a complete example of setting up a pipeline for a typical Express.js web application. Start with the base file .github/workflows/node-ci.yml.
Step 1: Basic Code Check
name: Node.js CI
on: push: branches: [ develop, main ] pull_request: branches: [ main ]
jobs: quality: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup Node.js uses: actions/setup-node@v4 with: node-version: '20' cache: 'npm' - name: Install dependencies run: npm ci - name: Lint code run: npm run lint - name: Run unit tests run: npm run test:unit - name: Build project run: npm run buildKey points:
cache: 'npm'— cachesnode_modules, speeding up subsequent runs.npm ci— clean install of dependencies, faster and more reliable thannpm install.- Splitting tests into unit and integration (optional) allows for faster feedback.
Step 2: Integration Tests with a Database
For testing with a real database, use GitHub Actions services:
jobs: integration-tests: runs-on: ubuntu-latest