chore: CI-CD 파일 수정 #18
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: MARU EGG CI-CD Workflow | |
on: | |
pull_request: | |
branches: | |
- main | |
jobs: | |
CI: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
# Setup Java JDK | |
- name: Setup Java JDK | |
uses: actions/setup-java@v3 | |
with: | |
java-version: 17 | |
distribution: 'temurin' | |
# Grant execute permission for gradlew | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
# Cache Gradle packages | |
- name: Cache Gradle packages | |
uses: actions/cache@v3 | |
with: | |
path: ~/.gradle/caches | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
# Build with Gradle | |
- name: Build with Gradle | |
run: ./gradlew build | |
# Check if the JAR file exists | |
- name: Check JAR file exists | |
run: ls -al build/libs | |
# Upload JAR file as artifact | |
- name: Upload JAR file | |
uses: actions/upload-artifact@v3 | |
with: | |
name: maru-egg-jar | |
path: build/libs/*.jar | |
CD: | |
runs-on: ubuntu-latest | |
needs: CI | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
# Download JAR file from artifact | |
- name: Download JAR file | |
uses: actions/download-artifact@v3 | |
with: | |
name: maru-egg-jar | |
path: build/libs | |
# Log in to Docker Hub | |
- name: Log in to Docker Hub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKER_USERNAME }} | |
password: ${{ secrets.DOCKER_PASSWORD }} | |
# Metadata action | |
- name: Metadata action | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: ${{ secrets.DOCKER_REPO }} | |
# Build and push Docker image | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v3 | |
with: | |
context: . | |
push: true | |
tags: ${{ steps.meta.outputs.tags }}-${{ github.run_number }} | |
labels: ${{ steps.meta.outputs.labels }} | |
# Deploy to EC2 using SSH | |
- name: Deploy to EC2 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.PUBLIC_DNS }} | |
username: ${{ secrets.USERNAME }} | |
key: ${{ secrets.PEM_KEY }} | |
script: | | |
ssh-keyscan -H ${{ secrets.PUBLIC_DNS }} >> ~/.ssh/known_hosts | |
docker pull ${{ steps.meta.outputs.tags }}-${{ github.run_number }} | |
docker stop maru_egg || true | |
docker rm maru_egg || true | |
docker run --restart always -d -p 80:8080 --name maru_egg ${{ steps.meta.outputs.tags }}-${{ github.run_number }} |