Spring Boot Application AWS Elastic Beanstalk CI/CD with GitHub Actions — Part 02
In Part 1, we set up a Spring Boot application and deployed it manually to AWS Elastic Beanstalk. In this part, we’ll configure Continuous Integration and Continuous Deployment (CI/CD) using GitHub Actions to automate the deployment process.
STEP — 05: Setup IAM User and Permissions
Let’s return to the AWS console and select the IAM service. Unlike the application we previously set up, IAM is region-independent, unlike the application we set up which is bound to the us-east-1 region of N. Virginia.
On Users in Menu on Left Side, and then click Add Users
to create user
Select Attach policies directly and for blogging purpose I have selected the AdministratorAccess but it can be vary with your requirements.
Move to the Tags page, we can skip it for now. Just Review the user and click Create user
Once the user is ready, view the user and Create access Key
Select Application running on an AWS compute service, then move to the Tags page, we can ignore it. Just Review the Access Key and click Done
Upon successful creation, capture the Access Key ID and Secret Access Key, which we will need for the GitHub Actions Workflow setup. You can also download the details as a .csv file.
That completes our action on the AWS side.
STEP — 06: Setup GitHub Action for CI/CD
Fore mostly we have to create a repository in GitHub https://github.com/SangeethRaajA/aws-deployment-github
Before writing the GitHub Actions workflow, we need to add the Access Key ID and Secret Key of our user, which we captured in the last step, as GitHub Secrets. Go to Settings of repository --> Secrets and Variables --> Action --> New Repository Secret
With above keys added as a secret, we would be ready to refer them in our GitHub Actions Workflow.
STEP — 07: Setup workflow in local machine
Create a folder named .github/workflows
at the root level of your repository. Then, create a file named main.yml
in that folder and make the necessary changes as indicated in the comments.
name: CI/CD Pipeline
on:
workflow_dispatch:
# this will trigger workflow whenever a change is pushed to main branch
push:
branches:
- main
jobs:
build:
name: Build Archive
# Will run steps on latest version of ubuntu
runs-on: ubuntu-latest
steps:
# Check-out your repository under $GITHUB_WORKSPACE, so your workflow can access it
- uses: actions/checkout@v1
# Set up JDK 8
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: '17'
# Set up Maven cache
- name: Cache Maven packages
# This action allows caching dependencies and build outputs to improve workflow execution time.
uses: actions/cache@v1
with:
path: ~/.m2
key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-m2
# Build the application using Maven
- name: Build with Maven
run: mvn -B package -DskipTests --file pom.xml
- name: Upload JAR
# We upload so we can re-use same jar in next job.
uses: actions/upload-artifact@v2
with:
# Name of artifact can be anything
name: artifact
# Relative path to jar file
path: target/aws-deployment-github-0.0.1-SNAPSHOT.jar
# Deploy's job
deploy:
# Depends on build's job
needs: build
name: Deploy to Elastic Beanstalk
# Will run steps on latest version of ubuntu
runs-on: ubuntu-latest
steps:
- name: Download JAR
# Download the artifact which was uploaded in the Build Archive's job
uses: actions/download-artifact@v2
with:
name: artifact
# Deploy the artifact (JAR) into AWS Beanstalk
- name: Deploy to EB
uses: einaregilsson/beanstalk-deploy@v13
with:
aws_access_key: ${{ secrets.AWS_ACCESS_KEY_ID }} # This is referred from Github Secrets
aws_secret_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} # This is referred from Github Secrets
use_existing_version_if_available: true
application_name: demo-eb-ci-cd # Application name we created in Elastic Beanstalk
environment_name: Demo-eb-ci-cd-env # Environment name we created in Elastic Beanstalk
version_label: ${{ github.SHA }}
region: us-east-1 # VERY IMPORTANT: AWS Region where initially Application was created in AWS EBS. We created in us-east-1 which is N-Virginia
deployment_package: aws-deployment-github-0.0.1-SNAPSHOT.jar # Download artifacts from previous job
We will now push our changes, but this time we’ll update application.properties as follows:
As soon as a change is commit and pushed , GitHub Action is triggered. FYI some instance we must check on manual workflow in Actions -> CI/CD Pipeline -> Run workflow
If everything is fine, we’ll receive logs and any errors for each step of individual runs.
Now, accessing the api/version endpoint shows an updated value.
We have successfully set up CI/CD with GitHub Actions. The workflow will run whenever there is a change in the main branch, but you can adjust the settings as needed.
Reference :
Thank you for reading! If you enjoyed the article, please like it to encourage me to write more. I value your feedback and suggestions. Feel free to connect with me at LinkedIn Sangeethraj Arulraj.