Merge pull request #17 from FitTheMan/feature/#15 #7
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
| name: develop push Build and Deploy | |
| on: | |
| push: | |
| branches: [ "develop" ] | |
| env: | |
| DOCKERHUB_USERNAME: fittheman | |
| DOCKERHUB_IMAGE_NAME: fittheman-server | |
| jobs: | |
| build-deploy: | |
| runs-on: ubuntu-latest | |
| environment: DEV | |
| steps: | |
| # 체크아웃 | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| # JDK 17 세팅 | |
| - name: Set up JDK 17 | |
| uses: actions/setup-java@v4 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| # Gradlew 실행 권한 허용 | |
| - name: Grant Execute Permission for Gradlew | |
| run: chmod +x ./gradlew | |
| # .env 파일 생성 | |
| - name: Load secrets into .env file | |
| run: | | |
| echo "${{ secrets.ENV }}" >> .env | |
| # Gradle 빌드 | |
| - name: Build with Gradle | |
| id: gradle | |
| uses: gradle/gradle-build-action@v2 | |
| with: | |
| arguments: | | |
| build | |
| --scan | |
| cache-read-only: ${{ github.ref != 'refs/heads/main' && github.ref != 'refs/heads/develop' }} | |
| # Dockerhub 로그인 | |
| - name: Login to Dockerhub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ env.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }} | |
| # Docker 메타데이터 | |
| - name: Extract Docker metadata | |
| id: metadata | |
| uses: docker/[email protected] | |
| env: | |
| DOCKERHUB_IMAGE_FULL_NAME: ${{ env.DOCKERHUB_USERNAME }}/${{ env.DOCKERHUB_IMAGE_NAME }} | |
| with: | |
| images: ${{ env.DOCKERHUB_IMAGE_FULL_NAME }} | |
| tags: | | |
| type=sha,prefix= | |
| # Docker 이미지 빌드, 도커허브 푸시 | |
| - name: Build and Push Docker image | |
| uses: docker/build-push-action@v3 | |
| with: | |
| context: . | |
| push: true | |
| tags: ${{ steps.metadata.outputs.tags }} # 추출된 도커 메타데이터 tags -> "${DOCKERHUB_USERNAME}/${DOCKERHUB_IMAGE_NAME}:{TAG} | |
| # EC2 서버로 docker-compose.yml 파일 복사 | |
| - name: Copy docker-compose file to EC2 | |
| uses: burnett01/[email protected] | |
| with: | |
| switches: -avzr --delete | |
| path: docker-compose.yml | |
| remote_host: ${{ secrets.EC2_HOST }} | |
| remote_user: ${{ secrets.EC2_USER }} | |
| remote_key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| remote_path: /home/ubuntu/ | |
| # EC2 서버로 nginx 파일 복사 | |
| # docker-compose.yml 에서 nginx 컨테이너 실행 시 파일을 마운트하기 위함 | |
| - name: Copy default.conf file to EC2 | |
| uses: burnett01/[email protected] | |
| with: | |
| switches: -avzr --delete | |
| path: ./nginx | |
| remote_host: ${{ secrets.EC2_HOST }} | |
| remote_user: ${{ secrets.EC2_USER }} | |
| remote_key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| remote_path: /home/ubuntu | |
| # EC2 배포 | |
| - name: Deploy to EC2 Server | |
| uses: appleboy/[email protected] | |
| env: | |
| IMAGE_FULL_PATH: ${{ steps.metadata.outputs.tags }} | |
| DOCKERHUB_IMAGE_NAME: ${{ env.DOCKERHUB_IMAGE_NAME }} | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.SSH_PRIVATE_KEY }} | |
| envs: IMAGE_FULL_PATH, DOCKERHUB_IMAGE_NAME # docker-compose.yml 에서 사용할 환경 변수 | |
| debug: true | |
| script: | | |
| echo "${{ secrets.DOCKERHUB_ACCESS_TOKEN }}" | docker login -u "${{ secrets.DOCKERHUB_USERNAME }}" --password-stdin | |
| docker compose up -d | |
| docker image prune -a -f |