Fixed index.js #4
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
| # Just a display name - what shows in the actions tab in Github | |
| name: Build and Deploy | |
| # The trigger, it says run this workflow whenever someone pushes to the main branch | |
| on: | |
| push: | |
| branches: [ main ] | |
| # A job is a collection of steps that run together on the same machine. | |
| # runs-on: ubuntu-latest, means GitHub will spin up a fresh Ubuntu VM in their cloud to run this job, its clean machine every single time | |
| jobs: | |
| build-and-push: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| # Login to Docker | |
| - name: Log in to Docker Hub | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| # Builds the docker image from the ./client directory and pushes it to Docker Hub | |
| - name: Build and push client | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./client # is equivalent to running "docker build ./client" | |
| push: true | |
| tags: | # multiline string, pushes two tags: the build number and the latest tag | |
| ${{ secrets.DOCKERHUB_USERNAME }}/client:${{ github.run_number }} | |
| ${{ secrets.DOCKERHUB_USERNAME }}/client:latest | |
| # Builds the docker image from the ./server directory and pushes it to Docker Hub | |
| - name: Build and push server | |
| uses: docker/build-push-action@v5 | |
| with: | |
| context: ./server # is equivalent to running "docker build ./server" | |
| push: true | |
| tags: | # multiline string, pushes two tags: the build number and the latest tag | |
| ${{ secrets.DOCKERHUB_USERNAME }}/server:${{ github.run_number }} | |
| ${{ secrets.DOCKERHUB_USERNAME }}/server:latest | |
| # Job 2 - deploy | |
| deploy: | |
| runs-on: ubuntu-latest | |
| needs: build-and-push # This job wont run until the "build-and-push" completes successfully | |
| steps: # Completely seperate VM from the build job, it also starts empty, so we need to checkout the code again to get "docker-compose.yaml" | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| # runs a shell command, uses "sed" command to find and replace the relevant image tags in "docker-compose.yaml" iwth the exact build , so instead of deploying :latest it deploys :47 or whatever the run number is. | |
| - name: Pin image tags in compose file | |
| run: | | |
| sed -i "s|image: ${{ secrets.DOCKERHUB_USERNAME }}/server:.*|image: ${{ secrets.DOCKERHUB_USERNAME }}/server:${{ github.run_number }}|" docker-compose.yaml | |
| sed -i "s|image: ${{ secrets.DOCKERHUB_USERNAME }}/client:.*|image: ${{ secrets.DOCKERHUB_USERNAME }}/client:${{ github.run_number }}|" docker-compose.yaml | |
| # uses "appleboy/scp-action" to handle SCP for us, so the it updates the docker-compose.yaml and database.sql to the EC2 instance, therefore no shell scripting is needed. | |
| - name: Copy compose file to EC2 | |
| uses: appleboy/scp-action@v0.1.7 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| source: docker-compose.yaml, database.sql | |
| target: ~/task-manager/ | |
| # SSH's into the EC2 and runs those 4 commands. | |
| - name: Deploy on EC2 | |
| uses: appleboy/ssh-action@v1.0.3 | |
| with: | |
| host: ${{ secrets.EC2_HOST }} | |
| username: ${{ secrets.EC2_USER }} | |
| key: ${{ secrets.EC2_SSH_KEY }} | |
| script: | # those 4 commands : | |
| cd ~/task-manager | |
| docker compose pull | |
| docker compose up -d | |
| docker compose ps | |
| # docker compose pull fetches the new images that were just pushed | |
| # docker compose up -d restarts the containers with the new images | |
| # docker compose ps prints the status |