Junior
Tell about the main stages of CI/CD.
sobes.tech AI
Answer from AI
Conceptually, CI/CD is divided into two main stages:
-
Continuous Integration (CI):
- Commit: Developers commit changes to a shared code repository (e.g., Git).
- Build: The application is automatically built from the source code.
- Test: Automated tests (unit, integration, static analyzers) are run.
-
Continuous Delivery/Deployment (CD):
- Deploy to Staging: The successfully built and tested artifact is deployed to an intermediate environment (Stage) that mimics production.
- Automated Acceptance Testing: Automated acceptance tests are performed on the Stage environment to verify business logic.
- Manual/Exploratory Testing: Manual testing on Stage is conducted if necessary.
- Deploy to Production: Upon successful completion of all previous stages, the artifact is deployed to the production environment. In Continuous Deployment, this step is fully automated. In Continuous Delivery, manual confirmation may be required.
- Monitoring and Feedback: After deployment, monitoring of the application's operation in production is carried out to identify issues and gather feedback.
Here is an example pipeline in Jenkinsfile:
// Jenkinsfile
pipeline {
agent any
stages {
stage('Build') {
steps {
sh 'mvn clean install -DskipTests' // Example of Maven build, skipping tests at this stage
}
}
stage('Test') {
steps {
sh 'mvn test' // Running unit and integration tests
}
post {
always {
junit '**/TEST-*.xml' // Publishing test results
}
}
}
stage('Static Analysis') {
steps {
sh 'mvn sonar:sonar' // Static analysis with SonarQube
}
}
stage('Build Docker Image') {
steps {
script {
docker.build("my-app:${env.BUILD_NUMBER}") // Building Docker image with build number tag
}
}
}
stage('Deploy to Stage') {
steps {
sh 'ssh user@stage-server "deploy_script.sh my-app:${env.BUILD_NUMBER}"' // Deploying to Stage via SSH
}
}
stage('Acceptance Tests') {
steps {
sh 'run_acceptance_tests.sh' // Running automated acceptance tests on Stage
}
}
stage('Deploy to Production') {
when {
environment name: 'CONTINUOUS_DEPLOYMENT', value: 'true' // Condition for automatic deployment to Production
}
steps {
sh 'ssh user@prod-server "deploy_script.sh my-app:${env.BUILD_NUMBER}"' // Deploying to Production
}
}
stage('Manual Approval for Production') { // This stage can be included for Continuous Delivery
when {
environment name: 'CONTINUOUS_DEPLOYMENT', value: 'false'
}
steps {
input message: 'Deploy to Production?', ok: 'Deploy Now'
sh 'ssh user@prod-server "deploy_script.sh my-app:${env.BUILD_NUMBER}"'
}
}
}
post {
failure {
mail to: 'devops@example.com', subject: "Build ${env.BUILD_NUMBER} Failed", body: "Build failed for ${env.JOB_NAME}" // Sending failure notification
}
success {
mail to: 'devops@example.com', subject: "Build ${env.BUILD_NUMBER} Succeeded", body: "Build succeeded for ${env.JOB_NAME}" // Sending success notification
}
}
}
Main differences between Continuous Delivery and Continuous Deployment:
| Characteristic | Continuous Delivery | Continuous Deployment |
|---|---|---|
| Deployment to Production | Requires manual confirmation | Fully automated |
| Deployment frequency | Can deploy frequently | Deployment occurs with every successful change |
| Stability | High, but with manual control | Maximal, with proper automated testing |
| Risks | Lower due to manual control | Higher, but reduced through comprehensive testing |
Each stage of the pipeline should be automated and perform its specific task.