- Write a simple CI/CD pipeline configuration (e.g., in GitLab CI/CD or GitHub Actions) that:
- Builds a basic Node.js application.
- Runs tests using npm test.
- Deploys the application to a staging server.
- Explain the YAML file structure and its components.
-
name:
Defines the name of the workflow. -
on:
Specifies the events that trigger the workflow:- A push to the
main
branch.
note: I configured a trigger to automate the build, test, and deployment processes on specific events.
- A push to the
-
jobs:
Defines the tasks to execute. Each job runs independently.
runs-on:
Specifies the environment to run the job (ubuntu-latest
).steps:
Contains a sequence of actions:- Checkout code: Use the GitHub-provided
checkout
action to retrieve the code; - Set up Node.js: Install the required Node.js version using the
setup-node
action; - Install dependencies: Use
npm install
to fetch dependencies; - Run tests: Execute tests using
npm test
; - Build application: Build the app with
npm run build
.
- Checkout code: Use the GitHub-provided
needs:
Specifies a dependency on thebuild
job. This ensures that deployment only happens if the build succeeds.env:
Defines environment variables like staging server details. These are stored securely in GitHub Secrets.run:
Executes deployment commands:- Uploads the
build
directory to the staging server usingscp
; - Restarts the application service on the server using
ssh
.
- Uploads the