test: deploy test #9
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
name: Deploy To EC2 | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Github Repository 파일 불러오기 | |
uses: actions/checkout@v4 | |
- name: JDK 17버전 설치 | |
uses: actions/setup-java@v4 | |
with: | |
distribution: temurin | |
java-version: 17 | |
- name: application.yml 파일 만들기 | |
run: echo "${{ secrets.APPLICATION_PROPERTIES }}" > ./src/main/resources/application.yml | |
- name: 테스트 및 빌드하기 | |
run: | | |
chmod +x ./gradlew | |
./gradlew clean build -x test | |
- name: AWS Resource에 접근할 수 있게 AWS credentials 설정 | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-region: ap-northeast-2 | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
- name: ECR에 로그인하기 | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v2 | |
- name: Docker 이미지 생성 | |
run: docker build -t cano-server . | |
- name: Docker 이미지에 Tag 붙이기 | |
run: docker tag cano-server ${{ steps.login-ecr.outputs.registry }}/cano-server:latest | |
- name: ECR에 Docker 이미지 Push하기 | |
run: docker push ${{ steps.login-ecr.outputs.registry }}/cano-server:latest | |
- name: SSH로 EC2에 접속하기 | |
uses: appleboy/ssh-action@v1.0.3 | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_PRIVATE_KEY }} | |
script_stop: true | |
script: | | |
# 네트워크 생성 (이미 존재할 경우 무시) | |
docker network create cano-network || echo "Network already exists" | |
# MySQL 컨테이너 중지 및 삭제 | |
docker stop mysql-container || true | |
docker rm mysql-container || true | |
# MySQL 이미지 풀 | |
docker pull mysql:5.7 | |
# MySQL 컨테이너 실행 | |
docker run -d --name mysql-container \ | |
--network cano-network \ # 네트워크 지정 | |
-e MYSQL_ROOT_PASSWORD=password123 \ | |
-e MYSQL_DATABASE=cano \ | |
-e MYSQL_USER=root \ | |
-e MYSQL_PASSWORD=password123 \ | |
-v mysql-data:/var/lib/mysql \ | |
-p 3306:3306 \ | |
mysql:5.7 | |
# 기존 Spring Boot 컨테이너 중지 및 삭제 | |
docker stop cano-server || true | |
docker rm cano-server || true | |
# Spring Boot 이미지 풀 | |
docker pull ${{ steps.login-ecr.outputs.registry }}/cano-server:latest | |
# Spring Boot 컨테이너 실행 | |
docker run -d --name cano-server \ | |
--network cano-network \ # 네트워크 지정 | |
-p 8080:8080 \ | |
-e SPRING_DATASOURCE_URL=jdbc:mysql://mysql-container:3306/cano \ # MySQL 컨테이너 이름으로 수정 | |
-e SPRING_DATASOURCE_USERNAME=root \ | |
-e SPRING_DATASOURCE_PASSWORD=password123 \ | |
${{ steps.login-ecr.outputs.registry }}/cano-server:latest |