From df7aeadf70e0c89c1adea70f666a23cc9517acd5 Mon Sep 17 00:00:00 2001 From: integrationninjas Date: Sun, 3 Dec 2023 19:05:28 +0530 Subject: [PATCH 01/15] test deployment --- .gihub/workflows/ci-cd.yml | 59 +++++++++++++++++++ pom.xml | 17 ++++++ .../controller/TestController.java | 20 ------- .../controller/UserController.java | 36 +++++++++++ .../springbootexample/dao/UserDao.java | 7 +++ .../springbootexample/dto/UserDto.java | 12 ++++ .../springbootexample/entity/User.java | 30 ++++++++++ .../service/UserService.java | 13 ++++ .../service/impl/UserServiceImpl.java | 46 +++++++++++++++ src/main/resources/application.properties | 5 ++ 10 files changed, 225 insertions(+), 20 deletions(-) create mode 100644 .gihub/workflows/ci-cd.yml delete mode 100644 src/main/java/com/integrationninjas/springbootexample/controller/TestController.java create mode 100644 src/main/java/com/integrationninjas/springbootexample/controller/UserController.java create mode 100644 src/main/java/com/integrationninjas/springbootexample/dao/UserDao.java create mode 100644 src/main/java/com/integrationninjas/springbootexample/dto/UserDto.java create mode 100644 src/main/java/com/integrationninjas/springbootexample/entity/User.java create mode 100644 src/main/java/com/integrationninjas/springbootexample/service/UserService.java create mode 100644 src/main/java/com/integrationninjas/springbootexample/service/impl/UserServiceImpl.java diff --git a/.gihub/workflows/ci-cd.yml b/.gihub/workflows/ci-cd.yml new file mode 100644 index 0000000..53a4a91 --- /dev/null +++ b/.gihub/workflows/ci-cd.yml @@ -0,0 +1,59 @@ +name: Deploy to AWS + +on: + push: + branches: [ deploy-to-ecs-fargate ] + +jobs: + build-and-deploy: + runs-on: [ ubuntu-latest ] + steps: + - name: Checkout source + uses: actions/checkout@v3 + - name: Setup Java + uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: '17' + - name: Build Project + run: mvn clean install -DskipTests + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v3 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: 'eu-north-1' + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + with: + mask-password: 'true' + + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + REPOSITORY: springboot-example + run: | + # Build a docker container and + # push it to ECR so that it can + # be deployed to ECS. + docker build -t $ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT + +# - name: Fill in 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: nodejs-app-task-definition.json +# container-name: nodejs-app +# image: ${{ steps.build-image.outputs.image }} +# - 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: nodejs-app-service +# cluster: DevCluster +# wait-for-service-stability: true \ No newline at end of file diff --git a/pom.xml b/pom.xml index aeee93a..7a98f2e 100644 --- a/pom.xml +++ b/pom.xml @@ -32,6 +32,23 @@ org.springframework.boot spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + org.projectlombok + lombok + true + + + + com.mysql + mysql-connector-j + runtime + diff --git a/src/main/java/com/integrationninjas/springbootexample/controller/TestController.java b/src/main/java/com/integrationninjas/springbootexample/controller/TestController.java deleted file mode 100644 index 9a5f4ba..0000000 --- a/src/main/java/com/integrationninjas/springbootexample/controller/TestController.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.integrationninjas.springbootexample.controller; - -import java.util.HashMap; -import java.util.Map; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class TestController { - - @GetMapping - public Object hello() { - Map object = new HashMap<>(); - object.put("name", "Integration Ninjas"); - object.put("email", "integrationninjas@gmail.com"); - return object; - } - -} diff --git a/src/main/java/com/integrationninjas/springbootexample/controller/UserController.java b/src/main/java/com/integrationninjas/springbootexample/controller/UserController.java new file mode 100644 index 0000000..97e6421 --- /dev/null +++ b/src/main/java/com/integrationninjas/springbootexample/controller/UserController.java @@ -0,0 +1,36 @@ +package com.integrationninjas.springbootexample.controller; + +import java.util.List; +import com.integrationninjas.springbootexample.dto.UserDto; +import com.integrationninjas.springbootexample.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class UserController { + + @Autowired + private UserService userService; + + @GetMapping("/users") + public ResponseEntity> getUsers() { + List usersList = userService.getUsers(); + return new ResponseEntity<>(usersList, HttpStatus.OK); + } + + @PostMapping("/users") + public ResponseEntity createUser(@RequestBody UserDto userDto) { + try { + String status = userService.createUser(userDto); + return new ResponseEntity<>(status, HttpStatus.CREATED); + } catch (Exception e) { + return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); + } + } + +} diff --git a/src/main/java/com/integrationninjas/springbootexample/dao/UserDao.java b/src/main/java/com/integrationninjas/springbootexample/dao/UserDao.java new file mode 100644 index 0000000..26e3038 --- /dev/null +++ b/src/main/java/com/integrationninjas/springbootexample/dao/UserDao.java @@ -0,0 +1,7 @@ +package com.integrationninjas.springbootexample.dao; + +import com.integrationninjas.springbootexample.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserDao extends JpaRepository { +} diff --git a/src/main/java/com/integrationninjas/springbootexample/dto/UserDto.java b/src/main/java/com/integrationninjas/springbootexample/dto/UserDto.java new file mode 100644 index 0000000..dd9cf92 --- /dev/null +++ b/src/main/java/com/integrationninjas/springbootexample/dto/UserDto.java @@ -0,0 +1,12 @@ +package com.integrationninjas.springbootexample.dto; + +import lombok.*; + +@Data +public class UserDto { + + private Long id; + private String firstName; + private String lastName; + private String email; +} diff --git a/src/main/java/com/integrationninjas/springbootexample/entity/User.java b/src/main/java/com/integrationninjas/springbootexample/entity/User.java new file mode 100644 index 0000000..3da05fc --- /dev/null +++ b/src/main/java/com/integrationninjas/springbootexample/entity/User.java @@ -0,0 +1,30 @@ +package com.integrationninjas.springbootexample.entity; + +import jakarta.persistence.*; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; + +@Getter +@Setter +@NoArgsConstructor +@AllArgsConstructor +@Entity +@Table(name = "users") +public class User { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private String firstName; + + @Column(nullable = false) + private String lastName; + + @Column(nullable = false, unique = true) + private String email; + +} diff --git a/src/main/java/com/integrationninjas/springbootexample/service/UserService.java b/src/main/java/com/integrationninjas/springbootexample/service/UserService.java new file mode 100644 index 0000000..dd636a1 --- /dev/null +++ b/src/main/java/com/integrationninjas/springbootexample/service/UserService.java @@ -0,0 +1,13 @@ +package com.integrationninjas.springbootexample.service; + +import com.integrationninjas.springbootexample.dto.UserDto; +import com.integrationninjas.springbootexample.entity.User; + +import java.util.List; + +public interface UserService { + + String createUser(UserDto userDto); + + List getUsers(); +} diff --git a/src/main/java/com/integrationninjas/springbootexample/service/impl/UserServiceImpl.java b/src/main/java/com/integrationninjas/springbootexample/service/impl/UserServiceImpl.java new file mode 100644 index 0000000..734517f --- /dev/null +++ b/src/main/java/com/integrationninjas/springbootexample/service/impl/UserServiceImpl.java @@ -0,0 +1,46 @@ +package com.integrationninjas.springbootexample.service.impl; + +import com.integrationninjas.springbootexample.dao.UserDao; +import com.integrationninjas.springbootexample.dto.UserDto; +import com.integrationninjas.springbootexample.entity.User; +import com.integrationninjas.springbootexample.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +@Service +public class UserServiceImpl implements UserService { + + @Autowired + UserDao userDao; + + @Override + public String createUser(UserDto userDto) { + User user = new User(); + user.setFirstName(userDto.getFirstName()); + user.setLastName(userDto.getLastName()); + user.setEmail(userDto.getEmail()); + userDao.saveAndFlush(user); + return "User Added Successfully"; + } + + @Override + public List getUsers() { + List usersList = userDao.findAll(); + List dtoList = new ArrayList<>(); + if (!usersList.isEmpty()) { + usersList.forEach(user -> { + UserDto dto = new UserDto(); + dto.setId(user.getId()); + dto.setFirstName(user.getFirstName()); + dto.setLastName(user.getLastName()); + dto.setEmail(user.getEmail()); + dtoList.add(dto); + }); + } + return dtoList; + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8b13789..0c4ae0d 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,6 @@ +spring.datasource.url=jdbc:mysql://user-management.cbvxsmsv5dwh.eu-north-1.rds.amazonaws.com:3306/user-management +spring.datasource.username=root +spring.datasource.password=password +spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file From b4e4c64f2b622015de72a5c93613f1a23605bec1 Mon Sep 17 00:00:00 2001 From: integrationninjas Date: Sun, 3 Dec 2023 19:07:36 +0530 Subject: [PATCH 02/15] test deployment --- .gihub/workflows/ci-cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gihub/workflows/ci-cd.yml b/.gihub/workflows/ci-cd.yml index 53a4a91..53d8e69 100644 --- a/.gihub/workflows/ci-cd.yml +++ b/.gihub/workflows/ci-cd.yml @@ -2,7 +2,7 @@ name: Deploy to AWS on: push: - branches: [ deploy-to-ecs-fargate ] + branches: [ test-sql ] jobs: build-and-deploy: From 90f79dbcd875620dcc2d58c3f4d81fab60c04d48 Mon Sep 17 00:00:00 2001 From: integrationninjas <140945507+integrationninjas@users.noreply.github.com> Date: Sun, 3 Dec 2023 19:21:44 +0530 Subject: [PATCH 03/15] Update ci-cd.yml --- .gihub/workflows/ci-cd.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.gihub/workflows/ci-cd.yml b/.gihub/workflows/ci-cd.yml index 53d8e69..d562745 100644 --- a/.gihub/workflows/ci-cd.yml +++ b/.gihub/workflows/ci-cd.yml @@ -1,9 +1,7 @@ name: Deploy to AWS - on: push: branches: [ test-sql ] - jobs: build-and-deploy: runs-on: [ ubuntu-latest ] @@ -56,4 +54,4 @@ jobs: # task-definition: ${{ steps.task-def.outputs.task-definition }} # service: nodejs-app-service # cluster: DevCluster -# wait-for-service-stability: true \ No newline at end of file +# wait-for-service-stability: true From c46bfccb795e8d914a7f267b6255ad7a5af328f9 Mon Sep 17 00:00:00 2001 From: integrationninjas <140945507+integrationninjas@users.noreply.github.com> Date: Sun, 3 Dec 2023 19:22:58 +0530 Subject: [PATCH 04/15] Rename ci-cd.yml to ci-cd.yml --- {.gihub => .github}/workflows/ci-cd.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {.gihub => .github}/workflows/ci-cd.yml (100%) diff --git a/.gihub/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml similarity index 100% rename from .gihub/workflows/ci-cd.yml rename to .github/workflows/ci-cd.yml From 2a1e00a60787f7a15835f72f903f93901bedb9ad Mon Sep 17 00:00:00 2001 From: integrationninjas <140945507+integrationninjas@users.noreply.github.com> Date: Sun, 3 Dec 2023 19:36:22 +0530 Subject: [PATCH 05/15] Create Dockerfile --- Dockerfile | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..dd53ba2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM eclipse-temurin:17-jdk-alpine +WORKDIR /app +COPY target/springboot-example.jar springboot-example.jar +EXPOSE 8080 +CMD ["java","-jar","springboot-example.jar"] From 121cd8fa005b20b0adcacbb19978845986dc8dab Mon Sep 17 00:00:00 2001 From: integrationninjas Date: Tue, 5 Dec 2023 19:06:02 +0530 Subject: [PATCH 06/15] test deployment --- pom.xml | 6 +++--- src/main/resources/application.properties | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 7a98f2e..61f0f27 100644 --- a/pom.xml +++ b/pom.xml @@ -45,9 +45,9 @@ - com.mysql - mysql-connector-j - runtime + mysql + mysql-connector-java + 8.0.29 diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 0c4ae0d..de4b4b0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,4 @@ -spring.datasource.url=jdbc:mysql://user-management.cbvxsmsv5dwh.eu-north-1.rds.amazonaws.com:3306/user-management +spring.datasource.url=jdbc:mysql://user-management.cbvxsmsv5dwh.eu-north-1.rds.amazonaws.com:3306/user_management spring.datasource.username=root spring.datasource.password=password From 6875fd2be71ca5c1dc57f4dd1928f4e74851fe27 Mon Sep 17 00:00:00 2001 From: integrationninjas Date: Tue, 5 Dec 2023 19:09:33 +0530 Subject: [PATCH 07/15] test deployment 1 --- src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index de4b4b0..6e37861 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,4 +3,4 @@ spring.datasource.username=root spring.datasource.password=password spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect -spring.jpa.hibernate.ddl-auto=update \ No newline at end of file +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file From 0cd79c2f053d0f1f3bca82399fc59b706c207512 Mon Sep 17 00:00:00 2001 From: integrationninjas Date: Wed, 6 Dec 2023 18:14:06 +0530 Subject: [PATCH 08/15] test deployment 2 --- .github/workflows/ci-cd.yml | 28 ++++---- springboot-app-task-definition.json | 88 +++++++++++++++++++++++ src/main/resources/application.properties | 2 +- 3 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 springboot-app-task-definition.json diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index d562745..eeb4f90 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -41,17 +41,17 @@ jobs: docker push $ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG echo "image=$ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT -# - name: Fill in 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: nodejs-app-task-definition.json -# container-name: nodejs-app -# image: ${{ steps.build-image.outputs.image }} -# - 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: nodejs-app-service -# cluster: DevCluster -# wait-for-service-stability: true + - name: Fill in 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: springboot-app-task-definition + container-name: springboot-example + image: ${{ steps.build-image.outputs.image }} + - 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-example-service + cluster: DevCluster + wait-for-service-stability: true diff --git a/springboot-app-task-definition.json b/springboot-app-task-definition.json new file mode 100644 index 0000000..1d78bf1 --- /dev/null +++ b/springboot-app-task-definition.json @@ -0,0 +1,88 @@ +{ + "taskDefinitionArn": "arn:aws:ecs:eu-north-1:974142680250:task-definition/springboot-app-task-definition:1", + "containerDefinitions": [ + { + "name": "springboot-example", + "image": "974142680250.dkr.ecr.eu-north-1.amazonaws.com/springboot-example:latest", + "cpu": 0, + "portMappings": [ + { + "name": "springboot-example-8080-tcp", + "containerPort": 8080, + "hostPort": 8080, + "protocol": "tcp", + "appProtocol": "http" + } + ], + "essential": true, + "environment": [], + "environmentFiles": [], + "mountPoints": [], + "volumesFrom": [], + "ulimits": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-create-group": "true", + "awslogs-group": "/ecs/springboot-app-task-definition", + "awslogs-region": "eu-north-1", + "awslogs-stream-prefix": "ecs" + }, + "secretOptions": [] + } + } + ], + "family": "springboot-app-task-definition", + "taskRoleArn": "arn:aws:iam::974142680250:role/ecsTaskExecutionRole", + "executionRoleArn": "arn:aws:iam::974142680250:role/ecsTaskExecutionRole", + "networkMode": "awsvpc", + "revision": 1, + "volumes": [], + "status": "ACTIVE", + "requiresAttributes": [ + { + "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" + }, + { + "name": "ecs.capability.execution-role-awslogs" + }, + { + "name": "com.amazonaws.ecs.capability.ecr-auth" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" + }, + { + "name": "com.amazonaws.ecs.capability.task-iam-role" + }, + { + "name": "ecs.capability.execution-role-ecr-pull" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" + }, + { + "name": "ecs.capability.task-eni" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29" + } + ], + "placementConstraints": [], + "compatibilities": [ + "EC2", + "FARGATE" + ], + "requiresCompatibilities": [ + "FARGATE" + ], + "cpu": "1024", + "memory": "3072", + "runtimePlatform": { + "cpuArchitecture": "X86_64", + "operatingSystemFamily": "LINUX" + }, + "registeredAt": "2023-12-05T18:03:50.753Z", + "registeredBy": "arn:aws:iam::974142680250:user/integration-ninjas", + "tags": [] +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6e37861..de4b4b0 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -3,4 +3,4 @@ spring.datasource.username=root spring.datasource.password=password spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect -spring.jpa.hibernate.ddl-auto=update \ No newline at end of file +spring.jpa.hibernate.ddl-auto=update \ No newline at end of file From b0ce3c693e43ffd46d36f723464443f3c2ec0d78 Mon Sep 17 00:00:00 2001 From: integrationninjas Date: Wed, 6 Dec 2023 18:17:52 +0530 Subject: [PATCH 09/15] test deployment 3 --- .github/workflows/ci-cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index eeb4f90..243e6f1 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -45,7 +45,7 @@ jobs: id: task-def uses: aws-actions/amazon-ecs-render-task-definition@v1 with: - task-definition: springboot-app-task-definition + task-definition: springboot-app-task-definition.json container-name: springboot-example image: ${{ steps.build-image.outputs.image }} - name: Deploy Amazon ECS task definition From 2f2bae5ac98993daed1e46d6bba8dc4f3c29fa0f Mon Sep 17 00:00:00 2001 From: integrationninjas Date: Wed, 6 Dec 2023 18:20:06 +0530 Subject: [PATCH 10/15] test deployment 4 --- .github/workflows/ci-cd.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index 243e6f1..d348b45 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -53,5 +53,5 @@ jobs: with: task-definition: ${{ steps.task-def.outputs.task-definition }} service: springboot-example-service - cluster: DevCluster + cluster: DevCluster1 wait-for-service-stability: true From 241793baabad5a109ef1e5ff2fab9f0c40a94b72 Mon Sep 17 00:00:00 2001 From: integrationninjas Date: Fri, 8 Dec 2023 23:32:46 +0530 Subject: [PATCH 11/15] test deployment 4 --- .github/workflows/ci-cd.yml | 57 ------------------- Dockerfile | 5 -- springboot-app-task-definition.json | 88 ----------------------------- 3 files changed, 150 deletions(-) delete mode 100644 .github/workflows/ci-cd.yml delete mode 100644 Dockerfile delete mode 100644 springboot-app-task-definition.json diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml deleted file mode 100644 index d348b45..0000000 --- a/.github/workflows/ci-cd.yml +++ /dev/null @@ -1,57 +0,0 @@ -name: Deploy to AWS -on: - push: - branches: [ test-sql ] -jobs: - build-and-deploy: - runs-on: [ ubuntu-latest ] - steps: - - name: Checkout source - uses: actions/checkout@v3 - - name: Setup Java - uses: actions/setup-java@v3 - with: - distribution: 'temurin' - java-version: '17' - - name: Build Project - run: mvn clean install -DskipTests - - name: Configure AWS credentials - uses: aws-actions/configure-aws-credentials@v3 - with: - aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} - aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} - aws-region: 'eu-north-1' - - name: Login to Amazon ECR - id: login-ecr - uses: aws-actions/amazon-ecr-login@v1 - with: - mask-password: 'true' - - - name: Build, tag, and push image to Amazon ECR - id: build-image - env: - ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} - IMAGE_TAG: ${{ github.sha }} - REPOSITORY: springboot-example - run: | - # Build a docker container and - # push it to ECR so that it can - # be deployed to ECS. - docker build -t $ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG . - docker push $ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG - echo "image=$ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT - - - name: Fill in 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: springboot-app-task-definition.json - container-name: springboot-example - image: ${{ steps.build-image.outputs.image }} - - 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-example-service - cluster: DevCluster1 - wait-for-service-stability: true diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index dd53ba2..0000000 --- a/Dockerfile +++ /dev/null @@ -1,5 +0,0 @@ -FROM eclipse-temurin:17-jdk-alpine -WORKDIR /app -COPY target/springboot-example.jar springboot-example.jar -EXPOSE 8080 -CMD ["java","-jar","springboot-example.jar"] diff --git a/springboot-app-task-definition.json b/springboot-app-task-definition.json deleted file mode 100644 index 1d78bf1..0000000 --- a/springboot-app-task-definition.json +++ /dev/null @@ -1,88 +0,0 @@ -{ - "taskDefinitionArn": "arn:aws:ecs:eu-north-1:974142680250:task-definition/springboot-app-task-definition:1", - "containerDefinitions": [ - { - "name": "springboot-example", - "image": "974142680250.dkr.ecr.eu-north-1.amazonaws.com/springboot-example:latest", - "cpu": 0, - "portMappings": [ - { - "name": "springboot-example-8080-tcp", - "containerPort": 8080, - "hostPort": 8080, - "protocol": "tcp", - "appProtocol": "http" - } - ], - "essential": true, - "environment": [], - "environmentFiles": [], - "mountPoints": [], - "volumesFrom": [], - "ulimits": [], - "logConfiguration": { - "logDriver": "awslogs", - "options": { - "awslogs-create-group": "true", - "awslogs-group": "/ecs/springboot-app-task-definition", - "awslogs-region": "eu-north-1", - "awslogs-stream-prefix": "ecs" - }, - "secretOptions": [] - } - } - ], - "family": "springboot-app-task-definition", - "taskRoleArn": "arn:aws:iam::974142680250:role/ecsTaskExecutionRole", - "executionRoleArn": "arn:aws:iam::974142680250:role/ecsTaskExecutionRole", - "networkMode": "awsvpc", - "revision": 1, - "volumes": [], - "status": "ACTIVE", - "requiresAttributes": [ - { - "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" - }, - { - "name": "ecs.capability.execution-role-awslogs" - }, - { - "name": "com.amazonaws.ecs.capability.ecr-auth" - }, - { - "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" - }, - { - "name": "com.amazonaws.ecs.capability.task-iam-role" - }, - { - "name": "ecs.capability.execution-role-ecr-pull" - }, - { - "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" - }, - { - "name": "ecs.capability.task-eni" - }, - { - "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29" - } - ], - "placementConstraints": [], - "compatibilities": [ - "EC2", - "FARGATE" - ], - "requiresCompatibilities": [ - "FARGATE" - ], - "cpu": "1024", - "memory": "3072", - "runtimePlatform": { - "cpuArchitecture": "X86_64", - "operatingSystemFamily": "LINUX" - }, - "registeredAt": "2023-12-05T18:03:50.753Z", - "registeredBy": "arn:aws:iam::974142680250:user/integration-ninjas", - "tags": [] -} \ No newline at end of file From 7797ecc0719b7df2e3d4ad8a0e87193749d61c2e Mon Sep 17 00:00:00 2001 From: integrationninjas <140945507+integrationninjas@users.noreply.github.com> Date: Sat, 9 Dec 2023 00:51:52 +0530 Subject: [PATCH 12/15] Create springboot-app-task-definition.json --- springboot-app-task-definition.json | 88 +++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 springboot-app-task-definition.json diff --git a/springboot-app-task-definition.json b/springboot-app-task-definition.json new file mode 100644 index 0000000..291b67f --- /dev/null +++ b/springboot-app-task-definition.json @@ -0,0 +1,88 @@ +{ + "taskDefinitionArn": "arn:aws:ecs:eu-north-1:974142680250:task-definition/springboot-app-task-definition:6", + "containerDefinitions": [ + { + "name": "springboot-example", + "image": "974142680250.dkr.ecr.eu-north-1.amazonaws.com/springboot-example:latest", + "cpu": 0, + "portMappings": [ + { + "name": "springboot-example-8080-tcp", + "containerPort": 8080, + "hostPort": 8080, + "protocol": "tcp", + "appProtocol": "http" + } + ], + "essential": true, + "environment": [], + "environmentFiles": [], + "mountPoints": [], + "volumesFrom": [], + "ulimits": [], + "logConfiguration": { + "logDriver": "awslogs", + "options": { + "awslogs-create-group": "true", + "awslogs-group": "/ecs/springboot-app-task-definition", + "awslogs-region": "eu-north-1", + "awslogs-stream-prefix": "ecs" + }, + "secretOptions": [] + } + } + ], + "family": "springboot-app-task-definition", + "taskRoleArn": "arn:aws:iam::974142680250:role/ecsTaskExecutionRole", + "executionRoleArn": "arn:aws:iam::974142680250:role/ecsTaskExecutionRole", + "networkMode": "awsvpc", + "revision": 6, + "volumes": [], + "status": "ACTIVE", + "requiresAttributes": [ + { + "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" + }, + { + "name": "ecs.capability.execution-role-awslogs" + }, + { + "name": "com.amazonaws.ecs.capability.ecr-auth" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.19" + }, + { + "name": "com.amazonaws.ecs.capability.task-iam-role" + }, + { + "name": "ecs.capability.execution-role-ecr-pull" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.18" + }, + { + "name": "ecs.capability.task-eni" + }, + { + "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29" + } + ], + "placementConstraints": [], + "compatibilities": [ + "EC2", + "FARGATE" + ], + "requiresCompatibilities": [ + "FARGATE" + ], + "cpu": "1024", + "memory": "3072", + "runtimePlatform": { + "cpuArchitecture": "X86_64", + "operatingSystemFamily": "LINUX" + }, + "registeredAt": "2023-12-08T19:03:51.274Z", + "registeredBy": "arn:aws:iam::974142680250:user/integration-ninjas", + "tags": [] +} From 0e1494bdc302bdbcf4b948902f97b0c7af5f2173 Mon Sep 17 00:00:00 2001 From: integrationninjas Date: Sat, 9 Dec 2023 00:55:11 +0530 Subject: [PATCH 13/15] deploy to ecs --- .github/workflows/ci-cd.yml | 59 +++++++++++++++++++++++++++++++++++++ Dockerfile | 5 ++++ 2 files changed, 64 insertions(+) create mode 100644 .github/workflows/ci-cd.yml create mode 100644 Dockerfile diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml new file mode 100644 index 0000000..bfdbd64 --- /dev/null +++ b/.github/workflows/ci-cd.yml @@ -0,0 +1,59 @@ +name: Deploy to AWS ECS +on: + push: + branches: [ deploy-to-ecs ] +jobs: + build-and-deploy: + runs-on: [ ubuntu-latest ] + steps: + - name: Checkout source + uses: actions/checkout@v3 + + - name: Setup Java + uses: actions/setup-java@v3 + with: + distribution: 'temurin' + java-version: '17' + + - name: Build Project + run: mvn clean install -DskipTests + + - name: Configure AWS credentials + uses: aws-actions/configure-aws-credentials@v3 + with: + aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} + aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} + aws-region: 'eu-north-1' + + - name: Login to Amazon ECR + id: login-ecr + uses: aws-actions/amazon-ecr-login@v1 + with: + mask-password: 'true' + + - name: Build, tag, and push image to Amazon ECR + id: build-image + env: + ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} + IMAGE_TAG: ${{ github.sha }} + run: | + # Build a docker container and + # push it to ECR so that it can + # be deployed to ECS. + docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT + - name: Fill in 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: springboot-app-task-definition.json + container-name: springboot-example + image: ${{ steps.build-image.outputs.image }} + - 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-example-service + cluster: DevCluster2 + wait-for-service-stability: true \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..5119b62 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,5 @@ +FROM eclipse-temurin:17-jdk-alpine +WORKDIR /app +COPY target/springboot-example.jar springboot-example.jar +EXPOSE 8080 +CMD ["java","-jar","springboot-example.jar"] \ No newline at end of file From 2fb1f206a60acacc71a2120cac6ff4b370143220 Mon Sep 17 00:00:00 2001 From: integrationninjas <140945507+integrationninjas@users.noreply.github.com> Date: Sat, 9 Dec 2023 01:02:19 +0530 Subject: [PATCH 14/15] Update ci-cd.yml --- .github/workflows/ci-cd.yml | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci-cd.yml b/.github/workflows/ci-cd.yml index bfdbd64..69eed43 100644 --- a/.github/workflows/ci-cd.yml +++ b/.github/workflows/ci-cd.yml @@ -36,13 +36,14 @@ jobs: env: ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} IMAGE_TAG: ${{ github.sha }} + REPOSITORY: springboot-example run: | # Build a docker container and # push it to ECR so that it can # be deployed to ECS. - docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . - docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG - echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT + docker build -t $ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG . + docker push $ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG + echo "image=$ECR_REGISTRY/$REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT - name: Fill in the new image ID in the Amazon ECS task definition id: task-def uses: aws-actions/amazon-ecs-render-task-definition@v1 @@ -56,4 +57,4 @@ jobs: task-definition: ${{ steps.task-def.outputs.task-definition }} service: springboot-example-service cluster: DevCluster2 - wait-for-service-stability: true \ No newline at end of file + wait-for-service-stability: true From c4d137e22f91b52f63a61aa90172fc1bc9238540 Mon Sep 17 00:00:00 2001 From: Soumik-CICD <60919299+Soumik-CICD@users.noreply.github.com> Date: Wed, 24 Sep 2025 01:38:38 +0530 Subject: [PATCH 15/15] Update springboot-app-task-definition.json --- springboot-app-task-definition.json | 75 ++++++++++++++++------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/springboot-app-task-definition.json b/springboot-app-task-definition.json index 291b67f..fa5d554 100644 --- a/springboot-app-task-definition.json +++ b/springboot-app-task-definition.json @@ -1,44 +1,57 @@ { - "taskDefinitionArn": "arn:aws:ecs:eu-north-1:974142680250:task-definition/springboot-app-task-definition:6", + "compatibilities": [ + "EC2", + "FARGATE" + ], "containerDefinitions": [ { - "name": "springboot-example", - "image": "974142680250.dkr.ecr.eu-north-1.amazonaws.com/springboot-example:latest", "cpu": 0, - "portMappings": [ - { - "name": "springboot-example-8080-tcp", - "containerPort": 8080, - "hostPort": 8080, - "protocol": "tcp", - "appProtocol": "http" - } - ], - "essential": true, "environment": [], "environmentFiles": [], - "mountPoints": [], - "volumesFrom": [], - "ulimits": [], + "essential": true, + "image": "493233983811.dkr.ecr.us-east-1.amazonaws.com/springboot-example:latest", "logConfiguration": { "logDriver": "awslogs", "options": { - "awslogs-create-group": "true", "awslogs-group": "/ecs/springboot-app-task-definition", - "awslogs-region": "eu-north-1", + "awslogs-create-group": "true", + "awslogs-region": "us-east-1", "awslogs-stream-prefix": "ecs" }, "secretOptions": [] - } + }, + "mountPoints": [], + "name": "springboot-example", + "portMappings": [ + { + "appProtocol": "http", + "containerPort": 80, + "hostPort": 80, + "name": "springboot-example-80-tcp", + "protocol": "tcp" + }, + { + "appProtocol": "http", + "containerPort": 8080, + "hostPort": 8080, + "name": "springboot-example-8080-tcp", + "protocol": "tcp" + } + ], + "systemControls": [], + "ulimits": [], + "volumesFrom": [] } ], + "cpu": "1024", + "enableFaultInjection": false, + "executionRoleArn": "arn:aws:iam::493233983811:role/ecsTaskExecutionRole", "family": "springboot-app-task-definition", - "taskRoleArn": "arn:aws:iam::974142680250:role/ecsTaskExecutionRole", - "executionRoleArn": "arn:aws:iam::974142680250:role/ecsTaskExecutionRole", + "memory": "3072", "networkMode": "awsvpc", - "revision": 6, - "volumes": [], - "status": "ACTIVE", + "placementConstraints": [], + "registeredAt": "2025-09-23T19:14:25.667Z", + "registeredBy": "arn:aws:iam::493233983811:root", "requiresAttributes": [ { "name": "com.amazonaws.ecs.capability.logging-driver.awslogs" @@ -68,21 +81,17 @@ "name": "com.amazonaws.ecs.capability.docker-remote-api.1.29" } ], - "placementConstraints": [], - "compatibilities": [ - "EC2", - "FARGATE" - ], "requiresCompatibilities": [ "FARGATE" ], - "cpu": "1024", - "memory": "3072", + "revision": 1, "runtimePlatform": { "cpuArchitecture": "X86_64", "operatingSystemFamily": "LINUX" }, - "registeredAt": "2023-12-08T19:03:51.274Z", - "registeredBy": "arn:aws:iam::974142680250:user/integration-ninjas", + "status": "ACTIVE", + "taskDefinitionArn": "arn:aws:ecs:us-east-1:493233983811:task-definition/springboot-app-task-definition:1", + "taskRoleArn": "arn:aws:iam::493233983811:role/ecsTaskExecutionRole", + "volumes": [], "tags": [] }