Ruby: Improved version of Makefile

This documentation is part of the Advanced usages guide. View the full guide here: Going further with your Ruby deployments.

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Ruby 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 Ruby cloud hosting solution!

Below is an improved Makefile that accommodates multiple rules for common tasks:

  • make dev (or simply make): Start the application in development mode.
  • make deploy: Deploy the application to the remote named stackhero (ideal 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 cases where code has already been deployed, avoiding the "Everything up-to-date" error.

Copy and paste the following content into your new Makefile:

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


# Stackhero for Ruby will execute the "run" rule on your instance.
# This is the command to run on both production and staging platforms.
run:
  rake assets:precompile
  rake db:migrate RAILS_ENV=production
  RAILS_ENV=production bundle exec puma -C config/puma.rb


# Command to run in the development environment
dev:
  RAILS_ENV=development rails server -b 0.0.0.0


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


# Rule "deploy-*" deploys to the instance "stackhero-*".
# For example, run "make deploy-production" to deploy to "stackhero-production",
# or "make deploy-staging" to deploy to "stackhero-staging".
deploy-%:
  @$(MAKE) -s deploy-script DEPLOY_REMOTE=stackhero-$* DEPLOY_BRANCH=main


# Internal deployment rule. Do not modify.
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}