📖 Scenario: You are working on a simple Node.js project. You want to automate testing and deployment using a CI/CD pipeline. This will help your team deliver updates faster and with fewer errors.
🎯 Goal: Build a basic CI/CD pipeline configuration file that runs tests and deploys the app automatically when code is pushed.
📋 What You'll Learn
Create a Node.js project with a test script
Add a configuration variable for the Node.js version
Write a CI/CD pipeline step to run tests
Write a CI/CD pipeline step to deploy the app
Print the pipeline steps to verify the setup
💡 Why This Matters
🌍 Real World
CI/CD pipelines automate testing and deployment so developers can deliver software updates quickly and reliably.
💼 Career
Understanding CI/CD basics is essential for DevOps roles and software development teams to improve workflow efficiency.
Progress0 / 4 steps
1
Create a Node.js project with a test script
Create a package.json file with a scripts section that includes a test script set to echo \"Running tests...\".
Node.js
Hint
The scripts section is a dictionary. Add a key test with the value echo \"Running tests...\".
2
Add a configuration variable for the Node.js version
Create a variable called nodeVersion and set it to the string "16.x".
Node.js
Hint
Use const nodeVersion = "16.x" to set the Node.js version.
3
Write a CI/CD pipeline step to run tests
Create an array called pipelineSteps and add an object with name set to "Run Tests" and command set to "npm test".
Node.js
Hint
Define pipelineSteps as an array with one object containing name and command keys.
4
Write a CI/CD pipeline step to deploy the app and print the pipeline
Add another object to pipelineSteps with name set to "Deploy" and command set to "echo Deploying app...". Then write console.log(pipelineSteps) to display the pipeline steps.
Node.js
Hint
Remember to add the deploy step as a second object in the pipelineSteps array. Use console.log(pipelineSteps) to print the array.
Practice
(1/5)
1. What is the main purpose of a CI/CD pipeline in Node.js projects?
easy
A. To automate building, testing, and deploying code
B. To manually review code changes before deployment
C. To write code faster without testing
D. To store code backups only
Solution
Step 1: Understand CI/CD pipeline purpose
A CI/CD pipeline automates repetitive tasks like building, testing, and deploying code to save time and reduce errors.
Step 2: Compare options against CI/CD definition
To automate building, testing, and deploying code correctly states this automation purpose, while others describe manual or unrelated tasks.
Final Answer:
To automate building, testing, and deploying code -> Option A
Quick Check:
CI/CD pipeline purpose = automation [OK]
Hint: CI/CD means automate build, test, deploy [OK]
Common Mistakes:
Thinking CI/CD is only for code backup
Believing CI/CD replaces manual code review
Assuming CI/CD skips testing
2. Which of the following is the correct syntax to define a simple GitHub Actions workflow file for Node.js that runs tests on push?
easy
A. name: Node.js CI
on: push
jobs:
build:
run-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install
- run: npm test
B. workflow: Node.js CI
trigger: push
steps:
- checkout
- setup-node 18
- run npm install
- run npm test
C. name: Node.js CI
on: push
jobs:
build:
runs-on: ubuntu-latest
steps:
- checkout
- setup-node
- run: npm install
- run: npm test
D. name: Node.js CI
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
Solution
Step 1: Check GitHub Actions syntax
The correct syntax uses 'name', 'on', 'jobs', 'runs-on', and 'steps' with proper indentation and action versions.
Step 2: Validate Node.js setup and OS
name: Node.js CI
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 uses 'ubuntu-latest', correct action versions, and specifies node-version properly. name: Node.js CI
on: push
jobs:
build:
run-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: '18'
- run: npm install
- run: npm test uses 'run-on: windows-latest' which is invalid key (should be 'runs-on'). Options B and C have syntax errors or missing versions.
Final Answer:
Correct syntax and setup for Node.js CI workflow -> Option D
Quick Check:
Correct GitHub Actions syntax = name: Node.js CI
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 [OK]
Hint: Look for correct YAML keys and action versions [OK]
Common Mistakes:
Missing 'uses:' keyword before actions
Incorrect indentation breaking YAML
Not specifying node version properly
3. Given this GitHub Actions step in a Node.js CI pipeline:
- run: npm test
env:
CI: true
What is the expected effect of setting CI=true in this context?
medium
A. It disables all tests to speed up the pipeline
B. It runs tests in continuous integration mode, causing failures on warnings
C. It installs dependencies before running tests
D. It deploys the app automatically after tests
Solution
Step 1: Understand CI environment variable
Setting CI=true tells test runners to run in continuous integration mode, often making warnings cause test failures.
Step 2: Match effect to options
It runs tests in continuous integration mode, causing failures on warnings correctly describes this behavior. Options A, C, and D describe unrelated or incorrect effects.
Final Answer:
Runs tests in CI mode, causing failures on warnings -> Option B
Quick Check:
CI=true means strict test mode [OK]
Hint: CI=true makes tests fail on warnings [OK]
Common Mistakes:
Thinking CI=true skips tests
Assuming CI=true installs dependencies
Believing CI=true triggers deployment
4. You have this GitHub Actions workflow snippet for Node.js:
The pipeline fails at the deploy step with "command not found" error. What is the likely cause?
medium
A. The deploy script is missing in package.json
B. Node.js version 16 does not support deploy commands
C. The checkout action is missing
D. npm install was not run before deploy
Solution
Step 1: Analyze error cause
"command not found" during 'npm run deploy' usually means the deploy script is not defined in package.json.
Step 2: Check other steps
Checkout and npm install steps exist, so code and dependencies are present. Node.js version 16 supports scripts. So missing deploy script is the cause.
Final Answer:
The deploy script is missing in package.json -> Option A
Quick Check:
Missing npm script causes command not found [OK]
Hint: Check package.json scripts for deploy command [OK]
Common Mistakes:
Blaming Node.js version for script errors
Ignoring missing npm scripts
Assuming checkout or install steps are missing
5. You want to create a CI/CD pipeline for a Node.js app that builds, tests, and deploys only when code is pushed to the main branch. Which YAML snippet correctly restricts the pipeline to run only on pushes to main?
hard
A. on:
pull_request:
branches:
- main
B. on:
push:
tags:
- main
C. on:
push:
branches:
- main
D. on:
push:
branches-ignore:
- main
Solution
Step 1: Understand GitHub Actions triggers
To run on pushes to main branch only, use 'on: push' with 'branches: [main]'.
Step 2: Analyze each trigger configuration
on:
push:
branches:
- main correctly uses branches filter. on:
push:
tags:
- main filters tags, not branches. on:
pull_request:
branches:
- main triggers on pull requests, not pushes. on:
push:
branches-ignore:
- main ignores main branch pushes.
Final Answer:
on push to main branch only -> Option C
Quick Check:
Push to main branch = on.push.branches [OK]
Hint: Use 'on: push' with 'branches: main' to restrict runs [OK]
Common Mistakes:
Confusing tags with branches
Using pull_request instead of push
Ignoring branches filter
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.