Making Jenkins Behave 2: Electric Boogaloo
Tags: jenkins, ci, cd, github
Published: 2019-09-15 15:12:28

---
title: Making Jenkins Behave 2: Electric Boogaloo
published: true
description: Integrating Jenkins & Github with Multi-Branch Pipelines 
tags: Jenkins,ci,cd,github 
cover_image:  https://thepracticaldev.s3.amazonaws.com/i/mavzofyf4x6e8ke44rzd.jpg
---

		
<img alt="Jenkins Being All Formal" src="https://thepracticaldev.s3.amazonaws.com/i/aav0j907623wnqa3oquk.png" style="width:25%;" width="25%"/>



<p>That&#8217;s right, as promised, I&#8217;m going to torture myself with Jenkins some more, this time with multi-branch pipelines!</p>



<p>If you missed it, I recently wrote a blog post in which I explained <a href="//www.errietta.me/blog/jenkins-github-integration/">how to integrate Jenkins and Github with freestyle jobs.</a> In that post, I stipulated that were I able to use Multi-Branch pipelines, my life would have been much easier. Well, it&#8217;s true. Sort of. Multi-branch pipelines, <strong>once you get them working</strong> are much, much better than freestyle jobs. As you may have guessed, the problem is the initial setup<strong>, because Jenkins has incredibly cryptic error messages.</strong> Thanks pals.</p>



<p>Anyway, I&#8217;ve figured it out so you don&#8217;t have to, so let&#8217;s get started!</p>



<p>The first thing you want to do is set up your github user account and repository, before you even touch Jenkins. Unlike last time, where the order of things didn&#8217;t matter , this time it&#8217;s extremely important if you want everything to go smoothly.</p>



<p>Navigate to your <a href="//github.com/settings/tokens">personal access tokens settings</a> on Github. You want to create a token with the following permissions:<br><em><code>admin:org_hook,&nbsp;admin:repo_hook,&nbsp;repo,&nbsp;user:email</code></em><br>They&#8217;re a bit more powerful than last time, but that does mean that Jenkins can set up the repo hooks for you so you don&#8217;t have to.</p>



<p>Now go to your repository and create a Jenkinsfile. That&#8217;s right, we&#8217;re creating the CI/CD pipeline before we&#8217;ve even touched Jenkins at all. This is because if Jenkins doesn&#8217;t find a Jenkinsfile, it pretends that your credentials are wrong and sends you on a wild goose chase, even if they&#8217;re absolutely fine. So just pick one of the <a href="//jenkins.io/doc/pipeline/tour/hello-world/">hello world examples</a> &#8211; it really doesn&#8217;t matter what, and all they do is display a version string, but you really want a valid Jenkinsfile.</p>



<p>Now go to Jenkins and click <strong>create new job</strong> or <strong>new item</strong>, and select <strong>multi-branch pipeline</strong>. Coincidentally, if this is the first time you&#8217;ve ran Jenkins, you might get an infinite loading screen. If that happens, just turn it off and back on again.</p>



<div class="wp-block-image"><figure class="aligncenter"><img src="https://thepracticaldev.s3.amazonaws.com/i/43makafyyk0e7mo27fkp.gif" alt="have you tried turning it off and on again"/><figcaption>&#8220;Hello, IT, have you tried turning it off and on again?&#8221;</figcaption></figure></div>



<ul><li>Click the &#8220;add source&#8221; dropdown and select Github.</li><li>Within credentials, click &#8216;add&#8217; and select &#8216;Jenkins&#8217;.</li><li>Keep Kind as &#8216;username with password. </li><li>As username, enter your github username</li><li>As password, enter the access token from earlier. If you&#8217;ve lost it, like I have, you can just regenerate it provided it&#8217;s not being used anywhere else.</li><li>Under repository https url enter the url of your repository, which can be a private repo.</li></ul>



<p>Now if you click apply and save, your github repo should have a new webhook!</p>



<p>If it&#8217;s worked so far, you may be tempted to click on &#8220;Scan repository now&#8221;. Bad idea. It won&#8217;t work, and it will confuse you.</p>



<p>What you ACTUALLY have to do, is commit a change to master (or whatever branch has the Jenkinsfile). If you do that, and wait a minute, it should automatically build!</p>



<div class="wp-block-image"><figure class="aligncenter"><img src="https://thepracticaldev.s3.amazonaws.com/i/8hj3u4ijig0d20b55pnz.png" alt="" class="wp-image-865"/><figcaption>Jenkins showing the branches</figcaption></figure></div>



<p>The commit on Github will also be updated to show success or failure.</p>



<div class="wp-block-image"><figure class="aligncenter is-resized"><img src="https://thepracticaldev.s3.amazonaws.com/i/vy2417n23lnhcre1p2sm.png" alt="Github commit updates" class="wp-image-866" width="718" height="131"/><figcaption>Github commit updates</figcaption></figure></div>




<p>You&#8217;ll also see PR status!</p>



<div class="wp-block-image"><figure class="aligncenter"><img src="https://thepracticaldev.s3.amazonaws.com/i/mavzofyf4x6e8ke44rzd.jpg" alt="PR status is shown" class="wp-image-868"/><figcaption>PR status is shown</figcaption></figure></div>



<p>As you can see, Multi-branch pipelines are already much easier to work with! I just wish Jenkins weren&#8217;t so cryptic &#8211; I wasted way too much time thinking it wasn&#8217;t working because I either hadn&#8217;t given it enough permissions, or I thought scanning was supposed to work.</p>



<p>Hope you have found this informative! Maybe next time I&#8217;ll dive deeper into multi-branch pipelines and build something cool!</p>