From 09e3185c45eadb1b51537fab7c1deb3befc16bac Mon Sep 17 00:00:00 2001 From: HEE-GEON Date: Sun, 10 Dec 2023 22:32:53 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20feat:=20CI/CD=20=EC=9E=90=EB=8F=99?= =?UTF-8?q?=20=EB=B0=B0=ED=8F=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Giyhub Action으로 Ec2 자동배포를 구성했습니다. --- .github/workflows/gradle.yml | 80 ++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 .github/workflows/gradle.yml diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml new file mode 100644 index 0000000..67b3c66 --- /dev/null +++ b/.github/workflows/gradle.yml @@ -0,0 +1,80 @@ +# 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 Java project with Gradle and cache/restore any dependencies to improve the workflow execution time +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle + +name: Java CI with Gradle + +on: + push: + branches: [ "main" ] + pull_request: + branches: [ "main" ] + +permissions: + contents: read + +jobs: + build: + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + + # yml 주입 + - name: make ymls + run: | + cd ./src/main/resources + + touch ./application-prod.yml + echo "${{ secrets.APPLICATION_PROD }}" > ./application-prod.yml + + touch ./application-redis.yml + echo "${{ secrets.APPLICATION_REDIS }}" > ./application-redis.yml + + touch ./application-mail.yml + echo "${{ secrets.APPLICATION_MAIL }}" > ./application-mail.yml + + touch ./secrets.yml + echo "${{ secrets.SECRETS }}" > ./secrets.yml + + touch ./application-s3.yml + echo "${{ secrets.APPLICATION_S3 }}" > ./application-s3.yml + + + # 권한 부여 + - name: Grant execute permission for gradlew + run: chmod +x gradlew + + # 배포 전 테스트 + - name: Build with Gradle + run: ./gradlew build -x test + + # 도커 허브 푸쉬 + - name: Docekr Build + run: | + docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} + docker build -t ${{ secrets.DOCKER_USERNAME }}/hbc . + docker push ${{ secrets.DOCKER_USERNAME }}/hbc + + # 도커 컨테이너 변경 + - name: Deploy + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.HOST }} + username: ec2-user + key: ${{ secrets.PRIVATE_KEY }} # pem 키 + script: | + sudo docker rm $(docker ps -qa) + sudo docker pull ${{ secrets.DOCKER_USERNAME }}/hbc + docker-compose up -d + sudo docker image prune -f