Skip to content

Commit

Permalink
feat: deploy spring app
Browse files Browse the repository at this point in the history
  • Loading branch information
yourzinc committed Jan 16, 2024
1 parent 66798b3 commit 3a26af4
Show file tree
Hide file tree
Showing 20 changed files with 649 additions and 13 deletions.
82 changes: 82 additions & 0 deletions .github/workflows/deploy-spring.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
name: AWS CodeDeploy Automation

on:
push:
branches:
- "deploy/spring"

env:
AWS_REGION : "ap-northeast-2"
CONTAINER_NAME: "spring-app"

permissions:
id-token: write
contents: read

jobs:
Deploy:
runs-on: ubuntu-latest
steps:

- name: Checkout repo
uses: actions/checkout@v4

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Build with Gradle
run: ./gradlew build -x test

- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: ${{ secrets.ROLE_NAME }}
aws-region: ${{ env.AWS_REGION }}

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2

- name: Build docker file and setting deploy files
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG .
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
- name: Prepare appspec.yml
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ secrets.ECR_REPOSITORY }}
IMAGE_TAG: ${{ github.sha }}
run: |
sed -i "s/<AWS_REGION>/${{ env.AWS_REGION }}/g" appspec.yml
sed -i "s/<CONTAINER_NAME>/${{ env.CONTAINER_NAME }}/g" appspec.yml
sed -i "s/<ECR_REGISTRY>/$ECR_REGISTRY/g" appspec.yml
sed -i "s/<ECR_REPOSITORY>/$ECR_REPOSITORY/g" appspec.yml
sed -i "s/<IMAGE_TAG>/$IMAGE_TAG/g" appspec.yml
- name: Make .zip
run: |
zip -r deployment_package.zip ./scripts/* appspec.yml
- name: Upload .zip to Amazon S3
run: |
aws s3 cp deployment_package.zip s3://${{ secrets.BUCKET_NAME }}/$GITHUB_SHA.zip
- name: Create deployment to AWS CodeDeploy
run: |
aws deploy create-deployment \
--application-name ${{ secrets.CODEDEPLOY_APP_NAME }} \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--deployment-group-name ${{ secrets.DEPLOYMENT_GROUP_NAME }} \
--s3-location bucket=${{ secrets.BUCKET_NAME }},key=$GITHUB_SHA.zip,bundleType=zip \
--file-exists-behavior OVERWRITE
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
HELP.md
.gradle
build/
!gradle/wrapper/gradle-wrapper.jar
!**/src/main/**/build/
!**/src/test/**/build/
/data1
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
out/
!**/src/main/**/out/
!**/src/test/**/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

.DS_Store

.env
7 changes: 5 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
FROM nginx
COPY ./index.html /usr/share/nginx/html
FROM openjdk:17
VOLUME /tmp
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar", "-Duser.timezone=Asia/Seoul","/app.jar"]
12 changes: 11 additions & 1 deletion appspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: 0.0
os: linux
files:
- source: /
destination: /home/ec2-user/my-nginx-app
destination: /home/ec2-user/my-spring-app
hooks:
BeforeInstall:
- location: scripts/before_install.sh
Expand All @@ -12,11 +12,21 @@ hooks:
- location: scripts/start_docker.sh
timeout: 300
runas: root
environment:
AWS_REGION: "<AWS_REGION>"
CONTAINER_NAME: "<CONTAINER_NAME>"
ECR_REGISTRY: "<ECR_REGISTRY>"
ECR_REPOSITORY: "<ECR_REPOSITORY>"
IMAGE_TAG: "<IMAGE_TAG>"
ApplicationStop:
- location: scripts/stop_docker.sh
timeout: 300
runas: root
environment:
CONTAINER_NAME: "<CONTAINER_NAME>"
ValidateService:
- location: scripts/validate_service.sh
timeout: 300
runas: root
environment:
CONTAINER_NAME: "<CONTAINER_NAME>"
58 changes: 58 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
plugins {
id 'java'
id 'org.springframework.boot' version '3.2.1'
id 'io.spring.dependency-management' version '1.1.4'
id 'com.diffplug.spotless' version '6.24.0'
}

group = 'com.healthier'

java {
sourceCompatibility = '17'
}

configurations {
compileOnly {
extendsFrom annotationProcessor
}
}

repositories {
mavenCentral()
}

dependencies {
// implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
// implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
// implementation 'org.springframework.boot:spring-boot-starter-data-redis'
// implementation 'org.springframework.boot:spring-boot-starter-security'

//swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0'

implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
runtimeOnly 'com.mysql:mysql-connector-j'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.security:spring-security-test'
}

tasks.named('test') {
useJUnitPlatform()
}

jar {
enabled = false
}

spotless {
java {
target("**/*.java")
googleJavaFormat().aosp()
importOrder()
removeUnusedImports()
trimTrailingWhitespace()
endWithNewline()
}
}
Binary file added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
7 changes: 7 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading

0 comments on commit 3a26af4

Please sign in to comment.