{"id":699,"date":"2016-07-30T08:20:18","date_gmt":"2016-07-30T08:20:18","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=699"},"modified":"2016-07-30T08:20:18","modified_gmt":"2016-07-30T08:20:18","slug":"automated-deployment-git-push-using-ansible","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=699","title":{"rendered":"Automated Deployment With Git Push Using Ansible"},"content":{"rendered":"<p>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.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/git-push-to-deploy-1.png\" class=\"kg-image\" alt=\"Classic way of push to deploy only with GIT\" loading=\"lazy\" width=\"1101\" height=\"661\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/git-push-to-deploy-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/git-push-to-deploy-1.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/git-push-to-deploy-1.png 1101w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Classic way of push to deploy only with GIT<\/span><\/figcaption><\/figure>\n<h2 id=\"automated-deployment-with-git\">Automated Deployment With Git<\/h2>\n<p>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.<\/p>\n<p>Like this<\/p>\n<pre><code>[remote \"origin\"]\n\turl = git@github.com:company\/project.git\n\turl = git@remote-server:project.git\n\tfetch = +refs\/heads\/*:refs\/remotes\/origin\/*<\/code><\/pre>\n<p>[or]<\/p>\n<pre><code>[remote \"deploy\"]\n\turl = git@remote-server:project.git<\/code><\/pre>\n<p>This will push the code in two different places at a time. one is for Codebase, another is for deployment.<\/p>\n<h3 id=\"here-is-how\">Here is how<\/h3>\n<p>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.<br \/>so we need to configure the git server first.<\/p>\n<ul>\n<li>First, we need to install git-core on the git server.<\/li>\n<\/ul>\n<pre><code>sudo apt-get install git-core<\/code><\/pre>\n<ul>\n<li>Then we need to create git user.<\/li>\n<\/ul>\n<pre><code>sudo useradd git passwd git<\/code><\/pre>\n<ul>\n<li>Then adding developer&#8217;s workstation public key authorized_keys file to allow developer&#8217;s workstation to access the git server from remote.<\/li>\n<\/ul>\n<pre><code>cat ~\/.ssh\/id_rsa.pub | ssh git@remote-server \"mkdir -p ~\/.ssh &amp;&amp; cat &gt;&gt;  ~\/.ssh\/authorized_keys\"<\/code><\/pre>\n<ul>\n<li>Then need to create an empty repository into git-server<\/li>\n<\/ul>\n<pre><code>mkdir -p \/home\/user\/project.git\ncd \/home\/swapnil\/project-1.git\ngit init --bare<\/code><\/pre>\n<p>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.<\/p>\n<p>so at initial, developer should add the git server&#8217;s project URL into his project.<\/p>\n<p><strong>like this :<\/strong><\/p>\n<pre><code>git remote add deploy git@remote-server:project.git\ngit push deploy master<\/code><\/pre>\n<p>this will deploy the developer&#8217;s code directly.<\/p>\n<p>Seems like a waste of time and unnecessary for developer right? So Ansible makes this very easy.<\/p>\n<figure class=\"kg-card kg-image-card kg-card-hascaption\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/git-push-to-deploy2-1.png\" class=\"kg-image\" alt=\"Push to deploy with Ansible \" loading=\"lazy\" width=\"1189\" height=\"707\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/git-push-to-deploy2-1.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w1000\/2025\/03\/git-push-to-deploy2-1.png 1000w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/git-push-to-deploy2-1.png 1189w\" sizes=\"auto, (min-width: 720px) 720px\"><figcaption><span style=\"white-space: pre-wrap;\">Push to deploy with Ansible<\/span><\/figcaption><\/figure>\n<p>Just install the role from ansible.galaxy by<\/p>\n<pre><code>ansible-galaxy install PrabhuVignesh.push_to_deploy<\/code><\/pre>\n<p>Just keep the workstation machine&#8217;s public key in a file.<\/p>\n<p>For example<\/p>\n<pre><code>$ cat publickeys.txt\nssh-rsa AAuyFUVFY65tYTFV567YTYTytytYTYTfyuV56FUYTVYTFYTVyTFYUG877686V767R76R67R76R6YTYTfyuVFUYTVYTFYTVyTFYUG877686V767R76R67R76R676V767676V76V768V876V76576V768V876V76Vv76V876VB76v7V76V76V6VBT9LLlNGtLo5pnEXIOPiz9X42ZdxBD721bG5XqDfPnz0JfgAYl6Zw4CXM0F6q0jiAQNJrtrfSNS92x1KMLY8CcVLKOZbpWdUHnouLdKCeM6dBHStpX7yjlb90fRKVZFch87eO0dyAoGWS3oBEYttFYL7s5dm\/QV \ndeveloper1@sample.com\nssh-rsa AAuyFUVFY65tYTFV567YTYTytytYTYTfyuV56FUYTVYTFYTVyTFYUG877686V767R76R67R76R6YTYTfyuVFUYTVYTFYTVyTFYUG877686V767R76R67R76R676V767676V76V768rrtrt54t46Vv76V876VB76v7V76V76V6VBT9LLlNGtLo5pnEXIOPiz9X42ZdxBD721bG5XqDfPnz0JfgAYl6Zb6sevtrrtyyw4CXM0F6q0jiAQNJrtrfSNS92x1KMLY8CcVLKOZbpWdUHnouLdKCeM6dBHStpX7yjlb90fRKVZFch87eO0dyAoGWS3oBEYttFYL7s5dm\/QV \ndeveloper1@sample.com\nssh-rsa AAuyFUVFY65tYTFV567YTYTytytYTYTfyuV56FUYTVYTFYTVyTFYUG877686V767R76R67R76R6YTYTfyuVFUYTVYTFYTVyTFYUG877686V767R76R67R76R63435343r4r3453434r34r34f34356trfhgjhgkjmnhi87t665rg6dvrdxdcser536c5456yvu675678br56er56v5v7y5y5gbr5576un8n78i8it0F6q0jiAQNJrtrfSNS92x1KMLY8CcVLKOZbpWdUHnouLdKCeM6dBHStpX7yjlb90fRKVZFch87eO0dyAoGWS3oBEYttFYL7s5dm\/QV \ndeveloper1@sample.com<\/code><\/pre>\n<p>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.<\/p>\n<pre><code>---\n  roles:\n    - role: PrabhuVignesh.push_to_deploy\n      path_for_authorized_keys: \/path\/to\/public_key\/file\n      git_repository_path: \/home\/path\/to\/your\/repo.git\n      post_receive_script: \"script to deploy the code\"\n      pre_receive_script: \"Prepare storing code\"<\/code><\/pre>\n<p>All the configuration of the git server will be done with this ansible run.<\/p>\n<p>whenever a developer pushes the code to git server, the application specified script like &#8220;puppet deploy script&#8221;, &#8220;cap deploy&#8230;&#8221; 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<\/p>\n<pre><code>#!\/bin\/bash\ndo\n\tif [[ $ref =~ .*\/master$ ]];\n\tthen\n    \techo \"I am master branch push and i will run deployment script\"\n\t\t# Run deployment script.....\n\telse\n\t\techo \"Ref $ref successfully received.  Doing nothing: only the master branch may be deployed on this server.\"\n\tfi\ndone<\/code><\/pre>\n<h3 id=\"conclusion\">Conclusion<\/h3>\n<p>GIT is really an amazing tool for IT automation, especially it connects application developers with IT operations to make things easy.<\/p>\n<p>If you are learning Ansible, checkout the <a href=\"https:\/\/devopscube.com\/ansible-playbook-examples\/\">ansible playbook examples.<\/a><\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/automated-deployment-git-push-using-ansible\/\" target=\"_blank\" rel=\"noopener noreferrer\">Automated Deployment With Git Push Using Ansible \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/automated-deployment-git-push-using-ansible\/<\/p>\n","protected":false},"author":1,"featured_media":700,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-699","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/699","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=699"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/699\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/700"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=699"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=699"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=699"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}