Deploy using git pull
Task – automatically pull files from bitbucket git repo upon commit/merge to master.
Whenever there’s a need to deploy by means of an automated git pull, without the whole container, docker registry, aws overhead, the code below works best.
exec(“git pull https://username:email@example.com/company/repo.git master”);
Here are the step-by step instructions:
- Create a user on bitbucket with access to the desired repository.
- SSH to your remote server, where the code should be pulled to.
- Clone the repository into the desired directory: git clone https://username:firstname.lastname@example.org/company/repo.git
- Create a script (python, php or shell) – doesn’t matter. The “exec” command above is for php. Script should be accessible from the web.
- Add a webhook to the repository on bitbucket, where the triggered url is the url to the php script.
All done! Now every time there’s a change to master, the code will be pulled by the remote server.
Why use username and password and not SSH keys?
SSH keys won’t work for a number of reasons, but anyone is free to give it a shot and let us know how it went. Note, the script is executed by the webserver, which runs under it’s own user that naturally has no access to the private key. Trying to make the keys available for that user is not worth the headache.
Bitbucket's own documentation has an FTP example, why not use that?
The whole FTP business is not secure and not that widely used. If one can avoid spinning up an FTP server, one probably should.
Below are a few search queries suggested by google, left here to help those seeking the solution to their problem