운영계 CD 설정 #1
This file contains hidden or 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
| # This workflow uses actions that are not certified by GitHub. | |
| # They are provided by a third-party and are governed by | |
| # separate terms of service, privacy policy, and support | |
| # documentation. | |
| # This workflow will build a package using Gradle and then publish it to GitHub packages when a release is created | |
| # For more information see: https://github.com/actions/setup-java/blob/main/docs/advanced-usage.md#Publishing-using-gradle | |
| name: Deploy Play Hive Server to AWS | |
| on: | |
| push: | |
| branches: | |
| - dev | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout Code | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: dev # 명확하게 dev 브랜치 지정 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v3 | |
| with: | |
| distribution: 'temurin' | |
| java-version: '17' | |
| - name: List Files in Current Directory | |
| run: ls -la | |
| - name: Copy docker-compose.yml to EC2 | |
| uses: appleboy/scp-action@v0.1.4 | |
| with: | |
| host: ${{ secrets.AWS_SERVER_IP }} | |
| username: ${{ secrets.AWS_USERNAME }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| source: ./docker-compose.yml | |
| target: ~/play-hive/ | |
| - name: Grant execute permission for gradlew | |
| run: chmod +x ./gradlew | |
| - name: Build Docker Image | |
| run: | | |
| docker build --no-cache -t ${{ secrets.DOCKER_USERNAME }}/play-hive:latest . | |
| - name: Login to Docker Hub | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Push Docker Image to Docker Hub | |
| run: | | |
| docker push ${{ secrets.DOCKER_USERNAME }}/play-hive:latest | |
| deploy: | |
| needs: build-and-push | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Install Java on AWS | |
| uses: appleboy/ssh-action@v0.1.10 | |
| with: | |
| host: ${{ secrets.AWS_SERVER_IP }} | |
| username: ${{ secrets.AWS_USERNAME }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| echo "Checking if Java is installed..." | |
| if ! java -version &> /dev/null; then | |
| echo "Java not found. Installing OpenJDK 17..." | |
| sudo apt update | |
| sudo apt install -y openjdk-17-jdk | |
| else | |
| echo "Java is already installed." | |
| fi | |
| - name: Deploy to AWS and Restart Services with Docker Compose | |
| uses: appleboy/ssh-action@v0.1.10 | |
| with: | |
| host: ${{ secrets.AWS_SERVER_IP }} | |
| username: ${{ secrets.AWS_USERNAME }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| script: | | |
| # 이동할 디렉토리 설정 | |
| DEPLOY_DIR=~/play-hive | |
| echo "Navigating to deployment directory: $DEPLOY_DIR" | |
| cd $DEPLOY_DIR || exit | |
| # 최신 Docker 및 Docker Compose 설치 확인 | |
| echo "Ensuring Docker and Docker Compose are installed..." | |
| # Docker 설치 여부 확인 | |
| if ! docker --version &>/dev/null; then | |
| echo "Docker is not installed. Installing Docker..." | |
| sudo apt-get update | |
| sudo apt-get install -y docker.io | |
| sudo systemctl start docker | |
| sudo systemctl enable docker | |
| else | |
| echo "Docker is already installed." | |
| fi | |
| # Docker Compose 설치 여부 확인 | |
| if ! docker-compose --version &>/dev/null; then | |
| echo "Docker Compose is not installed. Installing Docker Compose..." | |
| sudo apt-get update | |
| sudo apt-get install -y docker-compose | |
| else | |
| echo "Docker Compose is already installed." | |
| fi | |
| # 기존 컨테이너 중지 및 제거 | |
| echo "Stopping and removing existing containers..." | |
| sudo docker-compose down || true | |
| # 최신 이미지를 가져오기 | |
| echo "Pulling the latest Docker image..." | |
| sudo docker-compose pull | |
| # 컨테이너 재실행 | |
| echo "Starting containers with Docker Compose..." | |
| sudo docker-compose up -d | |
| # 사용하지 않는 이미지 제거 | |
| echo "Removing unused Docker images..." | |
| sudo docker image prune -f | |
| echo "Deployment completed successfully!" |