CI/CD Pipeline Best Practices

Manish Sharma
7 min readMar 3, 2024


CI/CD pipeline best practices include:

  • Automated Testing: Integrate automated unit tests, integration tests, and end-to-end tests into your pipeline to ensure code quality.
  • Version Control: Use a version control system like Git to manage your codebase and track changes effectively.
  • Infrastructure as Code (IaC): Define your infrastructure using code (e.g., Terraform, CloudFormation) to enable reproducibility and versioning of environments.
  • Continuous Integration (CI): Automate the process of merging code changes into a shared repository frequently to detect integration errors early.
  • Artifact Management: Store build artifacts (e.g., Docker images, JAR files) in a central repository for easy retrieval and versioning.
  • Immutable Deployments: Deploy immutable artifacts to ensure consistency and avoid configuration drift.
  • Continuous Deployment (CD): Automate the deployment process to move code changes through multiple environments (e.g., development, staging, production) safely and efficiently.
  • Monitoring and Logging: Integrate monitoring and logging tools (e.g., Prometheus, ELK stack) into your pipeline to track performance metrics and detect issues promptly.
  • Security Scanning: Incorporate security scanning tools (e.g., SonarQube, OWASP ZAP) to identify and address vulnerabilities early in the development process.
  • Feedback Loops: Implement feedback mechanisms to provide developers with insights into build and deployment outcomes, enabling continuous improvement.

Artifact Management

Artifact management is a critical aspect of the software development lifecycle, ensuring that all necessary components, such as binaries, libraries, and documentation, are stored, versioned, and organized efficiently. Choose a repository manager such as JFrog Artifactory, Sonatype Nexus, or AWS CodeArtifact to store artifacts.

Importance of Artifact Management:

  1. Version Control: Centralized storage allows teams to track and manage versions of artifacts, ensuring consistency and reproducibility.
  2. Dependency Management: Easily manage dependencies by storing and resolving artifact dependencies automatically.
  3. Reusability: Artifacts can be reused across projects, reducing duplication and promoting consistency.
  4. Security and Compliance: Securely store artifacts and ensure compliance with licensing and security policies.
  5. Traceability: Track changes and updates to artifacts, enabling traceability and auditability.

Example of a Bitbucket Pipeline that publishes artifacts to AWS CodeArtifact:

- step:
name: Build and Publish Artifact
image: maven:3.8.3-jdk-11
- mvn clean install
# Install and configure AWS CLI
- apt-get update && apt-get install -y python3-pip
- pip3 install awscli
- aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
- aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
- aws configure set region $AWS_DEFAULT_REGION
# Publish artifact to AWS CodeArtifact
- aws codeartifact publish-package-version --domain my-domain --repository my-repo --format maven --namespace com.example --package <package-name> --package-version <package-version> --asset-name my-application.jar
- target/my-application.jar

Monitoring and Logging

Monitoring and logging are crucial components of a CI/CD pipeline, providing visibility into the health and performance of the pipeline itself as well as the applications it deploys. Here’s a deeper look at each:


Monitoring involves tracking the state and performance of various components of the CI/CD pipeline in real-time. It helps identify issues, bottlenecks, and opportunities for optimization. Key aspects of monitoring in a CI/CD pipeline include:

  1. Pipeline Metrics: Monitoring the duration of each pipeline stage, success/failure rates of builds and deployments, and resource utilization.
  2. Infrastructure Monitoring: Monitoring the underlying infrastructure where the CI/CD tools and applications are hosted, including servers, containers, and databases.
  3. Application Monitoring: Monitoring the performance and behavior of the deployed applications, including response times, error rates, and resource consumption.
  4. Alerting: Setting up alerts based on predefined thresholds to notify stakeholders about critical issues or anomalies.


Logging involves capturing and storing detailed information about events and activities within the CI/CD pipeline and the deployed applications. Logs provide valuable insights into the execution flow, errors, and debugging information. Key aspects of logging in a CI/CD pipeline include:

  1. Pipeline Logs: Capturing logs from each stage of the pipeline, including build logs, test logs, and deployment logs.
  2. Application Logs: Aggregating logs generated by the deployed applications, including application server logs, error logs, and custom logs.
  3. Structured Logging: Using structured logging formats (e.g., JSON) to capture additional context along with log messages, facilitating easier analysis and filtering.
  4. Log Aggregation: Collecting logs from multiple sources into a centralized logging system or platform for easier management, analysis, and retention.
  5. Log Retention and Archiving: Establishing policies for log retention and archiving to meet compliance requirements and facilitate post-incident analysis.

Implementation in CI/CD Pipeline

Integrating monitoring and logging into a CI/CD pipeline involves:

  1. Instrumentation: Adding monitoring and logging libraries to application code and CI/CD scripts to generate relevant metrics and logs.
  2. Integration with Monitoring Tools: Configuring CI/CD tools to send metrics and logs to monitoring and logging platforms such as Prometheus, Grafana, ELK Stack (Elasticsearch, Logstash, Kibana), or cloud-based solutions like AWS CloudWatch and Azure Monitor.
  3. Alerting Configuration: Setting up alerting rules based on predefined thresholds and conditions to notify stakeholders via email, Slack, or other communication channels.
  4. Continuous Improvement: Regularly reviewing monitoring dashboards, analyzing logs, and refining alerting thresholds to improve the efficiency and reliability of the CI/CD pipeline.

Security Scanning

Security scanning in a pipeline involves integrating security checks into the automated CI/CD process to detect vulnerabilities and ensure code quality. Here’s a deep dive into how security scanning can be implemented in a Bitbucket Pipeline:

  • SAST tools like SonarQube or Checkmark can be integrated into the pipeline to analyze the source code for potential security vulnerabilities, such as SQL injection, XSS, and insecure coding practices.
  • Dependency scanning tools like OWASP Dependency-Check or Snyk can be used to identify known vulnerabilities in third-party libraries and components.
  • Container image scanning tools like Trivy or Clair or Lacework or Sysdig Secure can be employed to scan Docker images for vulnerabilities before deployment.
  • Tools like GitLeaks can be used to scan code repositories for sensitive information such as API keys, passwords, or tokens.

Feedback Loops

Feedback loops are crucial in CI/CD pipelines to provide developers with insights into the outcome of their changes. Let’s deep dive into feedback loops with an example:

Consider a simple CI/CD pipeline for a web application hosted on AWS. Here’s how the pipeline might look:

  1. Code Commit: Developers push their changes to a Git repository (e.g., GitHub).
  2. Continuous Integration (CI) Stage:Automated tests (unit tests, integration tests) are triggered upon each code commit. If tests pass, the code is built into a deployable artifact (e.g., Docker image).
  3. Continuous Deployment (CD) Stage: Deploy the artifact to a staging environment for further testing. Run additional tests (e.g., end-to-end tests, performance tests) in the staging environment. If all tests pass, promote the artifact to production.
  4. Production Monitoring: Monitor the production environment for performance, availability, and security issues. Use tools like Prometheus, Grafana, and ELK stack for monitoring and logging.

Now, let’s focus on feedback loops at each stage:

  1. Code Commit Stage: Feedback: Automated test results and code quality metrics. Example: After each commit, developers receive notifications about test results via email or Slack. They can also view detailed reports in the CI/CD dashboard, including code coverage metrics and static code analysis findings.
  2. CI Stage: Feedback: Build status and test results. Example: Developers receive immediate feedback on the success or failure of the build. If the build fails, they are notified of the reasons (e.g., compilation errors, test failures) and can take corrective action promptly.
  3. CD Stage: Feedback: Deployment status and environment health. Example: Developers receive notifications about the deployment status to staging and production environments. They can monitor the health of these environments in real-time through dashboards and receive alerts for any anomalies or failures.
  4. Production Monitoring: Feedback: Performance metrics and incident alerts. Example: Developers receive alerts for performance degradation, errors, or security threats detected in the production environment. They can investigate these incidents promptly and make necessary adjustments to the application code or infrastructure configuration.

In summary, feedback loops in CI/CD pipelines provide developers with timely and actionable insights at each stage of the software delivery process, enabling continuous improvement and faster delivery of high-quality software.

Sample BitBucket pipeline,

# Bitbucket Pipeline Configuration

- step:
name: Run Automated Tests
- echo "Running automated tests..."
# Run unit tests, integration tests, etc.
- ./
- test_results/*.xml
- docker

- step:
name: Build and Publish Docker Image
- echo "Building Docker image..."
# Build Docker image from code
- docker build -t myapp:$BITBUCKET_COMMIT .
# Push Docker image to Docker registry (e.g., AWS ECR)
- docker push myapp:$BITBUCKET_COMMIT
- docker_image: myapp:$BITBUCKET_COMMIT
- docker

- step:
name: Deploy to Staging Environment
deployment: staging
- echo "Deploying to staging environment..."
# Deploy Docker image to staging environment (e.g., Kubernetes)
- kubectl apply -f kubernetes/staging.yaml
# Run additional tests in the staging environment
- ./
- docker

- step:
name: Deploy to Production Environment
deployment: production
- echo "Deploying to production environment..."
# Deploy Docker image to production environment (e.g., Kubernetes)
- kubectl apply -f kubernetes/production.yaml
- docker

- step:
name: Monitor Production Environment
- echo "Monitoring production environment..."
# Monitor production environment using monitoring tools (e.g., Prometheus, Grafana)
- ./

- step:
name: Send Alert
- echo "Sending alert..."
# Send alert to stakeholders (e.g., Slack notification, email)
- ./ sample code to send alert to slack channel


# Function to send alert notification
send_alert() {
# Define the message content
message="[REPO: $BITBUCKET_REPO_FULL_NAME][PIPELINE NUMBER: $BITBUCKET_BUILD_NUMBER] Pipeline failed: The Bitbucket Pipeline encountered an issue. Please check the pipeline logs for details."
# Send Via Slack Channel
curl -X POST -H 'Content-type: application/json' --data '{"text":"'"$message"'"}'

# Call the function to send alert
send_alert sample code to send alert to Microsoft Teams


# Function to send alert notification to Microsoft Teams
send_alert() {
# Define the Microsoft Teams webhook URL
# Define the message content
message="[REPO: $BITBUCKET_REPO_FULL_NAME][PIPELINE NUMBER: $BITBUCKET_BUILD_NUMBER] Pipeline failed: The Bitbucket Pipeline encountered an issue. Please check the pipeline logs for details."
# Define the JSON payload for Microsoft Teams
# Send alert via Microsoft Teams webhook
curl -X POST -H "Content-Type: application/json" -d "$payload" "$webhook_url"

# Call the function to send alert

Enjoyed this article? Follow me for more!

If you found this article helpful or insightful, consider following me on Medium to stay updated with my latest posts.

Thank you for reading!



Manish Sharma

I am technology geek & keep pushing myself to learn new skills. I am AWS Solution Architect — Associate, Professional & Terraform Associate Developer certified.