Skip to content

Ci/179 : 도커 빌드시, 스프링 프로파일 전달 #76

Ci/179 : 도커 빌드시, 스프링 프로파일 전달

Ci/179 : 도커 빌드시, 스프링 프로파일 전달 #76

Workflow file for this run

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: Upload build artifacts
uses: actions/upload-artifact@v3
with:
name: build-artifact
path: build/libs/*.jar
deploy:
runs-on: ubuntu-latest
needs: build-and-deploy
steps:
- name: Download build artifacts
uses: actions/download-artifact@v3
with:
name: build-artifact
path: .
- 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.DEV_AWS_ACCESS_KEY_ID }}
aws configure set aws_secret_access_key ${{ secrets.DEV_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.DEV_AWS_ECR_REPOSITORY_URL }}
- name: Build Docker image
run: |
docker build --build-arg SPRING_PROFILES_ACTIVE=dev -t ${{ secrets.DEV_AWS_ECR_REPOSITORY_URL }}:${{ github.sha }} .
- name: Push Docker image to ECR
run: |
docker push ${{ secrets.DEV_AWS_ECR_REPOSITORY_URL }}:${{ github.sha }}
- name: Deploy to EC2 instance
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.DEV_EC2_HOST }}
username: ${{ secrets.DEV_EC2_USERNAME }}
key: ${{ secrets.DEV_EC2_SSH_KEY }}
port: 22
script: |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.DEV_AWS_ECR_REPOSITORY_URL }}
# Docker 이미지 pull
docker pull ${{ secrets.DEV_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.DEV_AWS_ECR_REPOSITORY_URL }}:${{ github.sha }}