Deployment To My Server

Deployment

After setting up the build process and storing my images in DigitalOcean Container Registry, the next step was to automate deployment. I decided to use GitHub Actions because it gives me a simple, repeatable workflow that runs every time I push a new version of my application.

The workflow has a few steps which I describe below with code snippets for each of them included.

  • Prepare the server by creating the target directory. If it already exists, remove it and recreate a fresh one so no old files remain that could cause issues.
    
    DEPLOY_DIR=${{ vars.APP_DIR }}
    if [ -d "$DEPLOY_DIR" ]; then
      echo "Folder exists. Cleaning up..."
      sudo find "$DEPLOY_DIR" -mindepth 1 -exec rm -rf {} +
    else
      echo "Directory does not exist. Creating..."
      sudo mkdir -p "$DEPLOY_DIR"
    fi
                                        
  • Copy the required files into the new directory. This includes the Docker Compose file and the Nginx configuration.
    
    rsync -avz -e "ssh -o StrictHostKeyChecking=no" \
    docker-compose.yml init_db.sql scheduledb-backup.sql applyDbBackup.bat nginx certbot \
    ${{ secrets.DROPLET_USER }}${{ secrets.DROPLET_IP }}:${{ vars.APP_DIR }}
                                        
  • Log in to the Docker Container Registry (DOCR) so the deployment workflow can pull the latest images for my services.
    
    echo "${{ secrets.REGISTRY_TOKEN }}" | docker login registry.digitalocean.com -u ${{ secrets.REGISTRY_USER_NAME }} --password-stdin
                                        
  • Run a few Docker commands to start the updated containers and bring the application online.
    
    
    sudo docker compose down --Stops and removes all containers, networks, and resources
    sudo docker compose up -d --Starts all containers defined in docker compose
    sudo docker system prune -f --Removes unused Docker resources
    sudo docker compose ps --Displays the status of containers managed by Docker Compose
    
                                        

An unhandled error has occurred. Reload 🗙