Chore: docker comopse #14
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: DuoBloom 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 }} | |
SMTP_HOST: ${{ secrets.SMTP_HOST }} | |
SMTP_PORT: ${{ secrets.SMTP_PORT }} | |
SMTP_USERNAME: ${{ secrets.SMTP_USERNAME }} | |
SMTP_PASSWORD: ${{ secrets.SMTP_PASSWORD }} | |
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 }}/akoming-test:latest . | |
docker push ${{ secrets.DOCKER_USERNAME }}/akoming-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 }}/akoming-test:latest | |
# κΈ°μ‘΄ 컨ν μ΄λ μ€μ§ λ° μμ | |
if [ "$(sudo docker ps -a -q -f name=akoming-test)" ]; then | |
echo "Stopping and removing existing akoming-test container..." | |
sudo docker stop akoming-test || true | |
sudo docker rm akoming-test || true | |
else | |
echo "No existing akoming-test container found." | |
fi | |
# μ΅μ μ΄λ―Έμ§ Pull | |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/akoming-test:latest | |
# μ 컨ν μ΄λ μ€ν λ° νκ²½ λ³μ μ λ¬ | |
sudo docker run -d --log-driver=syslog --name akoming-test -p 8080:8080 \ | |
--env-file ~/.env \ | |
${{ secrets.DOCKER_USERNAME }}/akoming-test:latest | |
# μ€ν μ€μΈ 컨ν μ΄λ νμΈ | |
sudo docker ps -a | grep "akoming-test" | |
# μ¬μ©νμ§ μλ μ΄λ―Έμ§ λ° μ»¨ν μ΄λ μ 리 | |
sudo docker image prune -a -f | |