Merge pull request #291 from Atractorrr/dev #147
Workflow file for this run
This file contains 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: Deploy To EC2 | |
on: | |
push: | |
branches: | |
- release | |
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.yml 파일 만들기 | |
run: echo '${{ secrets.APPLICATION_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: AWS Resource에 접근할 수 있게 AWS credentials 설정 | |
uses: aws-actions/configure-aws-credentials@v4 | |
with: | |
aws-region: ap-northeast-2 | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
- name: ECR에 로그인하기 | |
id: ecr-login | |
uses: aws-actions/amazon-ecr-login@v2 | |
with: | |
region: ap-northeast-2 | |
- name: Docker 이미지 생성 | |
run: docker build -t attraction-api-server . | |
- name: Docker 이미지에 Tag 붙이기 | |
run: docker tag attraction-api-server ${{ steps.ecr-login.outputs.registry }}/attraction-api-server:latest | |
- name: ECR에 Docker 이미지 Push | |
run: docker push ${{ steps.ecr-login.outputs.registry }}/attraction-api-server:latest | |
- name: 배포 작업 생성 | |
uses: chrnorm/deployment-action@v2 | |
id: deployment | |
with: | |
token: ${{ secrets.GITHUB_TOKEN }} | |
environment-url: https://attraction.run | |
environment: production | |
ref: ${{ github.ref }} | |
- name: SSH로 EC2에 접속하기 | |
uses: appleboy/[email protected] | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_PRIVATE_KEY }} | |
script_stop: true | |
script: | | |
docker stop attraction-api-server || true | |
docker rm attraction-api-server || true | |
docker pull ${{ steps.ecr-login.outputs.registry }}/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 | |
notification: | |
runs-on: ubuntu-latest | |
needs: deploy | |
steps: | |
- name: Check workflow status | |
uses: martialonline/workflow-status@v3 | |
id: check | |
- name: 슬랙으로 메시지 보내기 | |
uses: 8398a7/action-slack@v3 | |
with: | |
status: ${{ steps.check.outputs.status }} | |
author_name: Github Action Bot | |
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took | |
env: | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} | |
if: always() | |
# - name: 배포 상태 업데이트 (성공) | |
# if: success() | |
# uses: chrnorm/deployment-status@releases/v2 | |
# with: | |
# token: '${{ github.token }}' | |
# environment-url: ${{ steps.deployment.outputs.environment_url }} | |
# deployment-id: ${{ steps.deployment.outputs.deployment_id }} | |
# state: 'success' | |
# | |
# - name: 배포 상태 업데이트 (실패) | |
# if: failure() | |
# uses: chrnorm/deployment-status@v2 | |
# with: | |
# token: '${{ github.token }}' | |
# environment-url: ${{ steps.deployment.outputs.environment_url }} | |
# deployment-id: ${{ steps.deployment.outputs.deployment_id }} | |
# state: 'failure' |