CI/CD Overview
Understanding Continuous Integration and Continuous Deployment practices
CI/CD Overview
Continuous Integration (CI) and Continuous Deployment (CD) are practices in modern software development that automate the process of building, testing, and deploying applications. They aim to reduce manual effort, increase reliability, and ensure that new features or bug fixes reach production quickly.
Key Concepts
-
Continuous Integration (CI)
Developers frequently merge code changes into a shared repository. Automated builds and tests are triggered to catch issues early. -
Continuous Deployment (CD)
After passing CI checks, changes are automatically deployed to production or staging environments, reducing human error. -
Stages
CI/CD pipelines are divided into stages such asbuild,test, anddeploy. Each stage can contain multiple steps or jobs. -
Automation Tools
Common CI/CD tools include GitLab CI, GitHub Actions, Jenkins, and CircleCI.
Example: GitLab CI/CD Pipeline
Below is a simple GitLab CI/CD configuration that deploys a project called n-docs using Docker Compose.
stages:
- deploy
deploy:
stage: deploy
script:
- |
if [ ! -d "/home/gitlab-runner/n-docs/.git" ]; then
git clone https://gitlab.j551n.com/j551n/n-docs.git /home/gitlab-runner/n-docs
else
cd /home/gitlab-runner/n-docs
git fetch origin
git reset --hard origin/main
fi
- cd /home/gitlab-runner/n-docs
- docker compose stop n-docs || true
- docker compose up --build -d
only:
- main
- sharedHow This Works
-
Check out the repository
The pipeline first verifies if the repository exists on the runner. If it doesn't, it clones it. Otherwise, it fetches the latest changes and performs a hard reset to match the remote branch exactly. -
Navigate to project folder
The script ensures all subsequent commands run in the correct directory. -
Stop existing services
The Docker servicen-docsis stopped if it's already running. The|| trueensures the pipeline doesn't fail if the service is not running. -
Deploy with Docker Compose
The service is rebuilt and started in detached mode. -
Trigger conditions
This job runs only for commits to themainandsharedbranches.
Benefits
- Automates deployment, reducing manual errors.
- Ensures the application is always up-to-date with the latest changes.
- Provides a repeatable and reliable deployment process.