A CI/CD pipeline helps automate the process of testing and delivering code changes quickly and safely.
CI/CD pipeline basics in Node.js
pipeline {
agent any
stages {
stage('Build') {
steps {
// commands to build your app
}
}
stage('Test') {
steps {
// commands to test your app
}
}
stage('Deploy') {
steps {
// commands to deploy your app
}
}
}
}This example shows a simple pipeline structure with three stages: Build, Test, and Deploy.
Each stage contains steps that run commands or scripts.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building the app...'
}
}
}
}pipeline {
agent any
stages {
stage('Test') {
steps {
sh 'npm test'
}
}
}
}pipeline {
agent any
stages {
stage('Deploy') {
steps {
sh 'scp -r ./dist user@server:/var/www/app'
}
}
}
}This pipeline installs dependencies, runs tests, and deploys the app to a server automatically.
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Installing dependencies...'
sh 'npm install'
}
}
stage('Test') {
steps {
echo 'Running tests...'
sh 'npm test'
}
}
stage('Deploy') {
steps {
echo 'Deploying app...'
sh 'scp -r ./dist user@server:/var/www/app'
}
}
}
}Each stage runs only if the previous stage succeeded, helping catch errors early.
You can customize stages and steps to fit your project needs.
Using a CI/CD tool like Jenkins, GitHub Actions, or GitLab CI makes running pipelines easy.
A CI/CD pipeline automates building, testing, and deploying code.
It helps deliver software faster and with fewer errors.
Simple pipelines have stages like Build, Test, and Deploy.
Practice
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 AQuick Check:
CI/CD pipeline purpose = automation [OK]
- Thinking CI/CD is only for code backup
- Believing CI/CD replaces manual code review
- Assuming CI/CD skips testing
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 DQuick 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]
- Missing 'uses:' keyword before actions
- Incorrect indentation breaking YAML
- Not specifying node version properly
- run: npm test
env:
CI: true
What is the expected effect of setting CI=true in this context?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 BQuick Check:
CI=true means strict test mode [OK]
- Thinking CI=true skips tests
- Assuming CI=true installs dependencies
- Believing CI=true triggers deployment
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup Node
uses: actions/setup-node@v3
with:
node-version: '16'
- run: npm install
- run: npm test
- run: npm run deploy
The pipeline fails at the deploy step with "command not found" error. What is the likely cause?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 AQuick Check:
Missing npm script causes command not found [OK]
- Blaming Node.js version for script errors
- Ignoring missing npm scripts
- Assuming checkout or install steps are missing
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 CQuick Check:
Push to main branch = on.push.branches [OK]
- Confusing tags with branches
- Using pull_request instead of push
- Ignoring branches filter
Ready to prove it?
Pass the test and this pattern moves unknown
