Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…rinee/Server into dev

# Conflicts:
#	build.gradle
  • Loading branch information
ss7622 committed Jun 1, 2024
2 parents 11feec0 + 4d5af1c commit 5aaaad1
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 6 deletions.
80 changes: 80 additions & 0 deletions .github/workflows/dev_deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: Java CI/CD with Gradle

# 워크플로우가 실행될 조건을 정의합니다.
on:
push:
branches: [ "dev" ] # dev 브랜치에 push가 발생하면 실행됩니다.
pull_request:
branches: [ "dev" ] # dev 브랜치에 pull request가 생성되면 실행됩니다.

# 워크플로우가 접근할 수 있는 권한을 설정합니다.
permissions:
contents: read

jobs:
# Docker 이미지를 빌드하고 Docker Hub에 푸시하는 작업을 정의합니다.
build-docker-image:
runs-on: ubuntu-latest # 워크플로우가 실행될 환경을 지정합니다.
steps:
# 리포지토리의 코드를 체크아웃합니다.
- uses: actions/checkout@v3

# JDK 17을 설치하고 환경을 설정합니다.
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'

# Gradle Wrapper 파일에 실행 권한을 부여합니다.
- name: Grant Execute Permission For Gradlew
run: chmod +x gradlew

# Gradle을 사용하여 프로젝트를 빌드합니다.
- name: Build With Gradle
run: ./gradlew build -x test

# Docker 이미지를 빌드합니다.
- name: docker image build
run: docker build -t qormoon/fiurinee_docker .

# Docker Hub에 로그인합니다. 로그인 정보는 GitHub Secrets를 통해 안전하게 관리됩니다.
- name: docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }} # Docker Hub 사용자 이름
password: ${{ secrets.DOCKERHUB_PASSWORD }} # Docker Hub 비밀번호

# 빌드된 Docker 이미지를 Docker Hub에 푸시합니다.
- name: docker Hub push
run: docker push qormoon/fiurinee_docker

# 새로 빌드된 Docker 이미지를 EC2 서버에 배포하는 작업을 정의합니다.
deploy:
needs: build-docker-image # build-docker-image 작업이 완료된 후에 실행됩니다.
runs-on: ubuntu-latest # 워크플로우가 실행될 환경을 지정합니다.

steps:
# EC2 서버에 SSH로 접속하여 Docker 이미지를 pull하고 컨테이너를 재시작합니다.
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }} # EC2 서버의 호스트 주소
username: ubuntu # EC2 서버의 사용자 이름
key: ${{ secrets.EC2_SSH_KEY }} # EC2 서버에 접근하기 위한 SSH 개인 키
script: |
# Docker Hub에서 최신 이미지를 pull 합니다.
sudo docker pull qormoon/fiurinee_docker
# 기존 컨테이너를 중지합니다.
sudo docker stop fiurinee || true
# 기존 컨테이너를 제거합니다.
sudo docker rm fiurinee || true
# 새 컨테이너를 실행하면서 환경 변수를 전달합니다.
sudo docker run -d -p 8080:8080 --name fiurinee \
-e DB_URL=jdbc:postgresql://database-fiurinee.czyqweykwib4.ap-northeast-2.rds.amazonaws.com:5432/fiurinee \
-e DB_USERNAME=${{ secrets.DB_USERNAME }} \
-e DB_PASSWORD=${{ secrets.DB_PASSWORD }} \
-e AWS_ACCESSKEY=${{ secrets.AWS_ACCESSKEY }} \
-e AWS_SECRETKEY=${{ secrets.AWS_SECRETKEY }} \
-e AWS_BUCKET=${{ secrets.AWS_BUCKET }} \
qormoon/fiurinee_docker
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:17
ARG JAR_FILE=/build/libs/fiurinee-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
2 changes: 1 addition & 1 deletion src/main/java/com/example/fiurinee/S3Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class S3Controller {
private final AmazonS3 s3Client;
private final String bucketName;

public S3Controller(AmazonS3 s3Client, @Value("${cloud.aws.s3.bucket}") String bucketName) {
public S3Controller(AmazonS3 s3Client, @Value("${AWS_BUCKET}") String bucketName) {
this.s3Client = s3Client;
this.bucketName = bucketName;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/example/fiurinee/StorageConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
@Configuration
public class StorageConfig {

@Value("${cloud.aws.credentials.accessKey}")
@Value("${AWS_ACCESSKEY}")
private String accessKey;

@Value("${cloud.aws.credentials.secretKey}")
@Value("${AWS_SECRETKEY}")
private String accessSecret;
@Value("${cloud.aws.region.static}")
private String region;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/com/example/fiurinee/hello/HelloController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.example.fiurinee.hello;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
@GetMapping("/docker")
@ResponseBody
public String sayHello() {
return "ci/cd 테스트 다시 수정 메롱";
}
}
6 changes: 3 additions & 3 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ logging.level:
cloud:
aws:
credentials:
accessKey: ${cloud.aws.credentials.accessKey}
secretKey: ${cloud.aws.credentials.secretKey}
accessKey: ${AWS_ACCESSKEY}
secretKey: ${AWS_SECRETKEY}
s3:
bucket: ${cloud.aws.s3.bucket}
bucket: ${AWS_BUCKET}
region:
static: ap-northeast-2
stack:
Expand Down

0 comments on commit 5aaaad1

Please sign in to comment.