Running #137
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: CD | |
run-name: Running | |
on: | |
push: | |
branches: | |
- release/* | |
- hotfix/* | |
env: | |
AWS_REGION: ap-northeast-2 | |
ECR_REPOSITORY: chzz-rep | |
ECS_SERVICE: chzz-market | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: check out | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Extract version from branch name | |
id: extract-version | |
run: | | |
BRANCH_NAME="${{ github.ref }}" # Full ref name, e.g., refs/heads/release/5.5.6 | |
BRANCH_NAME=${BRANCH_NAME#refs/heads/} # Remove 'refs/heads/' prefix to get 'release/5.5.6' | |
# Display the extracted branch name for debugging | |
echo "Extracted branch name: $BRANCH_NAME" | |
# Check if the branch name matches the expected pattern | |
if [[ "$BRANCH_NAME" =~ ^(release|hotfix)/([0-9]+\.[0-9]+\.[0-9]+)$ ]]; then | |
VERSION="${BASH_REMATCH[2]}" | |
echo "VERSION=$VERSION" >> $GITHUB_ENV | |
echo "Extracted version: $VERSION" | |
else | |
echo "❌ Branch name does not match release/x.x.x or hotfix/x.x.x format." | |
exit 1 | |
fi | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'corretto' | |
- name: AWS credential 설정 | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-region: ${{ env.AWS_REGION }} | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
- name: ECR 로그인 | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: S3에서 설정파일 복사 | |
run: | | |
aws s3 cp s3://chzzmarket-production-storage/chzz-production-properties/application-prod.yml \ | |
./src/main/resources/application-prod.yml | |
- name: gradle 실행 권한 부여 | |
run: chmod +x ./gradlew | |
- name: gradle 빌드 | |
run: ./gradlew build -x test | |
- name: Docker 이미지 빌드 및 ECR 배포 | |
id: build-image | |
env: | |
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }} | |
IMAGE_TAG: ${{ env.VERSION }} | |
run: | | |
docker build -t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG . | |
docker push $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG | |
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT | |
- name: EC2로 파일 복사 (compose.yaml 및 nginx.conf 포함) | |
uses: appleboy/scp-action@master | |
with: | |
host: ${{ secrets.EC2_IP }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
source: './compose.yaml,./nginx.conf' | |
target: '/home/ec2-user' | |
- name: EC2에서 Docker Compose를 통한 애플리케이션 실행 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.EC2_IP }} | |
username: ${{ secrets.EC2_USERNAME }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script: | | |
export AWS_REGION=${{ env.AWS_REGION }} | |
export ECR_URI=${{ secrets.ECR_URI }} | |
# .env 파일 생성 및 ECR_IMAGE 변수 추가 | |
touch /home/ec2-user/.env | |
echo ECR_IMAGE=${{ steps.build-image.outputs.image }} >> /home/ec2-user/.env | |
# ECR 레지스트리에 로그인 | |
aws ecr get-login-password --region $AWS_REGION | sudo docker login --username AWS --password-stdin $ECR_URI/chzz-rep | |
# 기존 컨테이너 정지 및 삭제 | |
sudo docker compose down | |
# 최신 이미지를 pull | |
sudo docker compose pull | |
# Docker Compose로 애플리케이션 실행 | |
sudo docker compose up -d |