[#143] fix: 데이터베이스 레플리케이션 prod 환경에서만 적용 (#144) #78
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: develop(cicd) | |
on: | |
push: | |
branches: | |
- develop | |
jobs: | |
ci: | |
runs-on: ubuntu-latest | |
environment: DEVELOP | |
strategy: | |
matrix: | |
java-version: [ 17 ] | |
outputs: | |
VERSION: ${{ steps.get_version.outputs.VERSION }} | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set up Java | |
uses: actions/setup-java@v3 | |
with: | |
java-version: ${{ matrix.java-version }} | |
distribution: 'adopt' | |
- name: Cache SonarCloud packages | |
uses: actions/cache@v3 | |
with: | |
path: ~/.sonar/cache | |
key: ${{ runner.os }}-sonar | |
restore-keys: ${{ runner.os }}-sonar | |
- name: Cache Gradle packages | |
uses: actions/cache@v3 | |
with: | |
path: ~/.gradle/caches | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }} | |
restore-keys: ${{ runner.os }}-gradle | |
- name: Grant execute permission for gradlew | |
run: chmod +x ./gradlew | |
- name: spotless check | |
run: ./gradlew spotlessCheck --no-daemon | |
- name: Start containers | |
run: docker compose -f docker-compose-test.yml up -d | |
- name: test and analyze | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} | |
run: ./gradlew test sonar --info --stacktrace --no-daemon | |
- name: build | |
run: ./gradlew clean build -x test | |
- name: get project version | |
id: get_version | |
run: echo "::set-output name=VERSION::${{ github.sha }}" | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_KEY }} | |
aws-region: ${{ secrets.AWS_REGION }} | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Build And Push | |
uses: docker/build-push-action@v3 | |
with: | |
context: ./TodaysFail-Interface | |
push: true | |
tags: ${{secrets.AWS_ECR_REGISTRY_ID}}.dkr.ecr.${{secrets.AWS_REGION}}.amazonaws.com/${{secrets.AWS_ECR_REGISTRY_NAME}}:${{ steps.get_version.outputs.VERSION }} | |
cd: | |
runs-on: ubuntu-latest | |
needs: ci | |
environment: DEVELOP | |
steps: | |
- name: checkout | |
uses: actions/checkout@master | |
- name: create remote directory | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.HOST }} | |
username: root | |
key: ${{ secrets.KEY }} | |
port: ${{ secrets.PORT }} | |
script: mkdir -p ~/TodaysFail-Backend | |
- name: copy source via ssh key | |
uses: burnett01/[email protected] | |
with: | |
switches: -avzr | |
remote_path: ~/TodaysFail-Backend | |
remote_host: ${{ secrets.HOST }} | |
remote_port: ${{ secrets.PORT }} | |
remote_user: root | |
remote_key: ${{ secrets.KEY }} | |
- name: connect ssh | |
uses: appleboy/ssh-action@master | |
with: | |
host: ${{ secrets.HOST }} | |
username: root | |
key: ${{ secrets.KEY }} | |
port: ${{ secrets.PORT }} | |
script: | | |
aws ecr get-login-password --region ${{ secrets.AWS_REGION }} | docker login --username AWS --password-stdin ${{ secrets.AWS_ECR_REGISTRY_ID }}.dkr.ecr.${{ secrets.AWS_REGION }}.amazonaws.com | |
cd ~/TodaysFail-Backend | |
VERSION=${{ needs.ci.outputs.VERSION }} | |
sed -i "s/VERSION=.*/VERSION=$VERSION/" .env | |
EXIST_BLUE=$(docker-compose -f ~/TodaysFail-Backend/docker-compose-blue.yml ps | grep todaysfail-blue | awk '{print $1}') | |
if [ -z "${EXIST_BLUE}" ] | |
then | |
# GREEN이 실행중인 경우 | |
START_CONTAINER=blue | |
START_PORT=8080 | |
STOP_CONTAINER=green | |
STOP_PORT=8081 | |
sed -i "s/API_SERVER_PORT=.*/API_SERVER_PORT=8080/" .env | |
else | |
# BLUE가 실행중인 경우 | |
START_CONTAINER=green | |
START_PORT=8081 | |
STOP_CONTAINER=blue | |
STOP_PORT=8080 | |
sed -i "s/API_SERVER_PORT=.*/API_SERVER_PORT=8081/" .env | |
fi | |
docker-compose -f ~/TodaysFail-Backend/docker-compose-$START_CONTAINER.yml pull | |
docker-compose -f ~/TodaysFail-Backend/docker-compose-$START_CONTAINER.yml up --build -d | |
docker system prune --all -f | |
for cnt in {1..10} | |
do | |
DEPLOY_STATUS=$(curl -s http://localhost:$START_PORT/api/v1/examples/health | grep \"status\":200) | |
if [ -z "${DEPLOY_STATUS}" ] | |
then | |
echo "Deploying..." | |
else | |
break | |
fi | |
sleep 5 | |
done | |
if [ $cnt -eq 10 ] | |
then | |
echo "Deploy Failed" | |
exit 1 | |
fi | |
sed -i "s/$STOP_PORT/$START_PORT/" ~/../etc/nginx/conf.d/service_url.inc | |
service nginx reload | |
docker-compose -f ~/TodaysFail-Backend/docker-compose-$STOP_CONTAINER.yml down | |
- name: Notify on success | |
if: ${{ success() }} | |
uses: rtCamp/action-slack-notify@v2 | |
env: | |
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }} | |
SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }} | |
SLACK_USERNAME: '오늘의 실패' | |
SLACK_ICON: https://avatars.slack-edge.com/2023-07-26/5653063434977_51d6e36ad6977fe04337_192.png | |
SLACK_COLOR: '#008000' | |
SLACK_MESSAGE: | | |
*[DEV] 배포 성공 🎉* | |
*배포 버전* | |
${{ needs.ci.outputs.VERSION }} | |
*배포 내역* | |
${{ join(github.event.commits.*.message, '\n') }} | |
*배포 내역 보기* | |
https://github.com/TodaysFail/TodaysFail-Backend/commit/${{ needs.ci.outputs.VERSION }} | |
- name: Notify on failure | |
if: ${{ failure() }} | |
uses: rtCamp/action-slack-notify@v2 | |
env: | |
SLACK_WEBHOOK: ${{ secrets.SLACK_WEBHOOK_URL }} | |
SLACK_CHANNEL: ${{ secrets.SLACK_CHANNEL }} | |
SLACK_USERNAME: '오늘의 실패' | |
SLACK_ICON: https://avatars.slack-edge.com/2023-07-26/5653063434977_51d6e36ad6977fe04337_192.png | |
SLACK_COLOR: '#FF0000' | |
SLACK_MESSAGE: | | |
*[DEV] 배포 실패 😥* | |
*배포 버전* | |
${{ needs.ci.outputs.VERSION }} | |
*배포 내역* | |
${{ join(github.event.commits.*.message, '\n') }} | |
*배포 내역 보기* | |
https://github.com/TodaysFail/TodaysFail-Backend/commit/${{ needs.ci.outputs.VERSION }} |