Automated Deployment With Git Push Using Ansible

git push based deployment using ansible

Deploying applications these days are really a big thing. Especially developers are already working hard for their logic and they cannot find time to login into the server and run the deployment scripts one by one. Mainly developers know their time for deployment, which mean they know the importance and priority of the deployment. So automating and leaving deployment work with developers will really help healthy IT release.

Classic way of push to deploy only with GIT
Classic way of push to deploy only with GIT

Automated Deployment With Git

GIT push based deploy will really make this possible. Let developers use their own git repository tool but by adding extra remote URL on git configuration file, which is to deploy the application.

Like this

[remote "origin"]
	url = [email protected]:company/project.git
	url = [email protected]:project.git
	fetch = +refs/heads/*:refs/remotes/origin/*

[or]

[remote "deploy"]
	url = [email protected]:project.git

This will push the code in two different places at a time. one is for Codebase, another is for deployment.

Here is how

We just have to configure our git server in such a way that whenever git push event is happening, GIT server should run the deployment script remotely or locally.
so we need to configure the git server first.

  • First, we need to install git-core on the git server.
sudo apt-get install git-core
  • Then we need to create git user.
sudo useradd git
passwd git
  • Then adding developer’s workstation public key authorized_keys file to allow developer’s workstation to access the git server from remote.
cat ~/.ssh/id_rsa.pub | ssh [email protected] "mkdir -p ~/.ssh && cat >>  ~/.ssh/authorized_keys"
  • Then need to create an empty repository into git-server
mkdir -p /home/user/project.git
cd /home/swapnil/project-1.git
git init --bare

This will create dummy git repository and you can see the folder called hooks, which is having scripts to perform respective event so by keeping deployment script inside post-receive will execute the post deployment scripts and by keeping pre-deployment script inside pre-receive file will run the pre-deploy script.

READ  Docker Networking Tutorial : Connect Containers Across Hosts Using Weave

so at initial, developer should add the git server’s project URL into his project.

like this :

git remote add deploy [email protected]:project.git
git push deploy master

this will deploy the developer’s code directly.

Seems like a waste of time and unnecessary for developer right? So Ansible makes this very easy.

Push to deploy with Ansible
Push to deploy with Ansible

Just install the role from ansible.galaxy by

ansible-galaxy install PrabhuVignesh.push_to_deploy

Just keep the workstation machine’s public key in a file.

For example

$ cat publickeys.txt
ssh-rsa AAuyFUVFY65tYTFV567YTYTytytYTYTfyuV56FUYTVYTFYTVyTFYUG877686V767R76R67R76R6YTYTfyuVFUYTVYTFYTVyTFYUG877686V767R76R67R76R676V767676V76V768V876V76576V768V876V76Vv76V876VB76v7V76V76V6VBT9LLlNGtLo5pnEXIOPiz9X42ZdxBD721bG5XqDfPnz0JfgAYl6Zw4CXM0F6q0jiAQNJrtrfSNS92x1KMLY8CcVLKOZbpWdUHnouLdKCeM6dBHStpX7yjlb90fRKVZFch87eO0dyAoGWS3oBEYttFYL7s5dm/QV [email protected]
ssh-rsa AAuyFUVFY65tYTFV567YTYTytytYTYTfyuV56FUYTVYTFYTVyTFYUG877686V767R76R67R76R6YTYTfyuVFUYTVYTFYTVyTFYUG877686V767R76R67R76R676V767676V76V768rrtrt54t46Vv76V876VB76v7V76V76V6VBT9LLlNGtLo5pnEXIOPiz9X42ZdxBD721bG5XqDfPnz0JfgAYl6Zb6sevtrrtyyw4CXM0F6q0jiAQNJrtrfSNS92x1KMLY8CcVLKOZbpWdUHnouLdKCeM6dBHStpX7yjlb90fRKVZFch87eO0dyAoGWS3oBEYttFYL7s5dm/QV [email protected]
ssh-rsa AAuyFUVFY65tYTFV567YTYTytytYTYTfyuV56FUYTVYTFYTVyTFYUG877686V767R76R67R76R6YTYTfyuVFUYTVYTFYTVyTFYUG877686V767R76R67R76R63435343r4r3453434r34r34f34356trfhgjhgkjmnhi87t665rg6dvrdxdcser536c5456yvu675678br56er56v5v7y5y5gbr5576un8n78i8it0F6q0jiAQNJrtrfSNS92x1KMLY8CcVLKOZbpWdUHnouLdKCeM6dBHStpX7yjlb90fRKVZFch87eO0dyAoGWS3oBEYttFYL7s5dm/QV [email protected]

Mention the path of this file in path_for_authorized_keys,git_repository_path,post_receive_script,pre_receive_script in the variable as mentioned below.

---
  roles:
    - role: PrabhuVignesh.push_to_deploy
      path_for_authorized_keys: /path/to/public_key/file
      git_repository_path: /home/path/to/your/repo.git
      post_receive_script: "script to deploy the code"
      pre_receive_script: "Prepare storing code"

All the configuration of the git server will be done with this ansible run.

whenever a developer pushes the code to git server, the application specified script like “puppet deploy script”, “cap deploy…” etc.. for post-receive and pre-receive will run from there and the application will be deployed. Make the scripts in such a way that deployment script will run only after the master branch has been pushed. So that unnecessary deployment will be avoided. Example code for that is

#!/bin/bash
do
	if [[ $ref =~ .*/master$ ]];
	then
    	echo "I am master branch push and i will run deployment script"
		# Run deployment script.....
	else
		echo "Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server."
	fi
done

Conclusion

GIT is really an amazing tool for IT automation, especially it connects application developers with IT operations to make things easy.

 

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.

Leave a Reply

Your email address will not be published. Required fields are marked *