-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from 9oormthon-univ/feature/GOORM-6-github-act…
…ions [GOORM-6]- 도커 연동
- Loading branch information
Showing
2 changed files
with
72 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,70 @@ | ||
name: Deploy to EC2 | ||
name: CD with Gradle | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
branches: [ "master" ] | ||
pull_request: | ||
branches: [ "master" ] | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
deploy: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Set up SSH | ||
uses: webfactory/[email protected] | ||
with: | ||
ssh-private-key: ${{ secrets.EC2_PEM_KEY }} | ||
|
||
- name: Deploy to EC2 | ||
env: | ||
EC2_USER: ubuntu # EC2 사용자 이름 | ||
EC2_HOST: ${{ secrets.EC2_HOST }} # EC2 호스트 주소 | ||
EC2_PATH: 2024_DANPOONG_TEAM_49_BE # 프로젝트 경로 | ||
DB_NAME: ${{ secrets.DB_NAME }} | ||
DB_USERNAME: ${{ secrets.DB_USERNAME }} | ||
DB_PASSWORD: ${{ secrets.DB_PASSWORD }} | ||
REST_API_KEY: ${{ secrets.REST_API_KEY }} | ||
run: | | ||
ssh -o StrictHostKeyChecking=no $EC2_USER@${{ secrets.EC2_HOST }} << 'EOF' | ||
cd $EC2_PATH | ||
git pull origin master | ||
# application.yml 업데이트 | ||
cat > src/main/resources/application.yml << EOL | ||
spring: | ||
datasource: | ||
url: jdbc:mysql://localhost:3306/${DB_NAME} | ||
username: ${DB_USERNAME} | ||
password: ${DB_PASSWORD} | ||
external: | ||
api-key: ${REST_API_KEY} | ||
EOL | ||
# Gradle 빌드 및 애플리케이션 실행 | ||
./gradlew build -x test | ||
nohup java -jar build/libs/*.jar > app.log 2>&1 & | ||
EOF | ||
build: | ||
runs-on: ubuntu-latest # ubuntu 최신 버전에서 script를 실행 | ||
|
||
steps: | ||
# 지정한 저장소(현재 REPO)에서 코드를 워크플로우 환경으로 가져오도록 하는 github action | ||
- uses: actions/checkout@v3 | ||
|
||
# open jdk 17 버전 환경을 세팅 | ||
- name: Set up JDK 17 | ||
uses: actions/setup-java@v3 | ||
with: | ||
java-version: '17' | ||
distribution: "adopt" | ||
|
||
# Github secrets로부터 데이터를 받아서, 워크 플로우에 파일을 생성 | ||
- name: Make application.properties | ||
run: | | ||
cd ./src/main/resources | ||
touch ./application.properties | ||
echo "${{ secrets.PROPERTIES }}" > ./application.properties | ||
shell: bash | ||
|
||
# gradle을 통해 소스를 빌드. | ||
- name: Build with Gradle | ||
run: | | ||
chmod +x ./gradlew | ||
./gradlew clean build -x test | ||
# dockerfile을 통해 이미지를 빌드하고, 이를 docker repo로 push 합니다. | ||
# 이 때 사용되는 ${{ secrets.DOCKER_REPO }}/directors-dev 가 위에서 만든 도커 repository 입니다. | ||
- name: Docker build & push to docker repo | ||
run: | | ||
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | ||
docker build -f Dockerfile -t ${{ secrets.DOCKER_REPO }}/danpoong . | ||
docker push ${{ secrets.DOCKER_REPO }}/danpoong | ||
# appleboy/ssh-action@master 액션을 사용하여 지정한 서버에 ssh로 접속하고, script를 실행합니다. | ||
# script의 내용은 도커의 기존 프로세스들을 제거하고, docker repo로부터 방금 위에서 push한 내용을 pull 받아 실행하는 것입니다. | ||
# 실행 시, docker-compose를 사용합니다. | ||
- name: Deploy to server | ||
uses: appleboy/ssh-action@master | ||
id: deploy | ||
with: | ||
host: ${{ secrets.EC2_HOST }} | ||
username: ubuntu | ||
key: ${{ secrets.EC2_PEM_KEY }} | ||
envs: GITHUB_SHA | ||
script: | | ||
echo "DB_HOST=${{ secrets.DB_HOST }}" > .env | ||
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> .env | ||
echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env | ||
echo "REST_API_KEY=${{ secrets.REST_API_KEY }}" >> .env | ||
|
||
sudo docker rm -f $(docker ps -qa) | ||
sudo docker pull ${{ secrets.DOCKER_REPO }}/danpoong | ||
docker-compose up -d | ||
docker image prune -f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM openjdk:17 | ||
|
||
ARG JAR_FILE=build/libs/*.jar | ||
|
||
COPY ${JAR_FILE} app.jar | ||
|
||
ENTRYPOINT ["java","-jar","/app.jar"] |