Deployment Automation with Git, Bitbucket, and PHP

I am trying to set up automatic deployment when I push git to my bitbucket repository. I have a php deploy script that I used from this blog , but when I run the script, it registers that it is only updated from the previous commit.

Here is an example. Say I went to my server and typed git pull. The server will be updated with the latest changes and allows us to claim that the hash for this commit was 001. However, if I make several commits, call them 002, 003 and 004, my script should run every time, assuming that I pushed these changes to the bitpacket after every commit. The script starts, but every time it saves the changes from 001. Only when I log in to my server and type git pull, will the server update to 004. Do you know what can cause this?

// Make sure we're in the right directory exec('cd '.$this->_directory, $output); $this->log('Changing working directory... '.implode(' ', $output)); // Discard any changes to tracked files since our last deploy exec('git reset --hard HEAD', $output); $this->log('Reseting repository... '.implode(' ', $output)); // Update the local repository exec('git pull '.$this->_remote.' '.$this->_branch, $output); $this->log('Pulling in changes... '.implode(' ', $output)); // Secure the .git directory exec('chmod -R og-rx .git'); $this->log('Securing .git directory... '); if (is_callable($this->post_deploy)) { call_user_func($this->post_deploy, $this->_data); } $this->log('Deployment successful.'); 
+4
source share
4 answers

What I would recommend is the release, not based on the latest version in your host, but the most recent tag.

/home/my-user/my-application/1.0.12/www
/home/my-user/my-application/1.0.13/www

etc .. This provides rollback functionality. You can create a PHP script that connects to your server via SSH and creates a new clone based on this tag. If you use Composer, you can use it to execute commands. If not, you can do this with a makefile.

Edit: I forgot to mention how you link it.

You have a symbolic link
/home/my-user/my-application/www -> /home/my-user/my-application/1.0.12/www

When your script deployment completes without errors, you switch the symlink to:
/home/my-user/my-application/www -> /home/my-user/my-application/1.0.13/www

Your application now works without downtime.

+5
source

A very simple and effective approach is to use bitbucket pipelines, you can create a YAML script to compile your dependencies and push the code to the server for each commit, automatically or manually.

The following is an example of the Bitbucket YAML script pipeline:

 image: php:7.1.1 pipelines: default: - step: caches: - composer script: - apt-get update && apt-get install -y unzip - curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer - composer install - step: deployment: staging name: Deploy to Staging Server script: - apt-get update - apt-get -qq install rsync - echo "Upload Project files..." - rsync -avH * -e "ssh" root@ $IP_SERVER:/var/www/html/ 

This script installs Composer dependencies inside a Docker instance and pushes the project files to the server.

+1
source

The problem is file resolution.

I followed the same link to this blog post. I found that the user "www-data", which is used by the php and nginx processes, does not have permission to write to your repository code. It can't even use git.

To verify this, try doing a 'git pull' on the server as "www-user". You can switch to it using 'sudo su www-data'. You will find that it does not even recognize this as a valid git repo and cannot run your "deploy.php" script without errors.

You need to set the correct permissions for your repository so that www data can access it.

Or you change the whole approach. Follow this post http://toroid.org/ams/git-website-howto , which I think is much better than the above. I used this method.

0
source

You want to install the post-update hook on the remote computer you click on.

In the default case, git will not check any data that you click on the remote. Therefore, the default configuration git refuses to click on the marked branch, since the extracted files are no longer updated with HEAD when you click on the extract branch.

When the console receives a click on a branch, you can respond to this in post-update . To find out what will happen, you must first start with some sort of logging:

 echo "post update `date`: $*" >> /home/ingo/test/githooks.out 

When I click on the new branch, for example, I get the line

 post update Mi 24. Jun 13:01:14 CEST 2015: refs/heads/new 

where $* contains the branches I clicked on.

With this, you can write just write a script to check this thread. I prefer to check individual heads, since it is much easier to combine work with several modular repositories (without submodules) in a deployed version. See my answer in deploying source code on how to check and work with code outside the git repository.

For example, you could do

 for b in $* do B=`basename $b` if [ "$B" = "publish" ] # react on changes to the publish branch then git --work-tree "PATH TO THE PUBLISHED WORK TREE" checkout publish do_some_extra_work # to change permissions or inform your team leader fi done 

Of course, you can also do the verification step, which is performed when you click

 if [ "`cat HEAD`" = "ref: refs/heads/master" ] # only do that checkout if the repository is in a sane state then git merge new # I assume you push to new fi 

It's great that you will see the output of your hook on the remote control in the terminal where you did push:

 # git push remote master:new Counting objects: 3, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done. Total 3 (delta 0), reused 0 (delta 0) remote: Updating f81ba5b..a99a710 remote: Fast-forward remote: a.txt | 2 ++ remote: 1 file changed, 2 insertions(+) To ../remote db48da1..a99a710 master -> new 
0
source

All Articles