In this Jenkins script, we are performing several tasks to build and deploy an application using Docker, AWS ECR, Terraform, and Kubernetes. Here's a breakdown of the steps in a more understandable format:
Step 1: Build Docker Image and Push to AWS ECR
- Start by building a Docker image.
- The image is then pushed to an AWS Elastic Container Registry (ECR).
Step 2: Create AWS Resources
Under the "CREATE ENVIRONMENT" section, we set up AWS resources:
-
Configure the AWS environment variables such as AWS_DEFAULT_REGION, AWS_CREDENTIALS, and PATH.
-
Create an AWS ECR repository using the
aws ecr create-repository
command. -
Create an AWS S3 bucket using the
aws s3 mb
command. -
Create an AWS DynamoDB table using the
aws dynamodb create-table
command. Step 3: AWS ECR Authentication -
Use AWS CLI commands to authenticate with AWS ECR from within our Jenkins pipeline script.
Step 4: Terraform Initialization and Deployment
- Use Terraform to initialize and deploy infrastructure.
- Terraform reads configuration files from the
terraform/ecr_registry
directory. - The Terraform outputs (registry_id, repository_name, repository_url) are captured for later use.
Step 5: Build and Deploy Docker Image
- Build a Docker image from the Dockerfile.
- The image is tagged with the repository name and build ID.
- The Docker image is pushed to the ECR repository.
- Clean up by removing local Docker images.
Step 6: Kubernetes Deployment
- Configure the Kubernetes credentials and server URL.
- A Docker registry secret is created for accessing the ECR repository.
- Apply Kubernetes manifests (
k8sbook.yaml
andkjavaapps.yaml
) usingkubectl apply
. - Before applying
kjavaapps.yaml
, we useenvsubst
to substitute environment variables in the YAML file.
Additional Notes:
- You have defined functions for creating AWS resources (ECR, S3 bucket, DynamoDB) and retrieving Terraform paths.
- You use a Jenkins agent to run our pipeline stages.
- The Jenkins slave is set up to communicate with Kubernetes.
- You have configured Kubernetes credentials for Jenkins using
kubectl get secrets
. - You have to establish a tunnel for Jenkins to communicate with Kubernetes.
- Instead of a pod template, we use a Kubernetes manifest for defining the build pod.
- The Docker image used in the build pod contains AWS, Docker, Python, and kubectl.
- The Kubernetes cluster information is obtained using
kubectl cluster-info
.
Overall, this Jenkins script automates the process of building, deploying, and configuring various AWS and Kubernetes resources for your application, providing a streamlined CI/CD pipeline.