2019-04-19T01:15:50+00:00http://rrevanth.github.io/Feed tagged TRAVIS - Revanth's Blog I am a developer, photography enthusiast, and self-proclaimed music connoisseur.This is where I write about awesome things that intrigue me. Revanth RevooriAutomate Jekyll Blog to Github with Travis CI2015-11-11T23:14:34+00:002015-11-11T23:14:34+00:00http://rrevanth.github.io/blog/2015/11/jekyllRakeTravisCI/<p>In addition to supporting regular HTML content, GitHub Pages supports Jekyll, a simple, blog-aware static site generator. Jekyll makes it easy to create site-wide headers and footers without having to copy them across every page. It also offers some other advanced templating features.</p>
<!--more-->
<p>Every GitHub Page runs through Jekyll when you push content to a specially named branch within your repository. For User Pages, use the master branch in your username.github.io repository. For Project Pages, use the gh-pages branch in your project’s repository. Creating blog in Jekyll is awesome but it comes with a catch.Everytime we publish a post,we need to update the master branch with compiled website. Also, post editing is a horror if you are a person who likes to do it in editor.</p>
<p>In this post,we will go through</p>
<ol>
<li>Deploying a jekyll blog to github with rake</li>
<li>Automate the build process with Travis CI</li>
<li>Github content editor Prose</li>
</ol>
<h3 id="deploying-with-rake">Deploying with Rake</h3>
<p>This post assumes that your jekyll blog git repo contains two branches</p>
<ul>
<li>source branch - contains the source files for blog.</li>
<li>master branch - contains the compiled blog.</li>
</ul>
<p>Make sure you are in source branch.Add a file named Rakefile to your root dir that contains the following code ..</p>
<p>Rakefile</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre><span class="nb">require</span> <span class="s1">'rubygems'</span>
<span class="nb">require</span> <span class="s1">'rake'</span>
<span class="nb">require</span> <span class="s1">'rdoc'</span>
<span class="nb">require</span> <span class="s1">'date'</span>
<span class="nb">require</span> <span class="s1">'yaml'</span>
<span class="nb">require</span> <span class="s1">'tmpdir'</span>
<span class="nb">require</span> <span class="s1">'jekyll'</span>
<span class="n">desc</span> <span class="s2">"Generate blog files"</span>
<span class="n">task</span> <span class="ss">:generate</span> <span class="k">do</span>
<span class="no">Jekyll</span><span class="o">::</span><span class="no">Site</span><span class="p">.</span><span class="nf">new</span><span class="p">(</span><span class="no">Jekyll</span><span class="p">.</span><span class="nf">configuration</span><span class="p">({</span>
<span class="s2">"source"</span> <span class="o">=></span> <span class="s2">"."</span><span class="p">,</span>
<span class="s2">"destination"</span> <span class="o">=></span> <span class="s2">"_site"</span>
<span class="p">})).</span><span class="nf">process</span>
<span class="k">end</span>
<span class="n">desc</span> <span class="s2">"Generate and publish blog to master"</span>
<span class="n">task</span> <span class="ss">:publish</span> <span class="o">=></span> <span class="p">[</span><span class="ss">:generate</span><span class="p">]</span> <span class="k">do</span>
<span class="no">Dir</span><span class="p">.</span><span class="nf">mktmpdir</span> <span class="k">do</span> <span class="o">|</span><span class="n">tmp</span><span class="o">|</span>
<span class="nb">system</span> <span class="s2">"mv _site/* </span><span class="si">#{</span><span class="n">tmp</span><span class="si">}</span><span class="s2">"</span>
<span class="nb">system</span> <span class="s2">"git checkout -B master"</span>
<span class="nb">system</span> <span class="s2">"rm -rf *"</span>
<span class="nb">system</span> <span class="s2">"mv </span><span class="si">#{</span><span class="n">tmp</span><span class="si">}</span><span class="s2">/* ."</span>
<span class="n">message</span> <span class="o">=</span> <span class="s2">"Site updated at </span><span class="si">#{</span><span class="no">Time</span><span class="p">.</span><span class="nf">now</span><span class="p">.</span><span class="nf">utc</span><span class="si">}</span><span class="s2">"</span>
<span class="nb">system</span> <span class="s2">"git add ."</span>
<span class="nb">system</span> <span class="s2">"git commit -am </span><span class="si">#{</span><span class="n">message</span><span class="p">.</span><span class="nf">shellescape</span><span class="si">}</span><span class="s2">"</span>
<span class="nb">system</span> <span class="s2">"git push origin master --force"</span>
<span class="nb">system</span> <span class="s2">"git checkout source"</span>
<span class="nb">system</span> <span class="s2">"echo published to master"</span>
<span class="k">end</span>
<span class="k">end</span>
<span class="n">task</span> <span class="ss">:default</span> <span class="o">=></span> <span class="ss">:publish</span></pre></td></tr></tbody></table></code></pre></figure>
<p>After adding this,you can build and deploy the updated source code to master just by running one command.Make sure you are in root dir of your git repo and run the following command :</p>
<blockquote>
<p>rake</p>
</blockquote>
<p>That’s it.So simple,right.Not so fast.We just don’t wanna run this command every time we create to post.We need something much better.</p>
<h2 id="enter-travisthe-saviour">Enter Travis,the saviour</h2>
<h3 id="travis-ssh-setup">Travis SSH setup</h3>
<p>For travis to have git repo permissions,you can do this two ways</p>
<ol>
<li>Using Git access tokens (Less secure method)</li>
<li>Using SSH keys (More secure and recommended method)</li>
</ol>
<p>Generate a private/public key pair without passphrase in the repo directory.Be sure to be in root dir,not any sub dir or other system dir.</p>
<blockquote>
<p>ssh-keygen -t rsa -C “deploy@travis-ci.org” -f deploy_key -N ‘’</p>
</blockquote>
<p>Add the public key (deploy_key.pub) to the Git repo as a ‘Deploy Key’ through the web interface. We are using deploy keys so that we can make them specific to a single repository. You could use ‘Personal access tokens’ but they would then allow access to all repositories associated with the given account - this might be preferable in an organization context, using special GitHub accounts created specifically for Travis-CI to work with multiple repositories. The use of ‘Personal access tokens’ (and my starting point for this approach) is illustrated in Evan Sosenko’s article <a href="https://evansosenko.com/posts/automatic-publishing-github-pages-travis-ci/" title="Personal Access Jekyll Travis">Automatic publishing to GitHub Pages with Travis CI</a></p>
<p>Install the travis gem</p>
<blockquote>
<p>gem install travis</p>
</blockquote>
<p>Login to travis with your GitHub credentials</p>
<blockquote>
<p>travis login</p>
</blockquote>
<p>Encrypt the SSH key to generate <strong>deploy_key.enc</strong>.</p>
<blockquote>
<p>travis encrypt-file deploy_key</p>
</blockquote>
<p>This should modify your .travis.yml file adding the openssh keys to before_install property like this</p>
<blockquote>
<p>before_install:
openssl aes-256-cbc -K {blah blah blah}</p>
</blockquote>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
</pre></td><td class="code"><pre><span class="no">Add</span> <span class="kp">private</span> <span class="n">deploy</span> <span class="n">key</span> <span class="n">files</span> <span class="n">to</span> <span class="p">.</span><span class="nf">gitignore</span>
<span class="n">deploy_key</span>
<span class="n">deploy_key</span><span class="p">.</span><span class="nf">pub</span></pre></td></tr></tbody></table></code></pre></figure>
<blockquote class="warnquote">
<p>make sure you ignore the SSH keys and only commit the
encrypted version. You may also want to back up the original
key files elsewhere</p>
</blockquote>
<h3 id="setting-travis-in-the-project">Setting Travis in the project</h3>
<p>As you might know,Travis CI is used for testing and deploying automatically for git repositories.We are gonna use this feature to automatically build and deploy the site to master whenever we push any changes to source.</p>
<p>Adding Travis integration is simple,add .travis.yml file to source root dir.</p>
<p>.travis.yml</p>
<figure class="highlight"><pre><code class="language-ruby" data-lang="ruby"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre><span class="ss">language: </span><span class="n">ruby</span>
<span class="ss">branches:
only:
</span><span class="o">-</span> <span class="n">source</span> <span class="c1"># branch that contains source files</span>
<span class="ss">rvm:
</span><span class="o">-</span> <span class="mf">2.2</span><span class="o">.</span><span class="mi">3</span>
<span class="ss">before_install:
</span><span class="o">-</span> <span class="n">openssl</span> <span class="n">aes</span><span class="o">-</span><span class="mi">256</span><span class="o">-</span><span class="n">cbc</span> <span class="o">-</span><span class="no">K</span> <span class="vg">$encrypted_</span><span class="p">{</span><span class="n">blah</span> <span class="n">blah</span> <span class="n">blah</span><span class="p">}</span>
<span class="o">-</span> <span class="n">chmod</span> <span class="n">u</span><span class="o">=</span><span class="n">rw</span><span class="p">,</span><span class="n">og</span><span class="o">=</span> <span class="o">~</span><span class="sr">/.ssh/</span><span class="n">deploy_key</span>
<span class="o">-</span> <span class="n">echo</span> <span class="s2">"Host github.com"</span> <span class="o">>></span> <span class="o">~</span><span class="sr">/.ssh/</span><span class="n">config</span>
<span class="o">-</span> <span class="n">echo</span> <span class="s2">" IdentityFile ~/.ssh/deploy_key"</span> <span class="o">>></span> <span class="o">~</span><span class="sr">/.ssh/</span><span class="n">config</span>
<span class="o">-</span> <span class="n">git</span> <span class="o">--</span><span class="n">version</span>
<span class="o">-</span> <span class="n">git</span> <span class="n">config</span> <span class="n">user</span><span class="p">.</span><span class="nf">name</span> <span class="s2">"{GIT_UNAME}"</span>
<span class="o">-</span> <span class="n">git</span> <span class="n">config</span> <span class="n">user</span><span class="p">.</span><span class="nf">email</span> <span class="s2">"{GIT_EMAIL}"</span>
<span class="o">-</span> <span class="n">git</span> <span class="n">remote</span> <span class="n">set</span><span class="o">-</span><span class="n">url</span> <span class="n">origin</span> <span class="n">git</span><span class="vi">@github</span><span class="p">.</span><span class="nf">com</span><span class="p">:{</span><span class="no">GIT_UNAME</span><span class="p">}</span><span class="o">/</span><span class="p">{</span><span class="no">GIT_UNAME</span><span class="p">}.</span><span class="nf">github</span><span class="p">.</span><span class="nf">io</span><span class="p">.</span><span class="nf">git</span>
<span class="o">-</span> <span class="n">git</span> <span class="n">remote</span> <span class="o">-</span><span class="n">v</span>
<span class="ss">script:
</span><span class="o">-</span> <span class="n">bundle</span> <span class="nb">exec</span> <span class="n">rake</span>
<span class="ss">notifications:
email:
recipients:
</span><span class="o">-</span> <span class="p">{</span><span class="no">GIT_EMAIL</span><span class="p">}</span>
<span class="ss">on_success: </span><span class="n">change</span>
<span class="ss">on_failure: </span><span class="n">always</span>
</pre></td></tr></tbody></table></code></pre></figure>
<p>Replace {GIT_UNAME},{GIT_EMAIL} with their appropriate values.</p>
<p>Now Signin to Travis CI and add your site repo for watching the repo to Automatically build.</p>
<blockquote class="warnquote">
<p>In the project settings,Turn OFF Build pull requests and Build only if .travis.yml is present to ON.</p>
</blockquote>
<p>Now,whenever your push any changes to source branch,Travis should automatically build and deploy the site to master branch.</p>
<h3 id="post-editing">Post Editing</h3>
<p>Recently,I found a great site for post editing,checkout out <a href="http://prose.io/" title="Prose">Prose</a>.It has a awesome Markdown editor that accessess your repo and you can add posts,drafts to your source repo from there.</p>
<p>Life gets so much simplified with all this awesome tools and experience is blissful.</p>