Sunday, 20 March 2016

Prepare Production Git Remote

On your production server, install git with apt-get:
  • sudo apt-get install git
Then create a directory for the remote repository. We will create a bare git repository in the home directory called "appname_production". Feel free to name your remote repository whatever you want (except don't put it in ~/appname because that's where we will deploy the application to):
  • mkdir ~/appname_production
  • cd ~/appname_production
  • git init --bare
Since this is a bare repository, there is no working directory and all of the files that are located in .git in a conventional setup are in the main directory itself.
We need to create post-receive git hook, which is the script that will run when the production server receives a git push. Open the hooks/post-receive file in your editor:
  • vi hooks/post-receive
Copy and paste the following script into the post-receive file:
hooks/post-receive
#!/bin/bash

GIT_DIR=/home/deploy/appname_production
WORK_TREE=/home/deploy/appname
export APPNAME_DATABASE_USER='appname'
export APPNAME_DATABASE_PASSWORD='appname_password'

export RAILS_ENV=production
. ~/.bash_profile

while read oldrev newrev ref
do
    if [[ $ref =~ .*/master$ ]];
    then
        echo "Master ref received.  Deploying master branch to production..."
        mkdir -p $WORK_TREE
        git --work-tree=$WORK_TREE --git-dir=$GIT_DIR checkout -f
        mkdir -p $WORK_TREE/shared/pids $WORK_TREE/shared/sockets $WORK_TREE/shared/log

        # start deploy tasks
        cd $WORK_TREE
        bundle install
        rake db:create
        rake db:migrate
        rake assets:precompile
        sudo restart puma-manager
        sudo service nginx restart
        # end deploy tasks
        echo "Git hooks deploy complete"
    else
        echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
    fi
done
Be sure to update the following highlighted values:
  • GIT_DIR: the directory of bare git repository you created earlier
  • WORK_TREE: the directory to which you want to deploy your application (this should match the location that you specified in the Puma configuration)
  • APPNAME_DATABASE_USER: PostgreSQL username (required for rake tasks)
  • APPNAME_DATABASE_PASSWORD: PostgreSQL password (required for rake tasks)
Next, you should review the commands between the # start deploy tasks and # end deploy tasks comments. These are the commands that will run every time the master branch is pushed to the production git remote (appname_production). If you leave them as is, the server will attempt to do the following for the production environment of your application:
  • Run bundler
  • Create the database
  • Migrate the database
  • Precompile the assets
  • Restart Puma
  • Restart Nginx
If you want to make any changes, or add error checking, feel free to do that here.
Once you are done reviewing the post-receive script, save and exit.
Next, make the script executable:
  • chmod +x hooks/post-receive

Passwordless Sudo

Because the post-receive hook needs to run sudo commands, we will allow the deploy user to use passwordless sudo (substitute your deploy username here if it's different):
  • sudo sh -c 'echo "deploy ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/90-deploy'
This will allow the deploy user to run sudo commands without supplying a password. Note that you will probably want to restrict what the commands that the deploy user can run with superuser privileges. At a minimum, you will want to use SSH key authentication and disable password authentication.

Add Production Git Remote

Now that we have everything set up on the production server, let's add the production git remote to our application's repository.
On your development machine, ensure that you are in your application's directory:
  • cd ~/appname
Then add a new git remote named "production" that points to the bare git repository, appname_production, that you created on your production server. Substitute the username (deploy), server IP address, and remote repository name (appname_production):
  • git remote add production deploy@production_server_public_IP:appname_production
Now your application is ready to be deployed with a git push.

Deploy to Production

With all the preparation you've done, you can now deploy your application to your production server by running the following git command:
  • git push production master
This simply pushes your local master branch to the production remote that you created earlier. When the production remote receives the push, it will execute the post-receive hook script that we set up earlier. If you set everything up correctly, your application should now be available at the public IP address of the your production server.
If you used our example application, you should be able to access http://production_server_IP/tasks in a web browser and see something like this:
Sample Rails App

Conclusion

Any time you make a change to your application, you can run the same git push command to deploy to your production server. This alone should save you a lot of time over the life of your project.
This tutorial only covered the "post-receive" hook, but there are several other types of hooks that can help improve the automation of your deployment process.

No comments:

Post a Comment