{"id":11885,"date":"2014-02-25T11:41:52","date_gmt":"2014-02-25T06:11:52","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=11885"},"modified":"2014-02-25T14:18:04","modified_gmt":"2014-02-25T08:48:04","slug":"automating-deployment-of-a-static-website-hosted-on-amazon-s3","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/automating-deployment-of-a-static-website-hosted-on-amazon-s3\/","title":{"rendered":"Automating deployment of a static website hosted on Amazon S3"},"content":{"rendered":"<p style=\"text-align: left\">Most of us know how to host a static website on Amazon S3, but to deploy the website you would need to run the AWS Cli command everytime manually.<\/p>\n<p style=\"text-align: left\">In this blog I&#8217;ll show you how to automatically deploy your website while maintaining version control of your project using server side hook provided by Git.<\/p>\n<p style=\"text-align: left\"><strong>Prerequisite :<\/strong><\/p>\n<ul style=\"text-align: left\">\n<li>Git<\/li>\n<li>Aws Cli tool<\/li>\n<\/ul>\n<p style=\"text-align: left\"><strong>Initializing a Git repository on your existing Project :<\/strong><\/p>\n<p style=\"text-align: left\">If you already have a git repository for your project then you may skip this step , else initialize a git repo on your project :<\/p>\n<p>[shell]<br \/>\ncd \/opt\/testweb<br \/>\ngit init .<br \/>\n[\/shell]<\/p>\n<p style=\"text-align: left\"><strong>Creating\u00a0 a hook to automatically upload the website<\/strong><\/p>\n<p style=\"text-align: left\">Here we&#8217;ll write a \u201cpost-receive\u201d hook , which executes itself on the remote repository after all the refs have been updated. Hooks are located in the .git directory of your project under the hooks folder (<strong>.git\/hooks<\/strong>).<\/p>\n<p style=\"text-align: left\">Our post-receive hook looks somewhat like this :<\/p>\n<p>[shell]<br \/>\n# post-receive hook to upload to s3 bucket<br \/>\n#!\/bin\/bash<br \/>\nset -e<br \/>\nPROJ_DIR=$(dirname $PWD)<br \/>\nunset GIT_DIR<br \/>\ncd $PROJ_DIR<br \/>\n# Specify here , the branch whose code should be pushed to the bucket<br \/>\ngit checkout prod<br \/>\n# aws cli command to sync the code<br \/>\naws s3 sync $PROJ_DIR s3:\/\/my-first-website &#8211;delete &#8211;exclude &quot;.git\/*&quot;<br \/>\ngit checkout master<br \/>\n[\/shell]<\/p>\n<p style=\"text-align: left\">Few points to note here :<\/p>\n<ul style=\"text-align: left\">\n<li>You need to specify the branch here whose code should be pushed to the bucket.<\/li>\n<li>I am using sync command , so that update and delete happens accordingly to your working directory.<\/li>\n<li>and the .git directory is excluded<\/li>\n<li>Don&#8217;t forget to give executable permission to your hook.<\/li>\n<\/ul>\n<p style=\"text-align: left\"><strong>Let&#8217;s Test it out !!!<\/strong><\/p>\n<p style=\"text-align: left\">For this blog, I have a simple html page as soon below hosted on my S3 bucket, which I&#8217;ll later update with a template :<\/p>\n<p style=\"text-align: left\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/02\/ComingSoon.png\"><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-11964\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/02\/ComingSoon.png\" alt=\"html_page\" width=\"819\" height=\"244\" \/><\/a><\/p>\n<p style=\"text-align: left\">Let&#8217;s clone the project into some other directory .<\/p>\n<p>[shell]git clone file:\/\/\/opt\/testweb mywebsite[\/shell]<\/p>\n<p style=\"text-align: left\">After adding and committing new code files in the repo , Let&#8217;s push the changes to the \u201c prod \u201c branch of the server :<\/p>\n<p>[shell]git push origin master:prod[\/shell]<\/p>\n<p style=\"text-align: left\">with this our hook will also get executed and on the terminal you would notice that the hook is uploading your files on your bucket :<\/p>\n<p style=\"text-align: left\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/02\/prod.png\"><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/02\/prod.png\" alt=\"\" width=\"440\" height=\"355\" \/><\/a><\/p>\n<p style=\"text-align: left\">Now Lets check the index.html page on our S3 bucket :<\/p>\n<p style=\"text-align: left\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2014\/02\/after.png\"><img decoding=\"async\" loading=\"lazy\" src=\"\/blog\/wp-ttn-blog\/uploads\/2014\/02\/after.png\" alt=\"\" width=\"491\" height=\"257\" \/><\/a><\/p>\n<p style=\"text-align: left\">And here it is , so from now onward all you need is a simple PUSH \ud83d\ude42<\/p>\n<p style=\"text-align: left\">PS: This was a simple use of a git hook , you can hack it your way to do much more stuff.<\/p>\n<p style=\"text-align: left\">\n","protected":false},"excerpt":{"rendered":"<p>Most of us know how to host a static website on Amazon S3, but to deploy the website you would need to run the AWS Cli command everytime manually. In this blog I&#8217;ll show you how to automatically deploy your website while maintaining version control of your project using server side hook provided by Git. [&hellip;]<\/p>\n","protected":false},"author":108,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":4},"categories":[1174],"tags":[248,1332,375,1335],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11885"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/108"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=11885"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/11885\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=11885"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=11885"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=11885"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}