Create .env #24
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 Deploy to EC2 | |
| on: | |
| push: | |
| branches: | |
| - "main" | |
| jobs: | |
| # Build Frontend Image only when frontend changes | |
| build-frontend: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event_name == 'push' }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Detect changes in frontend | |
| id: frontend-changes | |
| uses: tj-actions/changed-files@v45 | |
| with: | |
| files: | | |
| frontend/** | |
| - name: Login to DockerHub | |
| if: steps.frontend-changes.outputs.any_changed == 'true' | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Build and push frontend image | |
| if: steps.frontend-changes.outputs.any_changed == 'true' | |
| run: | | |
| docker build -t ${{ secrets.DOCKER_USERNAME }}/frontend:latest ./frontend | |
| docker push ${{ secrets.DOCKER_USERNAME }}/frontend:latest | |
| # Build Backend Image only when backend changes | |
| build-backend: | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event_name == 'push' }} | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Detect changes in backend | |
| id: backend-changes | |
| uses: tj-actions/changed-files@v45 | |
| with: | |
| files: | | |
| backend/** | |
| - name: Login to DockerHub | |
| if: steps.backend-changes.outputs.any_changed == 'true' | |
| uses: docker/login-action@v2 | |
| with: | |
| username: ${{ secrets.DOCKER_USERNAME }} | |
| password: ${{ secrets.DOCKER_PASSWORD }} | |
| - name: Build and push backend image | |
| if: steps.backend-changes.outputs.any_changed == 'true' | |
| run: | | |
| docker build -t ${{ secrets.DOCKER_USERNAME }}/backend:latest ./backend | |
| docker push ${{ secrets.DOCKER_USERNAME }}/backend:latest | |
| # Deploy if frontend OR backend OR docker-compose.yml changes | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: [build-frontend, build-backend] | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v3 | |
| - name: Detect changes | |
| id: detect-changes | |
| uses: tj-actions/changed-files@v45 | |
| with: | |
| files: | | |
| frontend/** | |
| backend/** | |
| docker-compose.yml | |
| - name: Create .env on EC2 | |
| if: steps.detect-changes.outputs.any_changed == 'true' | |
| uses: appleboy/ssh-action@v0.1.10 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| mkdir -p ~/app | |
| echo "DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }}" > ~/app/.env | |
| - name: Copy docker-compose.yml to EC2 | |
| if: steps.detect-changes.outputs.any_changed == 'true' | |
| uses: appleboy/scp-action@v0.1.7 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| source: "docker-compose.yml" | |
| target: "~/app/" | |
| - name: Deploy on EC2 | |
| if: steps.detect-changes.outputs.any_changed == 'true' | |
| uses: appleboy/ssh-action@v0.1.10 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | | |
| cd ~/app | |
| sudo docker-compose pull | |
| sudo docker-compose up -d | |
| sudo docker image prune -f | |