Simulating an end-to-end CI/CD Pipeline for a Java web app using AWS Deployment Tools, using CodeArtifact to cache dependencies, CodeBuild to run the build servers, CodeDeploy to deploy to production, CodePipeline to automate these, and CloudFormation to set-up the resource stacks. The journey is documented in the pdfs through parts 1 to 7.
- Create a new EC2 instance and install maven
wget https://archive.apache.org/dist/maven/maven-3/3.5.2/binaries/apache-maven-3.5.2-bin.tar.gz
sudo tar -xzf apache-maven-3.5.2-bin.tar.gz -C /opt
echo "export PATH=/opt/apache-maven-3.5.2/bin:$PATH" >> ~/.bashrc
source ~/.bashrc
- Install Java 8
sudo dnf install -y java-1.8.0-amazon-corretto-devel
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-amazon-corretto.x86_64
export PATH=/usr/lib/jvm/java-1.8.0-amazon-corretto.x86_64/jre/bin/:$PATH
- Generate webapp template
mvn archetype:generate \
-DgroupId=com.nextwork.app \
-DartifactId=nextwork-web-project \
-DarchetypeArtifactId=maven-archetype-webapp \
-DinteractiveMode=false
Use this step if you want to use the CloudFormation template directly. Otherwise, do Step 2B to set things up manually for the first time.
- Create Build and Deploy stack using
CICDpipeline-NextWorkWebAppSetup-template.yaml
- Configure CodeArtifact connection using connection instructions from AWS console (create
settings.xml
) and compile project - Set CodeBuild's source as your CodeCommit/Github repo
- Create CodeCommit/Github repo, add url to project folder, and push
- Configure CodeArtifact connection using connection instructions from AWS console (create
settings.xml
) and compile project - Set-up IAM Policy for accessing CodeArtifact
- Set-up S3 bucket for storing build artifact
- Create CodeBuild project and add above CodeArtifact IAM policy to CodeBuild service role
- Create Deployment server with a VPC stack using
DeployServer-EC2-VPC-template.yaml
- Upload Appspec.yml
- Upload Bash scripts for the Deployment server
- Upload full Buildspec.yml (includes
appspec.yml
andscripts/**/*
folder in build artifacts)
- Create CodeDeploy service role to allow access to EC2 deployment server, with
AWSCodeDeployRole
permission - Create CodeDeploy application and deployment group
- Create a Deployment
- Follow the prompts to set-up a new pipeline
- Commit new code to repo, and watch the pipeline trigger itself automatically
- Click "rollback" on deployment stage to rollback to a previous version, or "release changes" to go to ship the most updated version
- Issue 1: Unable to compile and publish dependencies to CodeArtifact
- Solution: If necessary, start from a fresh instance or uninstall Maven first. Switch your Java runtime and compiler to Java 8 (openjdk 1.8) with
sudo alternatives --config java
andsudo alternatives --config javac
before installing Maven, and ensure that you get at least Maven version 3.2.5 or above. Recompile again withmvn -s settings.xml compile
.
- Solution: If necessary, start from a fresh instance or uninstall Maven first. Switch your Java runtime and compiler to Java 8 (openjdk 1.8) with
- Issue 2: Unable to create stack on CloudFormation due to circular reference
- Solution: resolve dependencies by removing the Managed Policies in the CodeBuild IAM Service Role entry