This is a CI/CD Model for building and updating docker images from github (as a repo) and ECS cluster as a target.
This project assumes you are have some basic knowledge of Springboot applications, Docker, Git, Github and AWS Pipeline. If you are not familiar with these concepts/tools, please visit the following links:
- https://www.docker.com/101-tutorial
- https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#getting-started
- https://docs.aws.amazon.com/whitepapers/latest/introduction-devops-aws/welcome.html
- https://docs.aws.amazon.com/codebuild/latest/APIReference/codebuild-api.pdf
- https://docs.github.com/en/get-started/quickstart/create-a-repo
- Creates a source(repo) as github where the dockerfile, sourcecode(Javafiles) as well as configurations(buildspec) and uri of image, etc., are stored.
- Once this source is created, the pipeline can then be connected by linking this repo as source in the first stage of the codepipeline.
- In the second stage, codebuild takes the dockerfile and javafiles and creates an image with it.
- The image created is the stored in a private repository in ECR.
- Each time there is a commit on github, the pipeline is triggered and codebuild compiles the Docker image.
- ECR is then configured to use the latest version of the image gotten from ECR.
Organize the sourcecode for your Springboot application locally(organised properly in directories). Using git, the following commands will initialize your repo, add a readme, make your first commit and main branch, initialize changes made to your repo locally and push the local version to perform a full sync.
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin yourrepo
git push -u origin main
For more information on this please visit https://docs.github.com/en/get-started/using-git/about-git#github-and-the-command-line
pre_build:
commands:
- mvn clean install
- echo Logging in to Amazon ECR...
- aws --version
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
- REPOSITORY_URI= Repository URI e.g. 484927367294.dkr.ecr.us-east-1.amazonaws.com/javadockerized
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=build-$(echo $CODEBUILD_BUILD_ID | awk -F":" '{print $2}')
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
- docker build -t $REPOSITORY_URI:latest .
- docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG
post_build:
commands:
- echo Build completed on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"order-service","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
- cat imagedefinitions.json
artifacts:
files:
- imagedefinitions.json
- target/order-service.jar
Configure codebuild as follows:
While configuring our codebuild, notice that we set Github as our source so our Buildspec can be fetched from our repo(source). We also ensure that the "priviledged" checkbox is ticked. This would give elevated permissions needed for docker operations.
Assign the below policies to your codebuildrole
- Create codepipeline
- Ensure that it pushes these changes to the master branch.
- If we want, we can push to a dev branch for a staging environment.
Skip the deploy stage and create the pipeline
- Select Fargate
- Select Linux
- Select Task Definition
- Select VPC and Subnet
- Expose ports 80 and 8080
- Enable auto Assign IP
- Run task
- Configure VPC, subnet,
- Ensure that auto assign IP is enabled and ensure that the security group has its ports exposed(8080 and 80) so you can access your application on that post
Each time the source(master branch) is modified, the pipeline is triggered and the newer version is updated