chore: 1차 배포 스크립트 완성 #11
Workflow file for this run
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: cicd | |
| on: | |
| push: | |
| branches: [ "main", "dev", "deploy/cicd" ] | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v3 | |
| - name: Install JDK 21 | |
| uses: actions/setup-java@v3 | |
| with: | |
| java-version: '21' | |
| distribution: 'temurin' | |
| - name: Build with Gradle And Test | |
| run: | | |
| echo ${{ secrets.APPLICATION_YML }} | base64 --decode > ./src/main/resources/application.yml | |
| if [ -s ./src/main/resources/application.yml ]; then | |
| echo "application.yml Create Success" | |
| else | |
| echo "application.yml Create Fail" | |
| exit 1 | |
| fi | |
| chmod +x ./gradlew | |
| ./gradlew clean build | |
| - name: Login to DockerHub | |
| uses: docker/login-action@v1 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_ACCESS_TOKEN }} | |
| - name: Build Docker | |
| run: docker build --platform linux/amd64 -t ${{ secrets.DOCKERHUB_USERNAME }}/saegil . | |
| - name: Push Docker | |
| run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/saegil:latest | |
| deploy: | |
| needs: build | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Get Github Actions VM IP | |
| id: ip | |
| uses: haythem/public-ip@v1.3 | |
| - name: Print VM IP | |
| run: | | |
| echo ${{ steps.ip.outputs.ipv4 }} | |
| - name: Configure AWS credentials | |
| uses: aws-actions/configure-aws-credentials@v4 | |
| with: | |
| aws-access-key-id: ${{ secrets.AWS_IAM_ACCESS_KEY }} | |
| aws-secret-access-key: ${{ secrets.AWS_IAM_ACCESS_SECRET_KEY }} | |
| aws-region: 'ap-northeast-2' | |
| - name: Add GitHub Actions IP | |
| run: | | |
| aws ec2 authorize-security-group-ingress \ | |
| --group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \ | |
| --protocol tcp \ | |
| --port 22 \ | |
| --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |
| - name: Test EC2 Connection And Manipulation | |
| uses: appleboy/ssh-action@master | |
| with: | |
| username: ubuntu | |
| host: ${{ secrets.EC2_ELASTIC_IP }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script_stop: true | |
| script: | | |
| echo "[✔] GitHub Actions Connection Success" | tee -a ~/cicd-log.txt | |
| echo "Time: $(date '+%Y-%m-%d %H:%M:%S')" | tee -a ~/cicd-log.txt | |
| echo "Branch: ${{ github.ref_name }}" | tee -a ~/cicd-log.txt | |
| echo "Commit: ${{ github.sha }}" | tee -a ~/cicd-log.txt | |
| echo "-------------------------------" | tee -a ~/cicd-log.txt | |
| - name: Set target IP | |
| run: | | |
| STATUS=$(curl -o /dev/null -w "%{http_code}" "https://${{ secrets.EC2_HTTPS_DOMAIN }}/env") | |
| echo $STATUS | |
| if [ $STATUS = 200 ]; then | |
| CURRENT_UPSTREAM=$(curl -s "https://${{ secrets.EC2_HTTPS_DOMAIN }}/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 | |
| elif [ $CURRENT_UPSTREAM = green ]; then | |
| echo "CURRENT_PORT=8081" >> $GITHUB_ENV | |
| echo "STOPPED_PORT=8080" >> $GITHUB_ENV | |
| echo "TARGET_UPSTREAM=blue" >> $GITHUB_ENV | |
| else | |
| echo "error" | |
| exit 1 | |
| fi | |
| - name: Docker compose | |
| uses: appleboy/ssh-action@master | |
| with: | |
| username: ubuntu | |
| host: ${{ secrets.EC2_ELASTIC_IP }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script_stop: true | |
| script: | | |
| sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/saegil: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: https://${{ secrets.EC2_HTTPS_DOMAIN }}:${{env.STOPPED_PORT}}/env | |
| max-attempts: 5 | |
| retry-delay: 10s | |
| - name: Change nginx upstream (local nginx) | |
| uses: appleboy/ssh-action@master | |
| with: | |
| username: ubuntu | |
| host: ${{ secrets.EC2_ELASTIC_IP }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script_stop: true | |
| script: | | |
| echo "set \$service_url ${{ env.TARGET_UPSTREAM }};" | sudo tee /etc/nginx/conf.d/service-env.inc | |
| sudo nginx -s reload | |
| - name: Stop current server | |
| uses: appleboy/ssh-action@master | |
| with: | |
| username: ubuntu | |
| host: ${{ secrets.EC2_ELASTIC_IP }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script_stop: true | |
| script: | | |
| sudo docker stop ${{env.CURRENT_UPSTREAM}} | |
| sudo docker rm ${{env.CURRENT_UPSTREAM}} | |
| sudo docker image prune -a -f | |
| - name: Remove GitHub Actions IP | |
| run: | | |
| aws ec2 revoke-security-group-ingress \ | |
| --group-id ${{ secrets.AWS_SECURITY_GROUP_ID }} \ | |
| --protocol tcp \ | |
| --port 22 \ | |
| --cidr ${{ steps.ip.outputs.ipv4 }}/32 | |