Skip to content

Merge pull request #287 from kookmin-sw/feature/284 #95

Merge pull request #287 from kookmin-sw/feature/284

Merge pull request #287 from kookmin-sw/feature/284 #95

Workflow file for this run

name: Build And Deploy for develop branch to test server
on:
push:
branches: [ "develop" ]
permissions:
contents: read
jobs:
server-build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: "adopt"
- name: Make folder resources
run: |
cd ./server/src/main
mkdir resources
cd resources
- name: Make application.properties
run: |
cd ./server/src/main/resources
touch ./application.properties
echo "${{ secrets.PROPERTIES_DEVELOP }}" > ./application.properties
- name: Build with gradle
run: |
cd server
chmod +x ./gradlew
./gradlew clean build -x test
- name: Docker build & push to docker repo
run: |
cd server
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile -t ${{ secrets.DOCKER_REPO }}:latest .
docker push ${{ secrets.DOCKER_REPO }}:latest
frontend-build-and-push:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Check node version
run: node -v
- name: Change frontend .env
run: |
rm -f ./frontend/.env
touch ./frontend/.env
echo "${{ secrets.ENV_DEVELOP }}" >> ./frontend/.env
- name: Yarn install dependencies
run: |
cd ./frontend
yarn install --frozen-lockfile
- name: Yarn build
run: |
cd ./frontend
yarn build
- name: Frontend docker build & push to docker repo
run: |
cd frontend
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -f Dockerfile -t ${{ secrets.DOCKER_FRONTEND_REPO }}:latest .
docker push ${{ secrets.DOCKER_FRONTEND_REPO }}:latest
pull-and-deploy:
needs:
- server-build-and-push
- frontend-build-and-push
runs-on: ubuntu-latest
steps:
- name: Set LocalTime Asia/Seoul
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.TEST_EC2_PUBLIC_IP }}
username: ubuntu
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
sudo timedatectl set-timezone Asia/Seoul
- name: Install docker
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.TEST_EC2_PUBLIC_IP }}
username: ubuntu
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
if ! command -v docker &> /dev/null; then
sudo apt-get update
sudo apt-get install -y \
ca-certificates \
curl \
gnupg
sudo mkdir -m 0755 -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo docker run hello-world
fi
- name: Install docker-compose
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.TEST_EC2_PUBLIC_IP }}
username: ubuntu
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
if ! command -v docker-compose &> /dev/null; then
sudo curl -L "https://github.com/docker/compose/releases/download/v2.15.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
else
echo "Docker Compose already installed via curl."
fi
- name: Remove & Change docker-compose file
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.TEST_EC2_PUBLIC_IP }}
username: ubuntu
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
if [ -f docker-compose.yml ]; then
sudo rm docker-compose.yml
fi
curl -o docker-compose.yml -L https://raw.githubusercontent.com/kookmin-sw/capstone-2024-14/develop/server/docker-compose.yml
cat docker-compose.yml
- name: Remove & Change server docker image
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.TEST_EC2_PUBLIC_IP }}
username: ubuntu
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
if [ -n "$(docker ps -qa)" ]; then
echo "Removing running containers..."
sudo docker rm -f $(docker ps -qa)
while sudo docker image prune -f 2>&1 | grep -q "a prune operation is already running"; do
echo "Waiting for ongoing prune operation to complete..."
sleep 10
done
fi
sudo docker pull ${{ secrets.DOCKER_REPO }}:latest
sudo docker pull ${{ secrets.DOCKER_FRONTEND_REPO }}:latest
sudo docker image prune -f
- name: Execute docker-compose
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.TEST_EC2_PUBLIC_IP }}
username: ubuntu
key: ${{ secrets.TEST_SSH_PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
sudo docker-compose up -d