Skip to content

Merge pull request #82 from Kernel360/I-migi-patch-2 #13

Merge pull request #82 from Kernel360/I-migi-patch-2

Merge pull request #82 from Kernel360/I-migi-patch-2 #13

Workflow file for this run

name: Build and Deploy
on:
push:
branches:
- release
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Set environment values badminton-api
run: |
cd ./badminton-api/src/main/resources
touch ./.env
echo "${{ secrets.ENV }}" > ./.env
shell: bash
- name: Set environment values badminton-batch
run: |
cd ./badminton-batch/src/main/resources
touch ./.env
echo "${{ secrets.ENV }}" > ./.env
shell: bash
- name: Set Gradle Wrapper Permissions
run: chmod +x ./gradlew
- name: Build with Gradle
run: ./gradlew assemble
- name: Install Docker Compose
run: |
sudo apt-get update
sudo apt-get install -y docker-compose
- name: Build and push Docker image
run: |
# Log in to Docker Hub
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
# Build the Docker image with the 'latest' tag
docker-compose build
# Push the Docker image to Docker Hub
docker-compose push
- name: Clean up dangling Docker images locally
run: |
# Clean up any dangling images after the build
sudo docker image prune -f
- name: Test SSH connection
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PUBLIC_DNS }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
echo "Testing SSH connection"
uptime
- name: Deploy to EC2 Server
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.PUBLIC_DNS }}
username: ${{ secrets.EC2_USER }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
echo "Docker Hub에 로그인 하기"
echo "${{ secrets.DOCKERHUB_PASSWORD }}" | sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} --password-stdin
echo "모든 실행 중인 컨테이너 중지"
sudo docker stop $(sudo docker ps -aq) # 모든 실행 중인 컨테이너 중지
echo "모든 컨테이너 및 네트워크 삭제"
sudo docker rm $(sudo docker ps -aq) # 모든 컨테이너 삭제
sudo docker network prune -f # 모든 브리지 네트워크 삭제 (주의: 사용 중인 네트워크도 삭제됨)
echo "모든 이미지 삭제"
sudo docker rmi $(sudo docker images -q) # 모든 이미지 삭제
echo "최신 Docker 이미지 Pull"
sudo docker-compose pull # Docker Compose 파일에 정의된 이미지 pull
echo "새로운 컨테이너 실행"
sudo docker-compose up -d # 모든 서비스를 백그라운드에서 실행
echo "서버에서 오래된 Docker 이미지 정리"
sudo docker image prune -f # 불필요한 이미지 정리