A CI/CD pipeline helps you automatically test and deliver your Express app updates quickly and safely.
CI/CD pipeline for Express apps
name: Express CI/CD Pipeline
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install
- run: npm test
- run: npm run build
- name: Deploy
run: |
echo "Deploy commands here"This example uses GitHub Actions syntax for a CI/CD pipeline.
Replace npm run build and deploy commands with your app's specific commands.
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm install
- run: npm teston:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm install
- run: npm run build
- run: scp -r ./dist user@server:/var/www/appThis pipeline runs on pushes to the main branch. It installs dependencies, runs tests, builds the app, then copies files to a server and restarts the app process.
name: Express CI/CD Pipeline
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: '18'
- name: Install dependencies
run: npm install
- name: Run tests
run: npm test
- name: Build app
run: npm run build
- name: Deploy to server
run: |
scp -r ./dist user@yourserver.com:/var/www/express-app
ssh user@yourserver.com 'pm2 restart express-app'
Make sure your server allows SSH access and you have keys set up for deployment.
Use environment variables to keep secrets like server user and IP safe.
Test your pipeline with a small change before relying on it fully.
A CI/CD pipeline automates testing and deployment for your Express app.
It saves time and reduces errors by running steps automatically on code changes.
Use tools like GitHub Actions to create simple pipelines with steps for install, test, build, and deploy.
Practice
Solution
Step 1: Understand CI/CD pipeline role
A CI/CD pipeline automates repetitive tasks like testing and deployment to save time and reduce errors.Step 2: Identify pipeline purpose for Express app
For an Express app, the pipeline runs tests and deploys the app automatically when code changes.Final Answer:
To automate testing and deployment when code changes -> Option AQuick Check:
CI/CD automates testing and deployment [OK]
- Thinking CI/CD writes code
- Confusing deployment with UI design
- Assuming CI/CD manages databases
Solution
Step 1: Review GitHub Actions syntax for commands
In GitHub Actions, commands run under a step use the key 'run:' followed by the command string.Step 2: Identify correct command format for tests
To run tests with npm, the correct syntax is 'run: npm test'. Other options miss the colon or use wrong keys.Final Answer:
run: npm test -> Option CQuick Check:
GitHub Actions uses 'run:' before commands [OK]
- Omitting colon after 'run'
- Using 'execute' or 'command' instead of 'run'
- Writing commands without quotes or colon
- name: Install dependencies
run: npm install
- name: Run tests
run: npm testWhat happens if the tests fail?
Solution
Step 1: Understand default GitHub Actions behavior on failure
If a step like 'npm test' fails (returns error), the pipeline stops by default and does not proceed to next steps.Step 2: Apply this to deployment step
Since deployment usually comes after tests, if tests fail, deployment will not run to avoid deploying broken code.Final Answer:
The pipeline stops and deployment does not happen -> Option AQuick Check:
Failed test stops pipeline by default [OK]
- Assuming pipeline continues after test failure
- Thinking tests auto-retry without setup
- Confusing install step with test step
jobs:
ci:
runs-on: ubuntu-latest
steps:
- name: Install dependencies
run: npm install
- name: Run tests
run npm test
- name: Deploy
run: npm run deployWhat is the error in this snippet?
Solution
Step 1: Check syntax of each step
In GitHub Actions, each step's command must use 'run:' with a colon. The 'Run tests' step misses the colon after 'run'.Step 2: Verify other steps
'npm run deploy' is a valid command if defined in package.json scripts. 'npm install' is acceptable.Final Answer:
Missing colon after 'run' in the Run tests step -> Option BQuick Check:
GitHub Actions commands need 'run:' with colon [OK]
- Forgetting colon after 'run'
- Assuming 'npm run deploy' is invalid
- Confusing job structure with step syntax
Solution
Step 1: Understand conditional deployment needs
Deployment should run only on main branch and only if tests succeed to avoid deploying broken code or on wrong branches.Step 2: Use GitHub Actions condition syntax
The 'if:' condition can check branch with 'github.ref == "refs/heads/main"' and check success with 'success()' function.Final Answer:
Use 'if: github.ref == "refs/heads/main" && success()' before the deploy step -> Option DQuick Check:
Conditional deploy uses 'if:' with branch and success check [OK]
- Deploying without branch or success check
- Deploying before tests run
- Using failure() to deploy on test failure
Ready to prove it?
Pass the test and this pattern moves unknown
