Automated Deployment With Git Push 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

[or]

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.

  • Then we need to create git user.

  • Then adding developer’s workstation public key authorized_keys file to allow developer’s workstation to access the git server from remote.
READ  Devops Trends 2015 : How it Becomes Mainstream

  • Then need to create an empty repository into git-server

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.

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

like this :

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

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

For example

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.

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

READ  How To Setup Kubernetes API Access Using Service Account

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

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.