Skip to content

Merge pull request #30 from CSID-DGU/develop #71

Merge pull request #30 from CSID-DGU/develop

Merge pull request #30 from CSID-DGU/develop #71

Workflow file for this run

name: akoming CI/CD pipeline
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
env:
DB_URL: ${{ secrets.DB_URL }}
DB_USERNAME: ${{ secrets.DB_USERNAME }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
REDIS_HOST: ${{ secrets.REDIS_HOST }}
REDIS_PORT: ${{ secrets.REDIS_PORT }}
REDIS_PASSWORD: ${{ secrets.REDIS_PASSWORD }}
SMTP_HOST: ${{ secrets.SMTP_HOST }}
SMTP_PORT: ${{ secrets.SMTP_PORT }}
SMTP_USERNAME: ${{ secrets.SMTP_USERNAME }}
SMTP_PASSWORD: ${{ secrets.SMTP_PASSWORD }}
S3_ACCESS_KEY: ${{secrets.S3_ACCESS_KEY}}
S3_SECRET_KEY: ${{secrets.S3_SECRET_KEY}}
steps:
- uses: actions/checkout@v4
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
# Gradle μΊμ‹œ μ„€μ •
- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: gradle-${{ runner.os }}-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
gradle-${{ runner.os }}-
- name: Build with Gradle Wrapper
run: ./gradlew build
# Docker λΉŒλ“œ 및 Docker Hub에 ν‘Έμ‹œ
- name: Docker build & push to Docker Hub
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -t ${{ secrets.DOCKER_USERNAME }}/docker-test:latest .
docker push ${{ secrets.DOCKER_USERNAME }}/docker-test:latest
# EC2에 배포
- name: Docker image pull & deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ubuntu
key: ${{ secrets.EC2_PRIVATE_KEY }}
script: |
# μ΅œμ‹  이미지 Pull
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/docker-test:latest
# κΈ°μ‘΄ μ»¨ν…Œμ΄λ„ˆ 쀑지 및 μ‚­μ œ
if [ "$(sudo docker ps -q -f name=docker-test)" ]; then
sudo docker stop docker-test
sudo docker rm docker-test
fi
# ν™˜κ²½ λ³€μˆ˜ 파일 생성
echo "DB_URL=${{ secrets.DB_URL }}" > ~/.env
echo "DB_USERNAME=${{ secrets.DB_USERNAME }}" >> ~/.env
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> ~/.env
echo "REDIS_HOST=${{ secrets.REDIS_HOST }}" >> ~/.env
echo "REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }}" >> ~/.env
echo "REDIS_PORT=${{ secrets.REDIS_PORT }}" >> ~/.env
echo "SMTP_HOST=${{ secrets.SMTP_HOST }}" >> ~/.env
echo "SMTP_PORT=${{ secrets.SMTP_PORT }}" >> ~/.env
echo "SMTP_USERNAME=${{ secrets.SMTP_USERNAME }}" >> ~/.env
echo "SMTP_PASSWORD=${{ secrets.SMTP_PASSWORD }}" >> ~/.env
echo "S3_ACCESS_KEY=${{ secrets.S3_ACCESS_KEY }}" >> ~/.env
echo "S3_SECRET_KEY=${{ secrets.S3_SECRET_KEY }}" >> ~/.env
# μƒˆ μ»¨ν…Œμ΄λ„ˆ μ‹€ν–‰ 및 ν™˜κ²½ λ³€μˆ˜ 전달
sudo docker run -d --log-driver=syslog --name docker-test -p 8080:8080 \
--env-file ~/.env \
${{ secrets.DOCKER_USERNAME }}/docker-test:latest
# μ‹€ν–‰ 쀑인 μ»¨ν…Œμ΄λ„ˆ 확인
sudo docker ps -a | grep "docker-test"
# μ‚¬μš©ν•˜μ§€ μ•ŠλŠ” 이미지 및 μ»¨ν…Œμ΄λ„ˆ 정리
sudo docker image prune -a -f