AWS CodeBuild Tutorial: Extracting Code from Maven Build

AWS CodeBuild DevOps Tool:

In Part 1 of the AWS DevOps tools, we saw how CodeCommit service was used to store the source code in a secure online version control service which is a pre-requisite for any DevOps implementation.

In Part 2 of the series, we will learn more about how the code from the CodeCommit repository can be compiled on the cloud using the CodeBuild service and ANT or Maven build utility.

                     Suggested Reading => Absolute DevOps Training Series

Most importantly there is no need to provide any build server. The artifacts produced are then stored in the AWS S3 bucket which is a Simple Storage Service for storing and retrieving any amount of data on the internet. These 2 activities in both the tutorials form a part of Continuous Integration.

AWS CodeBuild Devops Tutorial

In this tutorial, we will also see how Jenkins can be integrated with the CodeBuild.

Pre-Requisite:

  • Account with AWS preferably a free tier.
  • Good and fast internet connection.
  • AWS Region used – Asia Pacific (Singapore).

Note: If you have used AWS region (Mumbai) for CodeCommit service then this region is not supported in the CodeBuild. You need to use the appropriate region supported and hence Singapore region is used here.

Build Process in CodeBuild

Following is the process that is followed in the build while using the CodeBuild service:

  • The source code is fetched from the CodeCommit repository. The source code can also be stored in the GitHub or even an S3 bucket.
  • The CodeBuild service relies on a file called buildspec.yml which contains some commands to run during the build phases like Install, Pre-Build, Build and Post-Build. This file needs to be in the ROOT of the project folder and have to be committed to the repository.
  • If in case you do not want to use the buildspec.yml to perform the build then the maven commands can also be directly used.
  • Finally, the generated artifacts like J2EE JAR/WAR/EAR file is stored in the S3 bucket.
  • Notifications using AWS Simple Notification Service (SNS) can also be configured to email the build results to the relevant users.

Here, we will see how to use the CodeCommit repository (refer to part 1) and maven command to build our project and store the artifact in the S3 bucket.

Create Build Project

Login to AWS and launch the CodeBuild service. Click to create a new Build project.

Enter the details as follows:

Project name: AWS-HelloWorld

Source provider: AWS CodeCommit

Repository: AWS-HelloWorld

Operating system: Ubuntu

Runtime: Java

Runtime version: openjdk-8

Build specification: Insert build commands

Build commands: mvn clean install

Output files: Target/AWS-HelloWorld-1.0.0.war (This is in a format of Artifact ID-Version taken from the POM file).

Artifacts: Where to put the artifacts from this build project.

Type: Amazon S3

Name: HWJavaWebTarget (This will be the folder within the bucket).

Bucket name: hwcodebuildbucket (The bucket has to be created initially before running the build and must be in lowercase as per conventions).

The following 3 screenshots are from one single form to Configure the build project:

Artifacts

Click Continue and then Save. The build can now be triggered by clicking on the Start Build button.

Click again on the Start Build to view the progress of the execution.

The build is now successfully completed and the artifacts are uploaded to the S3 bucket.

View of the S3 bucket

The WAR file can now be used for deployment to the appropriate application servers.

Jenkins Integration with AWS CodeBuild

As we saw in the tutorial #1 of this series on how Jenkins was configured with CodeCommit repository for enabling the Continuous Integration activity. As the version control repository is configured, you can now trigger a build by integrating the CodeBuild with the same Jenkins build job.

The AWS CodeBuild plugin to Jenkins needs to be installed for integration. You can download and install it from here.

Use Jenkins => Manage Plugins to install the plugin.

Select the AWS CodeBuild as the Build Environment and select the appropriate CodeBuild project configured in the AWS as shown below. The IAM user Access keys and Secret keys will also need to be entered as a part of the configuration.

The key point that needs to be remembered here is that the build has not been triggered on the on-premise environments but is done on the cloud. So there is no need to provision any build servers.

Summary

Through this tutorial, we came to know how the CodeCommit repository can be used to pull the code and perform a Maven build using the CodeBuild service.

The build can be triggered by Jenkins as well. The artifact generated, which is primarily a WAR file, is then stored in the AWS S3 bucket.

In our upcoming tutorial, we will see how the WAR file from the S3 bucket can be used to deploy the Tomcat application server using the AWS CodeDeploy service.

PREV Tutorial | NEXT Tutorial