This blog describes deploying a spring-boot Application into AWS ESC via GitHub Actions. We will build a sample spring-boot Application, push the image to AWS ECR and then deploy it to AWS ECS.
Prerequisites:
1. AWS Account
2. GitHub Account
Create an AWS ECR repository
Login to your AWS account and create an Amazon Elastic Container Registry repository with a name.
Create a cluster inside AWS ECS and select the cluster template as “Networking Only” because we use AWS FARGATE here.
Create a Task Definition
Select launch type as FARGATE, select task role as “ecsTaskExecutionRole”, Select your operating system, select Task memory and CPU, and Add container with container name and the image should be the ECR repository URI.
Create a service inside the cluster
Click on Clusters then select our cluster and create a service with a service name and select launch type as FARGATE.
Create a task-definition.json file on the Project repository
Go to the project repository, create a new file “task-definition.json” and the file content will get from ( Task Definition –> select your task –> click on the Task name –> Get a JSON file and copy the file content and paste it into the “task-definition.json” file.)
Create the workflow
Add a YAML file to your repository (/.github/workflows/filename.yml)
name: Build and Deploy to AWS
on:
push:
branches:
- main
jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish, and Deploy
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
# Setup JDK 1.8
- name: Set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
server-id: github
settings-path: ${{ github.workspace }}
# Build
- name: Build and Test with Maven
run: mvn -B package --file pom.xml
# Configure AWS credentials
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-south-1
# Login to Amazon ECR
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
# Build, tag, and push image to Amazon ECR
- name: Build, tag, and push image to Amazon ECR
id: build-image
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: springboot
IMAGE_TAG: develop
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
echo "::set-output name=image::$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
# Push the new image ID in the Amazon ECS task definition
- name: Push the new image ID in the Amazon ECS task definition
id: task-def
uses: aws-actions/amazon-ecs-render-task-definition@v1
with:
task-definition: task-definition.json
container-name: springboot-container
image: ${{ steps.build-image.outputs.image }}
# Deploy Amazon ECS task definition
- name: Deploy Amazon ECS task definition
uses: aws-actions/amazon-ecs-deploy-task-definition@v1
with:
task-definition: ${{ steps.task-def.outputs.task-definition }}
service: springboot-service
cluster: springboot-cluster
wait-for-service-stability: true
Edit the pom.xml file and add the line
It indicates that the name of the generated artifact will be spring-boot-ecs. This name is often used to create a JAR or WAR file, which can then be deployed to a server or container, such as Amazon Elastic Container Service (ECS), to run a Spring Boot application
Create a Dockerfile inside the project repository
FROM maven:3.8.5-openjdk-18-slim AS build
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN mvn package
FROM openjdk:18-jdk-slim
EXPOSE 80
ARG JAR_FILE=spring-boot-ecs.jar
WORKDIR /opt/app
COPY --from=build /usr/src/app/target/${JAR_FILE} /opt/app/
ENTRYPOINT ["java","-jar","spring-boot-ecs.jar"]
Then click on Action
Here the image is pushed into the AWS ECR repository and deployed this image into AWS ECS. After completing the deployment go to the AWS cluster, click on Task and get a public IP, copy this IP, and search into a browser.