Merge pull request #37 from CarToi/dev #23
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" ] | |
| 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: | | |
| mkdir -p ./src/main/resources | |
| 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: Stop Current Server | |
| uses: appleboy/ssh-action@master | |
| with: | |
| username: ubuntu | |
| host: ${{ secrets.EC2_ELASTIC_IP }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| sudo docker stop ${{ secrets.EC2_WAS }} | |
| sudo docker rm ${{ secrets.EC2_WAS }} | |
| sudo docker image prune -a -f | |
| - name: Docker Compose | |
| uses: appleboy/ssh-action@master | |
| with: | |
| username: ubuntu | |
| host: ${{ secrets.EC2_ELASTIC_IP }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/saegil:latest | |
| sudo docker-compose -f docker-compose.yml up -d | |
| # 얘는 새롭게 뜬 도커 컨테이너 헬스체크 | |
| - name: Check Deploy Server URL | |
| uses: jtalk/url-health-check-action@v3 | |
| with: | |
| url: https://${{ secrets.EC2_HTTPS_DOMAIN }}/env | |
| max-attempts: 5 | |
| retry-delay: 10s | |
| - 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 | |