Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Automatic push to production server

amrit_b February 15, 2013

Hello,

I want know wheather its possible to automatically push code to production server? I have my code repo in bitbucket (git) and cloned it into my localhost and my production server. Whenever I do changes, I push it from the localhost and then pull it from the production server. Production server runs on EC2 ubuntu.

2 answers

0 votes
simonolsen September 13, 2018

We use Pipelines with our Drupal 7 sites. Every time we approve a pull request to master branch, it automatically deploys to staging (we use WHM cPanel) by running the Pipeline which

  • checks for a database (and if there is, sets the site to maintenance mode)
  • pulls all the files onto STAGING
  • runs drush updatedb
  • (turns off maintenance mode if it was turned on)

We also have a production branch for deploying automatically to live (so we don't accidentally deploy something live that is on master - it's a specific step to ensure we're only deploying good code to the live server). Same thing. Pull request from master to production and the Pipeline runs again, deploying to live.

You, of course, need to set up your SSH keys so the Pipeline has permissions to deploy files to the server/s.

This is what the bitbucket-pipelines.yml in the root of our Drupal 7 project looks like.

image: phpunit/phpunit:5.0.3
pipelines:
branches:
master:
-
step:
script:
-
make ci-setup
-
make ci-pre-deploy DRUSH=DRUSH_REMOTE_STAGING
-
make ci-deploy-staging
-
make ci-post-deploy DRUSH=DRUSH_REMOTE_STAGING
production:
-
step:
script:
-
make ci-setup
-
make ci-pre-deploy DRUSH=DRUSH_REMOTE_PRODUCTION
-
make ci-deploy-production
-
make ci-post-deploy DRUSH=DRUSH_REMOTE_PRODUCTION

This is what the Makefile in the root of our Drupal 7 project looks like.

USER=__cpanel_username_here__
STAGING_HOST=123.456.789.123
PRODUCTION_HOST=987.654.321.987

DRUSH_REMOTE_STAGING=ssh $(USER)@$(STAGING_HOST) drush -r public_html
DRUSH_REMOTE_PRODUCTION=ssh $(USER)@$(PRODUCTION_HOST) drush8 -r public_html

ci-setup:
mkdir -p ~/.ssh
cat deploy/known_hosts >> ~/.ssh/known_hosts
(umask 077 ; echo $$SSH_KEY | base64 --decode > ~/.ssh/id_rsa)

ci-deploy-staging:
rsync -e 'ssh' -irKzl --delete --verbose --exclude-from="deploy/.deploy-ignore" "./" $(USER)@$(STAGING_HOST):public_html

ci-deploy-production:
rsync -e 'ssh' -irKzl --delete --verbose --exclude-from="deploy/.deploy-ignore" "./" $(USER)@$(PRODUCTION_HOST):public_html

ci-pre-deploy:
if [[ -f sites/default/settings.php ]] ; then $($(DRUSH)) vset maintenance_mode 1; else echo "Nothing to do"; fi

ci-post-deploy:
if [[ -f sites/default/settings.php ]] ; then $($(DRUSH)) updb -y; else echo "Nothing to do"; fi
if [[ -f sites/default/settings.php ]] ; then $($(DRUSH)) vset maintenance_mode 0; else echo "Nothing to do"; fi

We also have a /deploy folder with a .deploy-ignore file...

README.txt
INSTALL.txt
MAINTAINERS.txt
UPGRADE.txt
CHANGELOG.txt
COPYRIGHT.txt
sites/default/settings.php
sites/default/files
tmp
Makefile
.sass-cache
*.css.map
.gitdeploy
.idea
bitbucket-pipelines.yml
error_log
cgi-bin

Also in the /deploy folder, there is a known_hosts file with ssh keys in there. 

One of our senior dev team set this up, and I don't know every in-and-out of our config (eg. the Docker image that does the deploying) so the above won't just be a copy-and-paste affair. But hopefully, it will point you in the right direction. At least you now know it can work with Drupal 7 (I'm still trying to figure out how to get it rocking with Drupal 8 and composer).

Here is a link to the Pipeline docks. Hopefully, this post and the docs will get you going. 

Auto-deployment with Pipelines has been SUCH a time saver for our team.

0 votes
danielwester
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
February 15, 2013
There are a couple ways of doing it. Easiest is to set up a cron job just to do a git pull every X minutes. Not really what you're asking for but it is the easiest. To do what you want - you'd need to set up a service to ping somewhere on a http accessible port. For more on bitbucket services see https://confluence.atlassian.com/display/BITBUCKET/Managing+Bitbucket+Services . Somewhere there is a list of ips that bitbucket pings from so you can filter on that. Then just have whatever is at the other end of that listening - do the git pull. Depending on your scale/usage etc - you might want to consider a CI server though.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events