Bird
Raised Fist0
Node.jsframework~10 mins

CI/CD pipeline basics in Node.js - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - CI/CD pipeline basics
Code Commit
↓
Build Stage
↓
Test Stage
↓
Deploy Stage
↓
Production Environment
The CI/CD pipeline starts with code commit, then builds the code, runs tests, and finally deploys to production.
Execution Sample
Node.js
git add .
git commit -m "Update"
npm run build
npm test
kubectl apply -f deployment.yaml
This sequence adds changes, commits code, builds the project, runs tests, and deploys to production.
Execution Table
StepActionCommandResultNext Step
1Code Commitgit add . && git commit -m "Update"Code saved in repositoryBuild Stage
2Buildnpm run buildProject compiled successfullyTest Stage
3Testnpm testAll tests passedDeploy Stage
4Deploykubectl apply -f deployment.yamlApplication deployed to productionPipeline Complete
5End-Pipeline finished successfully-
💡 Pipeline stops after successful deployment to production.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
Code StateLocal changesCommitted to repoBuilt artifacts readyTests passedDeployed to prodStable production
Key Moments - 3 Insights
Why do we run tests after building the code?
Tests run after build to check if the compiled code works correctly, as shown in step 3 of the execution table.
What happens if tests fail?
If tests fail, deployment does not happen. The pipeline stops after the test stage, preventing bad code from reaching production.
Why is deployment the last step?
Deployment is last to ensure only tested and built code reaches production, as seen in step 4 where deployment happens after successful tests.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the result after the build step?
AAll tests passed
BProject compiled successfully
CCode saved in repository
DApplication deployed to production
💡 Hint
Check the 'Result' column in row 2 of the execution table.
At which step does the pipeline deploy the application?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Action' column to find when deployment happens.
If tests fail, what happens to the deployment step?
ABuild runs again
BDeployment proceeds anyway
CDeployment is skipped
DCode is committed again
💡 Hint
Refer to the key moments section about test failures stopping the pipeline.
Concept Snapshot
CI/CD pipeline automates code delivery.
Steps: Commit code -> Build -> Test -> Deploy.
Build compiles code.
Tests verify correctness.
Deploy sends code to production only if tests pass.
Full Transcript
A CI/CD pipeline automates software delivery by running steps in order: code commit, build, test, and deploy. When a developer commits code, the pipeline builds the project to compile the code. Then it runs tests to check if the code works correctly. If tests pass, the pipeline deploys the application to production. If tests fail, deployment stops to avoid errors in production. This process helps deliver reliable software quickly and safely.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To automate building, testing, and deploying code -> Option A
  4. 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

  1. Step 1: Check GitHub Actions syntax

    The correct syntax uses 'name', 'on', 'jobs', 'runs-on', and 'steps' with proper indentation and action versions.
  2. 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.
  3. Final Answer:

    Correct syntax and setup for Node.js CI workflow -> Option D
  4. 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

  1. Step 1: Understand CI environment variable

    Setting CI=true tells test runners to run in continuous integration mode, often making warnings cause test failures.
  2. 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.
  3. Final Answer:

    Runs tests in CI mode, causing failures on warnings -> Option B
  4. 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:
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?
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

  1. Step 1: Analyze error cause

    "command not found" during 'npm run deploy' usually means the deploy script is not defined in package.json.
  2. 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.
  3. Final Answer:

    The deploy script is missing in package.json -> Option A
  4. 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

  1. Step 1: Understand GitHub Actions triggers

    To run on pushes to main branch only, use 'on: push' with 'branches: [main]'.
  2. 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.
  3. Final Answer:

    on push to main branch only -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard