Express.js CI CD GitHub Actions
CI stands for Continuous Integration. CD stands for Continuous Delivery or Deployment. Together they form an automatic pipeline that tests every code change and ships good changes to a server. GitHub Actions runs this pipeline inside a GitHub repository. A developer pushes code, and GitHub runs the checks without any manual steps.
The Factory Line Analogy
A factory line inspects each product at several stations. Defective items stop at the first failing station. Only products that pass every station reach the shipping dock. A CI/CD pipeline inspects code the same way. A failing test stops the line, and only healthy code reaches the server.
Pipeline Diagram
Developer pushes code
|
v
+--------------------+
| Install packages |
+--------------------+
|
v
+--------------------+
| Run lint and tests |---- fail ----> Stop and report
+--------------------+
| pass
v
+--------------------+
| Build Docker image |
+--------------------+
|
v
+--------------------+
| Deploy to server |
+--------------------+
Key Terms
| Term | Meaning |
|---|---|
| Workflow | A YAML file that describes the pipeline |
| Event | A trigger such as a push or a pull request |
| Job | A group of steps that run on one machine |
| Step | One command or one reusable action |
| Runner | The machine that executes the job |
| Secret | A protected value such as a deploy key |
Prepare the Project
The workflow runs the commands in package.json. Add a test script so the pipeline has something to run.
{
"scripts": {
"start": "node src/server.js",
"test": "jest --runInBand",
"lint": "eslint ."
}
}Create the CI Workflow
Save the file at .github/workflows/ci.yml. GitHub detects every YAML file inside that folder.
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20, 22]
steps:
- uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: npm
- run: npm ci
- run: npm run lint
- run: npm testHow the Workflow Reads
on
This block names the events that start the pipeline. The workflow runs on every push to main and on every pull request aimed at main.
matrix
The matrix runs the same job on several Node.js versions. A bug that appears in only one version shows up before release.
checkout and setup-node
The checkout action downloads the repository code. The setup-node action installs the chosen Node.js version and caches npm packages for faster runs.
npm ci
This command installs exact versions from the lock file. It gives repeatable results on every run.
Use Secrets for Sensitive Values
Never write passwords or keys in the YAML file. Store them in the repository settings under Secrets and variables. The workflow reads them with the secrets keyword. GitHub hides secret values in the log output.
Add a Deployment Job
The deploy job below runs only after the test job passes and only on the main branch. It connects to a server over SSH and restarts the app.
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- name: Deploy over SSH
uses: appleboy/ssh-action@v1.0.3
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SERVER_SSH_KEY }}
script: |
cd /var/www/my-app
git pull origin main
npm ci --omit=dev
pm2 restart my-appThe needs keyword creates the order. A failed test job cancels the deploy job.
Read the Results
Open the Actions tab in the repository to see each run. A green check means every step passed. A red cross means a step failed. Click the failed step to read its log and find the cause. Pull requests display the same status, so reviewers see test results before they approve a merge.
Best Practices
- Keep the pipeline fast. Cache packages and run tests in parallel when possible.
- Protect the main branch and require passing checks before a merge.
- Run security audits with npm audit in a separate step.
- Deploy to a staging server first and to production after approval.
- Rotate deploy keys on a regular schedule.
