Ci Cd
#Continuous Integration and Continuous Deployment (CI/CD)
Continuous Integration and Continuous Deployment (CI/CD) is a method to frequently deliver apps to customers by introducing automation into the stages of app development.
#What is CI/CD?
#Continuous Integration (CI)
Continuous Integration is a development practice where developers integrate code into a shared repository frequently, preferably several times a day. Each integration can then be verified by an automated build and automated tests.
#Continuous Delivery (CD)
Continuous Delivery is an extension of continuous integration to ensure that you can release new changes to your customers quickly in a sustainable way. This means that on top of having automated your testing, you also have automated your release process and you can deploy your application at any point of time by clicking a button.
#Continuous Deployment
Continuous Deployment goes one step further than Continuous Delivery. With this practice, every change that passes all stages of your production pipeline is released to your customers. There's no human intervention, and only a failed test will prevent a new change to be deployed to production.
#CI/CD Pipeline Components
A typical CI/CD pipeline includes the following stages:
- Source: Code is committed to a version control system (Git, SVN, etc.)
- Build: Code is compiled, dependencies are resolved
- Test: Automated tests are run (unit tests, integration tests, etc.)
- Deploy: Application is deployed to staging/production environments
- Monitor: Application performance and errors are monitored
#Popular CI/CD Tools
#Jenkins
Jenkins is an open-source automation server that enables developers to build, test, and deploy their software.
# Example Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
echo 'Building..'
sh 'npm install'
}
}
stage('Test') {
steps {
echo 'Testing..'
sh 'npm test'
}
}
stage('Deploy') {
steps {
echo 'Deploying....'
sh 'npm run deploy'
}
}
}
}
#GitHub Actions
GitHub Actions is a CI/CD platform that allows you to automate your build, test, and deployment pipeline directly from GitHub.
# Example GitHub Actions workflow
name: Node.js CI
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Use Node.js
uses: actions/setup-node@v1
with:
node-version: '14.x'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
- name: Deploy
if: github.ref == 'refs/heads/main'
run: npm run deploy
#GitLab CI/CD
GitLab CI/CD is a part of GitLab that allows you to apply all the continuous methods (Continuous Integration, Delivery, and Deployment) to your software.
# Example .gitlab-ci.yml
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Building the app"
- npm install
test:
stage: test
script:
- echo "Running tests"
- npm test
deploy:
stage: deploy
script:
- echo "Deploying application"
- npm run deploy
only:
- main
#CircleCI
CircleCI is a cloud-based CI/CD tool that automates the software development process.
# Example CircleCI configuration
version: 2.1
jobs:
build:
docker:
- image: cimg/node:14.17
steps:
- checkout
- run: npm install
- run: npm test
- run: npm run deploy
#Best Practices for CI/CD
- Automate Everything: Automate as much of the software delivery process as possible.
- Fail Fast: Detect and address issues as early as possible in the development process.
- Keep the Build Green: A broken build should be the team's highest priority to fix.
- Build Only Once: Build artifacts once and promote the same artifacts through the pipeline.
- Deploy the Same Way to Every Environment: Use the same deployment process for all environments.
- Smoke Test Your Deployments: Run basic tests after deployment to verify the system is running correctly.
- Keep Your CI/CD Pipeline Fast: Aim for a pipeline that completes in less than 10 minutes.
- Maintain Good Test Coverage: Ensure your tests cover most of your codebase.
#References
Code Snippets
Tags:
Source:
Sourced from: docs/devops/ci-cd.md