Skip to content

Ayodejiola/javaAWSDocker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

154591988-0ac89658-90e0-49e0-9753-2c20b716b1c1

CI/CD For a Springboot Application on AWS ECS

This is a CI/CD Model for building and updating docker images from github (as a repo) and ECS cluster as a target.

Documentation

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:

Summary:

  • 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.

Step 1: Creating a repo as source

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

Step 2: Create ECR and copy URI

image image

Step 3: Specify your buildspec as seen below

  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

Step 4: Create codebuild

Configure codebuild as follows:

codebuildeditted

codebuild2eddited

image

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.

Step 5: Create codepipeline, specify the stages(source, codebuild- created earlier)

Assign the below policies to your codebuildrole image

  • 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.

image pipelinesource image

Skip the deploy stage and create the pipeline

Step 6: Create Task Definition and select the container image URI you created in ECR.

fargate

image image

Step 7: Create an ECS Cluster and run task(using task definition created earlier)

run taskedditted

  • 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

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published