Merge pull request #13 from ShwimPing/feat/ldy/search #19
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: Java CI with Gradle | |
on: | |
push: | |
branches: [ "develop" ] | |
permissions: | |
contents: read | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
env: | |
APPLICATION_PROD: ${{ secrets.APPLICATION_PROD }} | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'corretto' | |
# 설정 파일 생성 | |
- name: application yml setting | |
run: | | |
echo ${{ env.APPLICATION_PROD }} | base64 --decode > ./src/main/resources/application-prod.yml | |
# Gradle 실행권한 부여 | |
- name: Grant execute permission to gradlew | |
run: chmod +x ./gradlew | |
# Spring boot application 빌드 | |
- name: Build with gradle | |
run: ./gradlew clean build | |
# DockerHub에 로그인 | |
- name: Login to DockerHub | |
uses: docker/login-action@v1 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
# Docker 이미지 build 후 DockerHub에 push | |
- name: Build Docker | |
run: docker build --platform linux/amd64 -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:prod . | |
- name: Push Docker | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:prod | |
deploy: | |
needs: build | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set target IP | |
run: | | |
STATUS=$(curl -o /dev/null -w "%{http_code}" "http://${{ secrets.SERVER_DOMAIN }}/global/health-check") | |
echo $STATUS | |
if [ $STATUS = 200 ]; then | |
CURRENT_UPSTREAM=$(curl -s "http://${{ secrets.SERVER_DOMAIN }}/global/health-check") | |
else | |
CURRENT_UPSTREAM=green | |
fi | |
echo CURRENT_UPSTREAM=$CURRENT_UPSTREAM >> $GITHUB_ENV | |
if [ $CURRENT_UPSTREAM = blue ]; then | |
echo "CURRENT_PORT=8080" >> $GITHUB_ENV | |
echo "STOPPED_PORT=8081" >> $GITHUB_ENV | |
echo "TARGET_UPSTREAM=green" >> $GITHUB_ENV | |
else | |
echo "CURRENT_PORT=8081" >> $GITHUB_ENV | |
echo "STOPPED_PORT=8080" >> $GITHUB_ENV | |
echo "TARGET_UPSTREAM=blue" >> $GITHUB_ENV | |
fi | |
- name: Docker compose | |
uses: appleboy/ssh-action@master | |
with: | |
username: root | |
password: ${{ secrets.EC2_SSH_PASSWORD }} | |
host: ${{ secrets.SERVER_DOMAIN }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script_stop: true | |
script: | | |
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:prod | |
sudo docker-compose -f docker-compose.yml up -d ${{ env.TARGET_UPSTREAM }} | |
- name: Check deploy server URL | |
uses: jtalk/url-health-check-action@v4 | |
with: | |
url: http://${{ secrets.LIVE_SERVER_IP }}:${{ env.STOPPED_PORT }}/global/health-check | |
max-attempts: 5 | |
retry-delay: 10s | |
- name: Change nginx upstream | |
uses: appleboy/ssh-action@master | |
with: | |
username: root | |
password: ${{ secrets.EC2_SSH_PASSWORD }} | |
host: ${{ secrets.SERVER_DOMAIN }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script_stop: true | |
script: | | |
sudo docker exec -i nginxserver bash -c 'echo "set \$service_url ${{ env.TARGET_UPSTREAM }};" > /etc/nginx/conf.d/service-env.inc && service nginx reload' | |
- name: Stop current server | |
uses: appleboy/ssh-action@master | |
with: | |
username: root | |
password: ${{ secrets.EC2_SSH_PASSWORD }} | |
host: ${{ secrets.SERVER_DOMAIN }} | |
key: ${{ secrets.EC2_SSH_KEY }} | |
script_stop: true | |
script: | | |
sudo docker stop ${{ env.CURRENT_UPSTREAM }} | |
sudo docker rm ${{ env.CURRENT_UPSTREAM }} |