Project Overview
For this project, I was tasked with creating a Jenkins pipeline to automate the process of building a Docker image from our codebase and pushing it to an Amazon Elastic Container Registry (ECR) repository. This project required familiarity with AWS services like AWS CodeBuild and AWS ECR, as well as proficiency in GitHub and Jenkins.
Project Workflow
Here’s a detailed breakdown of the project workflow:
1. Setting Up Jenkins
I began by setting up a Jenkins job. In my case, I used a Windows server as my Jenkins server. The primary function of this job was to retrieve the codebase from GitHub and trigger an AWS CodeBuild job.
2. AWS CodeBuild Job
The AWS CodeBuild job was configured to look for a file called `buildspec.yml` in the root folder of our codebase. This file contains instructions for the build process.
3. Sample `buildspec.yml`
phases:
install:
runtime-versions:
java: openjdk8
docker: 18
python: 3.7
pre_build:
commands:
- chmod +x gradlew
- echo Logging in to Amazon ECR...
- $(aws ecr get-login --no-include-email --region us-east-1)
build:
commands:
- echo Build started on `date`
- ./gradlew build -x test
- echo Building the Docker image...
- docker build -t cp-batch .
- docker tag {tags to tag the image }
post_build:
commands:
- echo Build completed
- echo Pushing the Docker images...
- docker push {ECR account name}
This `buildspec.yml` file defines the following phases:
- Install Phase: It installs the necessary runtime versions, such as Java, Docker, and Python.
- Pre-Build Phase: In this phase, it logs in to the AWS ECR account to prepare for pushing the Docker image.
-Build Phase: Here, it starts the build process, which includes building the project with Gradle and creating a Docker image while tagging it for identification.
- Post-Build Phase: Finally, in this phase, the Docker image that was built and tagged is pushed to our AWS ECR image repository.
This pipeline streamlines the process of building and deploying Docker images, making it a crucial part of our continuous integration and continuous deployment (CI/CD) workflow.