Tag Archives: Deployment

Heroku deploy sub-directory

Going through a couple of Udemy courses, and figured it was nice to keep all the code I wrote while doing them in a single github repository.

Was working nicely until I started the Node with React where part of the course is to deploy the app to Heroku which wants a full repository pushed to a certain git repo it creates for deployment. But in my case the app I want to deploy is of course a sub-directory…

Turns out there’s a git command called subtree one can use here:

# Setup
heroku login
heroku create

git remote add heroku/some-name https://git.heroku.com/{created-heroku-app-id}.git

# Deploy
git subtree push --force --prefix path/to/app heroku/some-name master

The subtree command needs to be run from the top-level directory of the git repository, but one can add a script command to the apps package.json, for example like this:

  "scripts": {
    "deploy": "cd ../../.. && git subtree push --prefix path/to/app heroku/some-name master"

Unfortunately, subtree push doesn’t support --force, but a workaround for that is running it nested like this:

git push heroku/some-name `git subtree split --prefix path/to/app master`:master --force

Which unfortunately doesn’t work on Windows… but you can do it in two steps instead:

git subtree split --prefix path/to/app master
git push heroku/some-name {id-from-previous-command}:master --force

Simple automated website deployment via GitHub

Here is a very simple way to add automatic deployment via GitHub. This assumes

  • You have shell access to your web host
  • Your website is in a repository on GitHub
  • CGI scripts are enabled for your website
  • That git (and optionally composer if you use that) is installed

Initial pull

Log on to your web host and do the initial pull.

$ cd path/to/website
$ git clone https://github.com/<you>/<website>.git .

Deployment script

Put the following script in a file named for example deploy.cgi in your website root.

echo Content-type: text/plain

export PATH=~/bin/:$PATH

git pull
composer.phar install
echo DONE
I’m on Dreamhost and the export line is so the cgi script will use the composer.phar and php symlink located in ~/bin. See this post for more info.

Add execute rights to the script.

$ chmod u+x deploy.cgi

You should now be able to visit http://<your-website>/deploy.cgi in your browser and see output like this:

Already up-to-date.
Loading composer repositories with package information
Installing dependencies from lock file
Nothing to install or update
Generating autoload files

Set up GitHub service hook

Go to https://github.com/<you>/<website>/settings/hooks, click on WebHook URLs and enter the full URL to your script, http://<your-website>/deploy.cgi

Test it out

With this set up you should be able to make a change at your dev machine, commit it and push it to GitHub. The script should then be executed shortly after and your website should be updated automatically. Pretty cool 🙂

This is of course ultra simple, and if this is a critical site you should probably add some security of some sort. A simple version could be to use a cryptic name like a guid for the script and of course not have this script checked in at GitHub. You might also perhaps want to only pull from a certain branch or things like that, but for a small simple site this works pretty well as long as you remember to only push to GitHub when you have a working set of commits 😉