{"id":397,"date":"2018-02-24T15:25:41","date_gmt":"2018-02-24T15:25:41","guid":{"rendered":"http:\/\/kevinmichaelcoy.com\/blog\/?p=397"},"modified":"2018-02-27T04:13:12","modified_gmt":"2018-02-27T04:13:12","slug":"how-to-point-bitbucket-webhook-to-jenkins-on-localhost","status":"publish","type":"post","link":"http:\/\/kevinmichaelcoy.com\/blog\/2018\/02\/24\/how-to-point-bitbucket-webhook-to-jenkins-on-localhost\/","title":{"rendered":"How to Point BitBucket Webhook to Jenkins on Localhost"},"content":{"rendered":"<p>If you want to host your Jenkins locally on your computer for testing, but are interested in leveraging BitBucket&#8217;s webhooks to point to your localhost, then you need to expose your computer to the outside world. \u00a0There are several ways to do this, but the easiest I have found is to use a tool called <a href=\"https:\/\/ngrok.com\/download\"><code>ngrok<\/code><\/a>. If you do not have it installed already and are using a Mac, I would suggest that you install it with <a href=\"https:\/\/brew.sh\">Homebrew<\/a>. This post was inspired by some instructions found on \u00a0<a href=\"https:\/\/developer.github.com\/webhooks\/configuring\/\">GitHub<\/a> actually, as opposed to BitBucket.<\/p>\n<p>[code language=&#8221;bash&#8221;]<br \/>\n$ \/usr\/bin\/ruby -e &quot;$(curl -fsSL https:\/\/raw.githubusercontent.com\/Homebrew\/install\/master\/install)&quot;<br \/>\n$ brew cask install ngrok<br \/>\n[\/code]<\/p>\n<p>You are ready to run <code>ngrok<\/code> now to expose your localhost to the outside world now. In this situation, I am demonstrating an example of exposing my local Tomcat; however, one word of caution is that you may not want to expose the default <code>8080<\/code> port. Reasons for doing this including a poor mans security to limiting the inbound traffic you expose yourself to by doing this. In order to change your default port for Tomcat, follow the instructions <a href=\"https:\/\/www.mkyong.com\/tomcat\/how-to-change-tomcat-default-port\/\">here<\/a>. An even better option would be to expose a secure link on <code>https<\/code>.<\/p>\n<p>So, presuming you have installed <code>ngrok<\/code> and you have changed your default port to something non-standard, then you are ready to run the tool to expose your machine to the outside world:<\/p>\n<p>[code language=&#8221;bash&#8221;]<br \/>\n$ ngrok http 8282<br \/>\n[\/code]<\/p>\n<p>Once you run this command, you will see the program run in the foreground (ideally you would want to run this in the background and\/or as a service on reboot). In order to test that you have set things up successfully, we can test by simply going to one of the two provided URLs in a browser and in my case it would take you to the Apache Tomcat landing page; however, we really want to test that we can get a webhook to work. So, in order to do this, we could use BitBucket to trigger a webhook to a Jenkins instance deployed to my local Tomcat whenever I push a code change to BitBucket. Here is the configuration:<\/p>\n<div id=\"attachment_395\" style=\"width: 2040px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-395\" loading=\"lazy\" class=\"size-full wp-image-395\" src=\"http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/BitBucket-Jenkins-Webhook-settings.png\" alt=\"Jenkins BitBucket webhook\" width=\"2030\" height=\"746\" srcset=\"http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/BitBucket-Jenkins-Webhook-settings.png 2030w, http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/BitBucket-Jenkins-Webhook-settings-300x110.png 300w, http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/BitBucket-Jenkins-Webhook-settings-768x282.png 768w, http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/BitBucket-Jenkins-Webhook-settings-1024x376.png 1024w, http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/BitBucket-Jenkins-Webhook-settings-150x55.png 150w\" sizes=\"(max-width: 2030px) 100vw, 2030px\" \/><p id=\"caption-attachment-395\" class=\"wp-caption-text\">Jenkins BitBucket webhook<\/p><\/div>\n<p>Now, we just need to make a code change and push it for testing. We can see both from <code>ngrok<\/code> and BitBucket that things worked by witnessing the HTTP 200 OKs.<\/p>\n<div id=\"attachment_399\" style=\"width: 1116px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-399\" loading=\"lazy\" class=\"size-full wp-image-399\" src=\"http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/ngrok-running-receiving-two-push-requests.png\" alt=\"ngrok running and it has successfully received two push requests from the webhook in BitBucket\" width=\"1106\" height=\"548\" srcset=\"http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/ngrok-running-receiving-two-push-requests.png 1106w, http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/ngrok-running-receiving-two-push-requests-300x149.png 300w, http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/ngrok-running-receiving-two-push-requests-768x381.png 768w, http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/ngrok-running-receiving-two-push-requests-1024x507.png 1024w, http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/ngrok-running-receiving-two-push-requests-150x74.png 150w\" sizes=\"(max-width: 1106px) 100vw, 1106px\" \/><p id=\"caption-attachment-399\" class=\"wp-caption-text\"><code>ngrok<\/code> running and it has successfully received two push requests from the webhook in BitBucket<\/p><\/div>\n<div id=\"attachment_402\" style=\"width: 2010px\" class=\"wp-caption aligncenter\"><img aria-describedby=\"caption-attachment-402\" loading=\"lazy\" class=\"size-full wp-image-402\" src=\"http:\/\/kevinmichaelcoy.com\/blog\/wp-content\/uploads\/2018\/02\/BitBucket-Jenkins-Webhook-requests-logs.png\" alt=\"Jenkins BitBucket Webhook Successful 200 OK requests log\" width=\"2000\" height=\"726\" \/><p id=\"caption-attachment-402\" class=\"wp-caption-text\">Jenkins BitBucket Webhook Successful 200 OK requests log<\/p><\/div>\n<p>By default, <code>ngrok<\/code> will only give you a session that last few a few hours at a time. Your options are to periodically restart your tunnel to expose your IP to the outside world, which also means needing to update your webook with the newest URL, or to sign up and register an authorization token: <a href=\"https:\/\/ngrok.com\/upgrade\">https:\/\/ngrok.com\/upgrade<\/a>. Even so, if you restart your command again (ie, reboot your machine or accidentally close the terminal that you used to launched the program), then you will still get a new URL.. As you may have guessed, the want you to pay them to get a static address.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you want to host your Jenkins locally on your computer for testing, but are interested in leveraging BitBucket&#8217;s webhooks to point to your localhost, then you need to expose your computer to the outside world. \u00a0There are several ways to do this, but the easiest I have found is&#8230;<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"http:\/\/kevinmichaelcoy.com\/blog\/2018\/02\/24\/how-to-point-bitbucket-webhook-to-jenkins-on-localhost\/\">Continue reading<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[282,307,312,313,311,310,309,237,308],"_links":{"self":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/397"}],"collection":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/comments?post=397"}],"version-history":[{"count":4,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/397\/revisions"}],"predecessor-version":[{"id":435,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/posts\/397\/revisions\/435"}],"wp:attachment":[{"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/media?parent=397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/categories?post=397"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/kevinmichaelcoy.com\/blog\/wp-json\/wp\/v2\/tags?post=397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}