Python: Improved version of Makefile

This documentation is part of the Advanced usages guide. You can view the complete guide here: Going further with your Python deployments.

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Python cloud solution that provides a host of benefits, including:

  • Deploy your application in seconds with a simple git push.
  • Use your own domain name and benefit from the automatic configuration of HTTPS certificates for enhanced security.
  • Enjoy peace of mind with automatic backups, one-click updates, and straightforward, transparent, and predictable pricing.
  • Get optimal performance and robust security thanks to a private and dedicated VM.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's Python cloud hosting solution!

Below is an improved Makefile that supports multiple rules:

  • make dev (or simply make): Start the application in development mode.
  • make deploy: Deploy the application to the remote named stackhero. This works well when you have a single Stackhero instance.
  • make deploy-production: Deploy the application to the remote named stackhero-production.
  • make deploy-staging: Deploy the application to the remote named stackhero-staging.

This Makefile is designed to handle situations where the code has not changed by avoiding the "Everything up-to-date" error.

Copy and paste the following content as your new Makefile:

# Rule to execute by default when invoking "make" without an argument
.DEFAULT_GOAL := dev


# Stackhero for Python will execute the "run" rule on your instance.
# This is the command to run on both your production and staging environments.
run:
  ENV=production gunicorn app:app \
    --error-logfile - \
    -b 0.0.0.0:8080


# The command to use in the development environment
dev:
  python app.py


# Rule "deploy" to deploy to the instance "stackhero".
# Suitable if you have only one instance.
deploy:
  @$(MAKE) -s deploy-script DEPLOY_REMOTE=stackhero DEPLOY_BRANCH=main


# The "deploy-*" rules deploy to an instance named "stackhero-*".
# For example, running "make deploy-production" deploys to "stackhero-production",
# or "make deploy-staging" deploys to "stackhero-staging".
deploy-%:
  @$(MAKE) -s deploy-script DEPLOY_REMOTE=stackhero-$* DEPLOY_BRANCH=main


# Internal deployment rule. Do not modify it.
deploy-script:
  @echo "Deploying branch \"${DEPLOY_BRANCH}\" to \"${DEPLOY_REMOTE}\"..."
  @echo

  @if [ -n "$$(git status --porcelain)" ]; then \
    echo "Can't deploy because there are uncommitted changes:"; \
    echo "\e[0m"; \
    git status -s; \
    echo ""; \
    echo "\e[0;31m"; \
    echo "You can use this command to commit the changes:"; \
    echo "git add -A . && git commit -m \"Your message\""; \
    echo "\e[0m"; \
    exit 1; \
  fi

  @git push --dry-run ${DEPLOY_REMOTE} ${DEPLOY_BRANCH} 2>&1 | grep -q -F "Everything up-to-date"; \
  EXIT_CODE=$$?; \
  if [ $$EXIT_CODE -eq 0 ]; then \
    echo -n "Nothing new to deploy... Force deploy (this will create a new commit)? (y/N) "; \
    read answer && \
    case $$answer in \
      y|Y|yes|YES) \
      git commit --allow-empty -m "Force update for deploy purpose to \"${DEPLOY_REMOTE}\"" ; \
      ;; \
    *) \
      echo "Nothing to deploy!"; \
      exit 1; \
      ;; \
    esac \
  fi

  git push ${DEPLOY_REMOTE} ${DEPLOY_BRANCH}