Skip to content

BE CD for Dev

BE CD for Dev #221

Workflow file for this run

name: BE CD for Dev
on:
workflow_dispatch:
push:
branches: [ "develop" ]
paths:
- backend/**
jobs:
build:
timeout-minutes: 3
runs-on: ubuntu-latest
env:
DOCKER_REPOSITORY_NAME: ddangkong/ddangkong-api-dev
defaults:
run:
working-directory: ./backend
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setting application-dev.yml
run: |
echo "${{ secrets.APPLICATION_DEV_YML }}" > ./src/main/resources/application-dev.yml
- name: Setting SSL/TLS Key
run: |
mkdir -p ./src/main/resources/ssl
echo "${{ secrets.SSL_KEY_BASE64 }}" | base64 -d > ./src/main/resources/ssl/keystore.p12
- name: Set up JDK 17
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: '17'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew bootJar -PcreateRestDocs
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_GMAIL }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Build Docker image
run: |
docker compose -f deploy/docker-compose-dev.yml build
docker tag deploy-ddangkong-api-dev:latest $DOCKER_REPOSITORY_NAME:latest
- name: Push Docker image
run: docker push $DOCKER_REPOSITORY_NAME:latest
deploy:
needs: build
timeout-minutes: 2
runs-on: [ self-hosted, linux, ARM64, dev ] # Self hosted runner 사용
env:
DOCKER_REPOSITORY_NAME: ddangkong/ddangkong-api-dev
CONTAINER_NAME: ddangkong-api-dev
steps:
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_GMAIL }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Stop and Remove previous Docker container
run: |
CONTAINER_ID=$(sudo docker ps -aqf name=$CONTAINER_NAME)
if [ -n "${CONTAINER_ID}" ]; then
sudo docker rm -f ${CONTAINER_ID}
echo "Container ${CONTAINER_ID} is stopped and removed."
else
echo "No previous container found with name. Skipping removal."
fi
- name: Remove previous Docker image
run: |
IMAGE_ID=$(sudo docker images --filter=reference=ddangkong/ddangkong-api-dev --format "{{.ID}}")
if [ -n "${IMAGE_ID}" ]; then
sudo docker rmi ${IMAGE_ID}
echo "Image ${IMAGE_ID} is removed."
else
echo "No previous image found with repository name. Skipping removal."
fi
- name: Pull docker image
run: sudo docker pull $DOCKER_REPOSITORY_NAME:latest
- name: Run new Docker container
run: |
docker run -d -p 443:8080 --name $CONTAINER_NAME \
-v /home/ubuntu/app-logs:/logs \
$DOCKER_REPOSITORY_NAME:latest