Merge pull request #36 from Wayble-Project/feature/wonjun #16
This file contains hidden or 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: CI/CD | |
| on: | |
| push: | |
| branches: [ "develop" ] | |
| pull_request: | |
| branches: [ "develop" ] | |
| permissions: | |
| contents: read | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Install JDK 17 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '17' | |
| distribution: 'temurin' | |
| - name: Build with Gradle | |
| run: | | |
| chmod +x ./gradlew | |
| ./gradlew clean bootJar -x test | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v1 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build Docker | |
| run: docker build --platform linux/amd64 -t ${{ secrets.DOCKERHUB_USERNAME }}/live_server . | |
| - name: Push Docker | |
| run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/live_server:latest | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Set target IP | |
| run: | | |
| STATUS=$(curl -o /dev/null -w "%{http_code}" "http://${{ secrets.WAYBLE_SERVER_IP }}/env") | |
| if [ "$STATUS" = "200" ]; then | |
| CURRENT_UPSTREAM=$(curl -s "http://${{ secrets.WAYBLE_SERVER_IP }}/env") | |
| 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 | |
| echo "TARGET_CONTAINER=green" >> $GITHUB_ENV | |
| echo "CURRENT_CONTAINER=blue" >> $GITHUB_ENV | |
| elif [ "$CURRENT_UPSTREAM" = "green" ]; then | |
| echo "CURRENT_PORT=8081" >> $GITHUB_ENV | |
| echo "STOPPED_PORT=8080" >> $GITHUB_ENV | |
| echo "TARGET_UPSTREAM=blue" >> $GITHUB_ENV | |
| echo "TARGET_CONTAINER=blue" >> $GITHUB_ENV | |
| echo "CURRENT_CONTAINER=green" >> $GITHUB_ENV | |
| else | |
| echo "error: invalid CURRENT_UPSTREAM" | |
| exit 1 | |
| fi | |
| - name: Docker compose | |
| uses: appleboy/ssh-action@master | |
| with: | |
| username: ubuntu | |
| host: ${{ secrets.WAYBLE_SERVER_IP }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script_stop: true | |
| script: | | |
| sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/live_server:latest | |
| sudo docker-compose -f docker-compose-${{env.TARGET_UPSTREAM}}.yml up -d | |
| - name: Check deploy server URL | |
| uses: jtalk/url-health-check-action@v3 | |
| with: | |
| url: http://${{ secrets.WAYBLE_SERVER_IP }}:${{env.STOPPED_PORT}}/env | |
| max-attempts: 3 | |
| retry-delay: 10s | |
| - name: Change nginx upstream | |
| uses: appleboy/ssh-action@master | |
| with: | |
| username: ubuntu | |
| host: ${{ secrets.WAYBLE_SERVER_IP }} | |
| 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/service-env.inc && nginx -s reload' | |
| - name: Stop current server | |
| uses: appleboy/ssh-action@master | |
| with: | |
| username: ubuntu | |
| host: ${{ secrets.WAYBLE_SERVER_IP }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script_stop: true | |
| script: | | |
| sudo docker stop ${{env.CURRENT_CONTAINER}} | |
| sudo docker rm ${{env.CURRENT_CONTAINER}} | |
| # name: CD - DEVELOP | |
| # on: #이 워크플로우가 언제 실행될지 트리거를 정의함. | |
| # pull_request: | |
| # types : [closed] #누군가가 Pull request를 닫았을 때 실행됨. | |
| # workflow_dispatch: #수동 실행도 가능하도록 | |
| # jobs: #실제 실행할 작업을 정의 | |
| # build: #작업 이름 | |
| # runs-on: ubuntu-latest #OS환경 | |
| # if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'develop' | |
| # #닫힌 Pull Request 중에서, 병합된 것이고, 병합 대상 브랜치가 develop 브랜치일 경우에만 이 작업을 실행 |