My Deploy Method Brings Most of the Boys to the Yard

Flat files or no, deploying via FTP is tedious. So stop.

This method requires that you use Grunt and have SSH access to your server. If you don’t already use Grunt, you should start now. If you don’t have SSH access to your server, Site5 has cheap plans with SSH access, and they do good work.

If you stubbornly refuse to make your life easier by using Grunt, Gareth Redfern (Statamic Community Manager) wrote about his good-but-not-as-good-as-my workflow on Statamic Themes.

Why my method is better than the Garethod

  • It’s more efficient.
  • It deploys only your built code (concatenated, minified, compressed, etc.), not every individual bulky file you use in development.

However, it only works if you create all content locally. It could be modified a bit to work with sites whose content changes remotely, but as of now, I don’t need to worry about that, so I don’t. If you have to worry about such things, I suggest you figure it out and write about how your deploy method brings all the boys to the yard, or use the aforementioned Garethod.

You two sheep-boys! Stop that jibber-jabbin’!

  1. Install grunt-build-control and add necessary info to your Gruntfile.

    Here’s how mine looks:

    grunt.initConfig({
      ...
    
      buildcontrol: {
        options: {
          dir: 'dist',
          commit: true,
          push: true,
          message: 'Built %sourceName% from commit %sourceCommit% on branch %sourceBranch%'
        },
        stage: {
          options: {
            remote: 'user@curtisblackwell.com:my_repo_stage.git',
            branch: 'master'
          }
        },
        live: {
          options: {
            remote: 'user@curtisblackwell.com:my_repo_live.git',
            branch: 'master'
          }
        }
      },
    
      ...
    });
    
    grunt.registerTask('deploy', [
      'buildcontrol:stage'
    ]);
    grunt.registerTask('deploy_live', [
      'buildcontrol:live'
    ]);
    
  1. Set up git repos on the remote server by entering these commands:

    # Connect to the remote server.
    $ ssh user@domain.com
    
    # Create the repo directories.
    $ mkdir my_repo_stage.git my_repo_live.git
    
    # Run git init and set up git hooks in each directory.
    $ cd my_repo_stage.git
    $ git init --bare
    $ cat > hooks/post-receive << EOF
    $ #!/bin/sh
    $ GIT_WORK_TREE=/path/to/site git checkout -f
    $ EOF
    $ chmod +x hooks/post-receive
    
    $ cd ../my_repo_live.git
    $ git init --bare
    $ cat > hooks/post-receive << EOF
    $ #!/bin/sh
    $ GIT_WORK_TREE=/path/to/site git checkout -f
    $ EOF
    $ chmod +x hooks/post-receive
    

/path/to/site will probably look like this: /home/USERNAME/public_html

  1. In a separate terminal, enter the following commands:

    $ cd path/to/local/repo
    $ git remote add stage user@domain.com:path/to/my_repo_stage.git
    $ git remote add live user@domain.com:path/to/my_repo_live.git
    
  2. If you want to deploy to stage,

    $ grunt deploy
    

    If you want to deploy to live,

    $ grunt deploy_live
    

Bypass Repeated Password Entry (Optional)

There are already plenty of great tutorials on setting up SSH keys. Site5 has an article called How to generate SSH keys and login to your account with SSH. If it’s not to your liking, try Google.

comments powered by Disqus