[#noissue] Redis Sercice 관점 분리 #148
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: 'work' | |
on: | |
pull_request: | |
types: | |
- closed | |
jobs: | |
update: | |
name: 프로젝트 버전 업데이트 | |
runs-on: ubuntu-latest | |
if: github.event.pull_request.merged == true && github.base_ref == 'main' | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v2 | |
with: | |
java-version: '17' | |
distribution: 'adopt' | |
- name: Save PR labels to file | |
run: echo '${{ toJson(github.event.pull_request.labels) }}' > pr_labels.json | |
- name: Determine Version Increment | |
id: vars | |
run: | | |
LABELS=$(jq -r '.[].name' pr_labels.json | tr '\n' ' ') | |
echo "Detected Labels: $LABELS" | |
if echo $LABELS | grep -q "major"; then | |
echo "Detected major label" | |
echo "::set-output name=INCREMENT::major" | |
elif echo $LABELS | grep -q "minor"; then | |
echo "Detected minor label" | |
echo "::set-output name=INCREMENT::minor" | |
elif echo $LABELS | grep -q "patch"; then | |
echo "Detected patch label" | |
echo "::set-output name=INCREMENT::patch" | |
else | |
echo "No version label detected" | |
echo "::set-output name=INCREMENT::" | |
fi | |
- name: Grant execute permissions for gradlew | |
run: chmod +x ./gradlew | |
- name: Update Project Version with Gradle | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
if: steps.vars.outputs.INCREMENT | |
run: | | |
./gradlew updateVersion -Pincrement=${{ steps.vars.outputs.INCREMENT }} | |
git config --global user.name 'github-actions[bot]' | |
git config --global user.email 'github-actions[bot]@users.noreply.github.com' | |
git add . | |
git commit -m "chore: update project version based on PR label" | |
git push | |
build: | |
name: 이미지 빌드 및 도커허브 푸시 | |
runs-on: ubuntu-latest | |
if: github.event.pull_request.merged == true && github.base_ref == 'main' | |
needs: update | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
ref: refs/heads/main | |
- name: application-secret.yml 생성 | |
env: | |
ACTIONS_STEP_DEBUG: true | |
APPLICATION_SECRET: ${{ secrets.APPLICATION_SECRET_YML }} | |
run: echo "$APPLICATION_SECRET" > src/main/resources/application-secret.yml | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
cache: 'gradle' | |
- name: gradlew 실행 권한 부여 | |
run: chmod +x gradlew | |
- name: gradle 빌드 | |
run: ./gradlew build --no-daemon | |
- name: 빌드 버전 저장 | |
id: get_version | |
run: echo "::set-output name=VERSION::$(./gradlew properties --no-daemon | grep "version:" | awk '{print $2}' | sed 's/-SNAPSHOT//')" | |
- name: build 폴더를 캐시에 저장 | |
uses: actions/upload-artifact@v3 | |
with: | |
name: build-artifact | |
path: build | |
retention-days: 1 | |
- name: 도커 이미지 빌드 및 푸시 | |
run: | | |
docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} -p ${{ secrets.DOCKER_HUB_PASSWORD }} | |
docker build -t ${{ secrets.DOCKER_REPO }}/withfestival:${{ steps.get_version.outputs.VERSION }} . | |
docker push ${{ secrets.DOCKER_REPO }}/withfestival:${{ steps.get_version.outputs.VERSION }} | |
deploy: | |
name: 원격 서버에 배포 | |
runs-on: ubuntu-latest | |
if: github.event.pull_request.merged == true && github.base_ref == 'main' | |
needs: build | |
steps: | |
- name: 원격 서버에 배포하기 | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.SSH_HOST }} | |
username: ${{ secrets.SSH_USER }} | |
password: ${{ secrets.SSH_PASSWORD }} | |
script: | | |
echo ${{ secrets.DOCKER_HUB_PASSWORD }} | sudo docker login -u ${{ secrets.DOCKER_HUB_USERNAME }} --password-stdin | |
sudo docker stop withfestival_app || true | |
sudo docker rm withfestival_app || true | |
# Get the image ID of the currently running container | |
PREVIOUS_IMAGE_ID=$(sudo docker inspect withfestival_app --format '{{.Image}}') | |
# Search Latest Image Version | |
VERSION=$(curl -s "https://registry.hub.docker.com/v2/repositories/${{ secrets.DOCKER_REPO }}/withfestival/tags/" | \ | |
jq -r '.results[].name' | sort -V | tail -n 1) | |
# Docker Image Run | |
sudo docker pull ${{ secrets.DOCKER_REPO }}/withfestival:${VERSION} | |
if ! sudo docker run --name=withfestival_app --network docker-net --restart unless-stopped \ | |
-p 8080:8080 -e TZ=Asia/Seoul -d ${{ secrets.DOCKER_REPO }}/withfestival:${VERSION}; then | |
echo "Docker run failed, rolling back to previous image..." | |
sudo docker run --name=withfestival_app --network docker-net --restart unless-stopped \ | |
-p 8080:8080 -e TZ=Asia/Seoul -d $PREVIOUS_IMAGE_ID | |
else | |
# Remove old images if docker run succeeds | |
sudo docker image prune -f | |
fi |