Adam
Adam

Reputation: 29079

automatic pull on production server after push to BitBucket

I develop my code locally, commit it to my local repsoitory and then push it to my Bitbucket server. After that I have to login via ssh to my production server like this:

ssh [email protected]
pass: very-secure-passs
cd www/myPage
cd git pull origin master
pass: very-secure-pass 

I would like to avoid login to my production server and let him pull automatically.

I want to have 3 repositories (local, BitBucket and production Server), so I cant use this solution: Do an automatic pull request after pushing to server

I found this question A hook that let `pull` from VPS when I `push` to Bitbucket which is exactly what I want, but it is from 2013 and the answer is outdated since BitBucket has changed since then.

I found here https://community.atlassian.com/t5/Answers-Developer-Questions/How-can-I-deploy-my-bitbucket-repo-to-my-production-server/qaq-p/565348 that someone suggested to use a free Plugin called HTTP-Request Hook for Bitbucket Server

set up an automated "Pull": Each time you do a push to your central repository, your production machine is notified and pulls the repository on notification. Bitbucket Server offers serval plugins to support the notification process - the one I use is Http Request Post Receive Hook: each time a push is made, a configured URL is contacted, submitting some info. On my production machine I have set up a little web server, waiting for this HTTP-Request. On receiving the HTPP-Request I evaluate the given parameters and perform an action (for example: pulling the repository ...)

Now my questions are:

  1. Is it possible to use a Webhook instead of the HTTP-Request Hook Plugin?

  2. How should the file on my production server look like so that it will do a pull request when it receives a HTTP-Request? I would be interested in a basic example in PHP.

Upvotes: 7

Views: 6548

Answers (2)

Tapas
Tapas

Reputation: 1193

Instead of directly trying to pull on production server, you can setup a Jenkins job that will push the code to production server on each commit. Using Jenkins you can even customize the solutions to match particular token in commit message.

Upvotes: 0

Jim Redmond
Jim Redmond

Reputation: 5660

The webhook documentation you linked is for Bitbucket Cloud (bitbucket.org), not Bitbucket Server (which is self-hosted and has some other URL). If you're using BB Cloud, then the HTTP-Request Hook Plugin won't work, but the documentation you linked will. If you're using BB Server, then you can use https://confluence.atlassian.com/bitbucketserver/managing-webhooks-in-bitbucket-server-938025878.html instead to define a webhook.

For the second half of your question - how to set up your server's end of a webhook - you'll need to have a small service that listens for the incoming webhook, does whatever authentication you want, and then runs your pull method. There are a zillion ways to do this, but most will vary based on your preferred language and security settings and on the network configuration of the server in question. I'd suggest a Google search for "webhook deploy $LANGUAGE" to see how some others have done it with your preferred language, or to see if there's a public repo or gist or snippet out there that you can use.

Upvotes: 4

Related Questions