Hook Bitbucket with Jenkins

Configuring Jenkins

  1. Make sure our Configure Global Security setting is fine
  2. Entry our Jenkins project's Configure
  3. Check Trigger builds remotely
  4. Type the random text into Authentication Token
  5. Check Build when a change is pushed to Bitbucket

Expose the local server to the internet

ngrok allows us to expose a web server running on our local machine to the internet. Just tell ngrok what port our web server is listening on.

  1. Download ngrok for Linux 64-Bit version

    ngrok-stable-linux-amd64.zip

  2. Unzip it to~/
  3. cd ~/
  4. ./ngrok help
  5. Expose a Jenkins server on port 8080 of our local machine to the internet.

    ngrok http 8080

When we start ngrok, it will display the public URL of Jenkins. We can use it to instead of private IP http://localhost:8080.

ngrok by @inconshreveable

Tunnel Status                 online
Version                       2.0.25/2.1.1
Region                        United States (us)
Web Interface                 http://127.0.0.1:4040
Forwarding                    http://86e883ff.ngrok.io -> localhost:8080
Forwarding                    https:///86e883ff.ngrok.io -> localhost:8080

Connnections                  ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00

Configuring Bitbucket

  1. Entry the Settings page of Bitbucket project
  2. Entry the Webhooks sub-page
  3. Press Add webhooks button
  4. Type any Title we like
  5. Type the URL of Jenkins project

    It should be http://USER_ID:API_TOKEN@JENKINS_URL/job/JENKINS_PROJECT_NAME/build?token=TOKEN_NAME

    USER_ID: find it in Jenkins -> People page.

    API_TOKEN: select the user in People page and find it at Configure page.

    JENKINS_URL: need to use public IP address to instead ofhttp://localhost:8080

    JENKINS_PROJECT_NAME

    AUTH_TOKEN: Entry the Jenkins project -> Configure, find Authentication token at Build Triggers.

  6. Check Active of Status
  7. Select Triggers with Repository push
  8. Save it

Test the auto build trigger

  1. Commit the source to Bitbucket
  2. ngrok will display the state on Terminal
    HTTP Requests
    -------------
    POST /job/JenkinsTest/build                              201 Created
    GET  /static/cbf1fb65/images/top-sticker-bottom-edge.png 200 OK
    GET  /static/cbf1fb65/favicon.ico                        200 OK
    
  3. Jenkins will display the progress bar at Build Queue and Build History

Reference

  1. Integrate with BitBucket: build after each commit
  2. 如何整合Bitbucket 自動觸發建置動作
  3. 怎麼將內網的 localhost 讓外面的人都看得到呢

results matching ""

    No results matching ""