Simple site deploy from Git
A long time ago I used to upload sites to the internet via an FTP client. Then internet became faster and cheaper and I started to edit pages inside FTP client because necessity of uploading files after every edit was very frustrating.
Today all my sites are in Git repositories on GitHub or Bitbucket, so I can easily deploy sites from Git: make changes, commit them, type a command, and a minute later my users see new version on my site.
First you need to set up SSH keys to be able to log in to your server without typing a password.
SSH key creation
Connection alias creation
You can save another few keystrokes by adding an SSH alias which you will use instead of host/login combination. Add this lines to your
Use your own host, login and key which you made on a previous step.
SSH key uploading
Upload SSH key to your hosting:
Now you can open SSH connection with your hosting by typing just that:
I use Grunt to build my projects. If you don’t you can skip this section.
There are two ways to build and deploy sites:
- Commit built files and deploy with simple
- Commit only source code and build on deploy.
I usually use the latter. It makes repository more tidy and diffs cleaner. But one have to setup site building on a server. However it’s not a problem with Grunt.
I have a
deploy task in my Gruntfiles which does everything except image optimization, testing and other long running processes that aren’t necessary to build a site.
I always install Grunt plugins with an npm’s
--save-dev switch to make npm save package names and their versions to
package.json. It should look like this:
Thus I can be sure that build willn’t break if a wrong version of some plugin is installed on a server.
Link our local Git repository with a remote one and push it:
Clone repository onto a hosting
It shouldn’t ask you any passwords.
Use this one-liner to install shipit:
My typical deploy script looks like this:
- Switch to
masterbranch (just in case).
- Get recent changes from GitHub/Bitbucket.
- Install/update npm packages.
- Build site using Grunt.
In the latter step I run
deploy task in a locally installed (
npm install without
-g switch) Grunt. In that case all deploy dependencies can be installed with a single command:
npm install and don’t require
Make a shipit script:
And save it as
.shipit in a project root folder.
Shipit connects to a server via SSH, goes to a project folder and runs all commands contained after
[deploy] line. (In fact it’s much more flexible.)
That’s all, now deploy is as easy as this: