From 384765f238595391893e58161a6273c75f5f97ac Mon Sep 17 00:00:00 2001 From: Cheol-Won <76567238+Ryan-Dia@users.noreply.github.com> Date: Wed, 25 Sep 2024 20:46:47 +0900 Subject: [PATCH] =?UTF-8?q?[ATR-611]=20chore:=20=EC=8A=A4=ED=85=8C?= =?UTF-8?q?=EC=9D=B4=EC=A7=95=20=EC=84=9C=EB=B2=84=20ci/cd=20=EC=84=A4?= =?UTF-8?q?=EC=A0=95=20(#295)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/dev.yml | 96 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 .github/workflows/dev.yml diff --git a/.github/workflows/dev.yml b/.github/workflows/dev.yml new file mode 100644 index 00000000..761e304b --- /dev/null +++ b/.github/workflows/dev.yml @@ -0,0 +1,96 @@ +name: Deploy to Staging + +on: + push: + branches: + - dev + +jobs: + Deploy: + runs-on: ubuntu-latest + + permissions: + deployments: write + + steps: + - name: Github Repository 파일 불러오기 + uses: actions/checkout@v4 + + - name: JDK v21 설치 + uses: actions/setup-java@v4 + with: + distribution: temurin + java-version: 21 + + - name: application-dev.yml 파일 만들기 + run: echo '${{ secrets.APPLICATION_DEV_PROPERTIES }}' > ./src/main/resources/application.yml + + - name: application-test.yml 파일 만들기 + run: echo '${{ secrets.APPLICATION_TEST_PROPERTIES }}' > ./src/main/resources/application-test.yml + + - name: credentials.json 파일 만들기 + run: echo '${{ secrets.CREDENTIAL_JSON }}' > ./src/main/resources/credentials.json + + - name: log4j2.xml 파일 만들기 + run: echo '${{ secrets.LOG4J2_XML }}' > ./src/main/resources/log4j2.xml + + - name: 테스트 및 빌드하기 + run: ./gradlew clean build + + - name: Docker hub 로그인 + uses: docker/login-action@v3 + with: + username: ${{secrets.DOCKERHUB_USERNAME}} + password: ${{secrets.DOCKERHUB_TOKEN}} + + - name: build and release to DockerHub + env: + NAME: ${{ secrets.DOCKERHUB_USERNAME }} + REPO: attraction-api-server + run: | + docker build -t $REPO . + docker tag $REPO:latest $NAME/$REPO:latest + docker push $NAME/$REPO:latest + + - name: 배포 작업 생성 + uses: chrnorm/deployment-action@v2 + id: deployment + with: + token: ${{ secrets.GITHUB_TOKEN }} + environment-url: https://attraction.run + environment: dev + ref: ${{ github.ref }} + + - name: SSH로 EC2에 접속하기 + uses: appleboy/ssh-action@v1.0.3 + with: + host: ${{ secrets.DEV_EC2_HOST }} + username: ${{ secrets.DEV_EC2_USERNAME }} + key: ${{ secrets.DEV_EC2_PRIVATE_KEY }} + script_stop: true + script: | + docker stop attraction-api-server || true + docker rm attraction-api-server || true + docker pull ${{ secrets.DOCKERHUB_USERNAME }}/attraction-api-server:latest + + # Redis 컨테이너 실행 + docker stop redis-server || true + docker rm redis-server || true + docker run -d --name redis-server -p 6379:6379 redis:latest + + # Spring Boot 애플리케이션 컨테이너 실행 + docker run -d --name attraction-api-server --link redis-server:redis -e TZ=Asia/Seoul -p 8080:8080 -v /home/ec2-user/workspace/logs:/logs ${{ steps.ecr-login.outputs.registry }}/attraction-api-server:latest + + # filebeat 실행 + docker stop filebeat || true + docker rm filebeat || true + docker run -d --name filebeat -v "$HOME/workspace/logs:/usr/share/filebeat/logs" -v "$HOME/workspace/filebeat/filebeat.yml:/usr/share/filebeat/filebeat.yml" docker.elastic.co/beats/filebeat:8.14.1 + + - name: 슬랙 알림 + uses: 8398a7/action-slack@v3 + with: + status: ${{ job.status }} + fields: repo,message,commit,author,action,eventName,ref,workflow,job,took,pullRequest + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} + if: always() \ No newline at end of file