Feature/140 : 회원가입 응모권 5장 발급 #70
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: Daily Phrase Dev API CI/CD | |
on: | |
pull_request: | |
branches: | |
- develop | |
types: | |
- closed | |
workflow_dispatch: | |
jobs: | |
build-and-deploy: | |
runs-on: ubuntu-latest | |
if: github.event.pull_request.merged == true | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Create application.yml | |
run: | | |
mkdir -p ./src/main/resources/ | |
echo "${{ secrets.APPLICATION_YML }}" > ./src/main/resources/application.yml | |
- name: Create firebase directory | |
run: mkdir -p ./src/main/resources/firebase/ | |
- name: Create JSON | |
uses: jsdaniell/[email protected] | |
with: | |
name: "firebase-adminsdk.json" | |
json: ${{ secrets.SECRET_JSON }} | |
dir: "./src/main/resources/firebase/" | |
- name: Grant execute permission for Gradlew | |
run: chmod +x ./gradlew | |
- name: Build with Gradle | |
run: ./gradlew build -x test -Pprofile=dev | |
- name: Install or Update AWS CLI | |
run: | | |
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip" | |
unzip awscliv2.zip | |
sudo ./aws/install --update | |
- name: Configure AWS CLI | |
run: | | |
aws configure set aws_access_key_id ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws configure set aws_secret_access_key ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws configure set default.region ${{ secrets.AWS_REGION }} | |
- name: Log in to AWS ECR | |
run: | | |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_REPOSITORY_URL }} | |
- name: Build Docker image | |
run: | | |
docker build -t ${{ secrets.AWS_ECR_REPOSITORY_URL }}:${{ github.sha }} . | |
- name: Push Docker image to ECR | |
run: | | |
docker push ${{ secrets.AWS_ECR_REPOSITORY_URL }}:${{ github.sha }} | |
- name: Deploy to EC2 instance | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_HOST }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
port: 22 | |
script: | | |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_REPOSITORY_URL }} | |
# Docker 이미지 pull | |
docker pull ${{ secrets.AWS_ECR_REPOSITORY_URL }}:${{ github.sha }} | |
# 기존 컨테이너 중지 및 제거 | |
docker stop dailyphrase-dev-api || true | |
docker rm dailyphrase-dev-api || true | |
# 새로운 Docker 컨테이너 실행 | |
docker run -d --name dailyphrase-dev-api -p 80:8080 ${{ secrets.AWS_ECR_REPOSITORY_URL }}:${{ github.sha }} |