End-to-End CI/CD : Jenkins, Terraform, and Azure Container Apps

End-to-End CI/CD

Prerequisites and Context

Before diving into the End-to-End CI/CD setup, let’s establish the groundwork. This methodology is designed to work seamlessly in a local WSL (Windows Subsystem for Linux) environment. While perfect for testing and development, you can easily scale this approach to a production-level setup by leveraging Linux servers from your preferred cloud provider.

Here’s a quick overview of the prerequisites:
  1. Local Environment: We’re working on a WSL environment, but the steps are adaptable for cloud-hosted Linux servers for production deployment.
  2. SonarQube for Code Quality: A Docker container running SonarQube is used to analyze the quality and security of the code during the CI process.
  3. Trivy for Vulnerability Scanning: Trivy, a security scanner for container images, is installed on the VM with necessary permissions to ensure the security of your deployment.
  4. Terraform for Infrastructure Automation: Terraform is installed on the VM, ready to automate Azure resource provisioning. Ensure your setup includes the necessary permissions to access your Azure environment.
  5. Encrypted Credentials: The Terraform repository for this setup includes three essential files:
    • main.tf: Defines the infrastructure resources.
    • variables.tf: Specifies configurable variables for the deployment.
    • terraform.tfvars: Stores sensitive credentials, encrypted using GPG to enhance security.

If your Terraform code doesn’t follow this exact structure or methodology, you’ll need to tweak the Jenkins pipeline accordingly. The encrypted credentials approach ensures your secrets are protected while allowing the pipeline to securely deploy the infrastructure.

With these prerequisites in place, we’re ready to build a pipeline that brings together code quality, security, and deployment automation into a single streamlined workflow.

Step 1: Setting the Foundation with GitHub

Every successful CI/CD pipeline begins with a well-organized source code repository. For this setup, our web application’s source code will live in a GitHub repository—a reliable and developer-friendly platform for managing code.

With your code now on GitHub, it’s not just stored—it’s primed for action. This repository will serve as the starting point for automating builds, tests, and deployments, setting the stage for the CI/CD pipeline magic that lies ahead.

Step 2: Installing Tools and Setting Up Prerequisites

To build a seamless CI/CD pipeline, it’s essential to have all the required tools and plugins in place. Here’s a step-by-step guide to installing and configuring everything you’ll need in your local WSL environment.

1. Install Jenkins

Begin by installing Jenkins, the heart of our CI/CD setup:

Access Jenkins via http://localhost:8080 and follow the setup wizard. Use the initial admin password from /var/lib/jenkins/secrets/initialAdminPassword to log in.

2. Install Required Jenkins Plugins

To support Terraform, Docker, and security scanning, install these plugins:

  • Aqua Security Scanner
  • Docker(API, Commons Plugin, Pipeline)
  • Eclipse Temurin Installer Plugin (To install different tools like node/jdk etc)
  • SonarQube Scanner
  • Azure
  • Git
  • All the Default plugins while jenkins Installation

Install them via Manage Jenkins > Plugin Manager > Available Plugins

3. Set Up Docker and SonarQube

Install Docker:

Run SonarQube in a Docker container:

The command written in blue is used to run a docker container using volumes you can omit the blue line if you don’t want persistency in your SQ container

Access SonarQube at http://localhost:9000 and set up an admin account.

Default Username & Pass : admin

4. Install Trivy for Security Scanning

Install Trivy for vulnerability scanning:

5. Install Terraform

Install Terraform for infrastructure management:

6. Configure Encrypted Credentials for Terraform

Click on this to learn about this more

Step 3: Understanding the Jenkinsfile (Pipeline 1 – CI )

The Jenkinsfile is the heart of your pipeline, defining every stage and action in a declarative syntax. Let’s break down this Jenkinsfile step by step to understand its purpose and functionality.

End-to-end CI/CD Pipeline Overview

  1. Agent and Tools Setup:
    • agent any: This tells Jenkins to run the pipeline on any available agent.
    • tools: Specifies the required tools, such as JDK 17 and Node.js 16, ensuring the environment is set up correctly for the tasks.
  2. Environment Variables:
    • Variables like APP_NAME, DOCKER_USER, IMAGE_NAME, and IMAGE_TAG help manage application and container details dynamically.
    • Credentials such as JENKINS_API_TOKEN and DOCKER_PASS are securely fetched using Jenkins credentials.
    • The SCANNER_HOME points to the SonarQube scanner tool for code quality analysis.

Stages

1. SonarQube Analysis

  • This stage runs a code quality analysis using SonarQube.
  • The withSonarQubeEnv block ensures SonarQube environment variables are available.
  • The scanner is invoked with project details like projectName and projectKey.

2. Build Docker Image

  • Jenkins uses Docker to build an image for the web application.
  • The image is tagged with the dynamic IMAGE_NAME and built in the workspace.

3. Trivy Scanning

  • Security scanning for vulnerabilities is done using Trivy.
  • Reports are saved in the workspace with a timestamped filename for traceability.

4. Push Docker Image

  • The Docker image is pushed to a container registry (Docker Hub in this case).
  • Both a versioned tag and the latest tag are pushed for flexibility.

5. Cleanup Artifacts

  • Docker images are removed from the build environment to save space.
  • This step ensures the workspace remains clean after the build.

Post Actions

  • On pipeline success, a curl command triggers another Jenkins pipeline (PRE_PROD Pipeline) using the built image.
  • Parameters like IMAGE_TAG are passed to the next job for seamless integration.

Step 4 : Understanding the Jenkinsfile (Pipeline 2 – CD)

This pipeline automates Terraform workflows, such as creating or tearing down Azure infrastructure, using parameters for dynamic control. It also handles secure file decryption, updates configurations, and cleans up sensitive files post-execution.

End to End CI/CD Pipeline Overview

  • Agent: Runs the pipeline on any available Jenkins agent.
  • Parameters: Introduces a dropdown for action, allowing users to choose between plan and destroy.
  • Environment Variables:
    • TFVARS_PASSPHRASE: Securely fetches the passphrase for decrypting Terraform variables.
    • APP_NAME: Name of the application, reused for dynamic image updates.

Stages Explained

1. Terraform Initialization

Prepares the Terraform working directory by downloading provider plugins and configuring the backend. Ensures the environment is ready for infrastructure actions.

2. Decrypting the Terraform Variables

  • Decrypts the encrypted terraform.tfvars.gpg file to retrieve sensitive variables like service principal credentials.
  • Uses GPG for secure decryption with the passphrase fetched from Jenkins credentials.

3. Updating the Image Name

Dynamically updates the image_name variable in terraform.tfvars to use the latest Docker image built in the previous pipeline.Uses the sed command for inline editing of the file.

4. Executing Terraform Actions

Executes the Terraform command specified by the action parameter:

  • plan: Shows proposed changes without making them.
  • destroy: Tears down the infrastructure.
  • Apply: Applies the Plan and creates infrastructure

Offers flexibility to choose the operation at runtime.

5. Cleanup Sensitive Files

Deletes the decrypted terraform.tfvars file after its use to ensure no sensitive information remains.

Post Actions

We clean up the workspace, removing all temporary files and directories after the pipeline execution.

With our End to End CI/CD pipelines explained and configured, the final step of deploying your application to Azure Container Apps is already in motion. Once the Terraform apply stage completes, the infrastructure is created or updated in Azure, and your web application is live on a scalable, containerized platform.

This process reflects the power of combining Jenkins, Terraform, and Azure to automate infrastructure and application deployments. The seamless integration of tools and techniques ensures:

  • End-to-End CI/CD Automation: Automate every step of the CI/CD process, from code commits to live application deployment, ensuring efficiency and consistency.
  • Security Best Practices: We encrypt sensitive data, decrypt it only when needed, and remove it after use.
  • Dynamic and Scalable Infrastructure: Azure Container Apps offer automatic scaling, managing your app’s needs without manual intervention.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *