News Intelligent CD - Deploy to AWS EC2 #93
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: News Intelligent CD - Deploy to AWS EC2 | |
| on: | |
| workflow_run: | |
| # CI 성공 시 수행 | |
| workflows: | |
| - "News Intelligent CI" | |
| types: | |
| - completed | |
| jobs: | |
| deploy: | |
| if: ${{github.event.workflow_run.conclusion == 'success' }} | |
| runs-on: ubuntu-22.04 | |
| steps: | |
| - name: Checkout repository at CI commit | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ github.event.workflow_run.head_sha }} | |
| fetch-depth: 1 | |
| - name: List workspace & check compose file | |
| run: | | |
| echo "HEAD SHA: ${{ github.event.workflow_run.head_sha }}" | |
| pwd | |
| ls -al | |
| test -f docker-compose-prod.yml || (echo "compose file not found" && exit 1) | |
| - name: Prepare remote dir | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.EC2_SERVER_IP }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_PRIVATE_KEY }} | |
| script: | | |
| mkdir -p ~/${{ secrets.REPOSITORY_PATH }} | |
| - name: Upload compose file | |
| uses: appleboy/[email protected] | |
| with: | |
| host: ${{ secrets.EC2_SERVER_IP }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_PRIVATE_KEY }} | |
| source: "docker-compose-prod.yml" | |
| target: "~/${{ secrets.REPOSITORY_PATH }}/" | |
| overwrite: true | |
| - name: Deploy to EC2 | |
| uses: appleboy/[email protected] | |
| env: | |
| IMAGE_TAG: ${{ github.event.workflow_run.head_sha }} | |
| with: | |
| host: ${{ secrets.EC2_SERVER_IP }} | |
| username: ${{ secrets.EC2_USERNAME }} | |
| key: ${{ secrets.EC2_PRIVATE_KEY }} | |
| envs: IMAGE_TAG | |
| script: | | |
| set -eo pipefail | |
| cd ~/${{ secrets.REPOSITORY_PATH }} | |
| export IMAGE_TAG=$IMAGE_TAG | |
| docker-compose -f docker-compose-prod.yml pull | |
| docker-compose -f docker-compose-prod.yml up -d --force-recreate --remove-orphans | |
| docker image prune -f |