Jump to content

Using git with a CMS for version control and deployment on multiple machines.


n0sleeves
 Share

Recommended Posts

Please read this first as this is very similar to what I currently do: http://www.sitepoint.com/forums/showthread.php?1183418-Using-git-with-a-CMS-for-version-control-and-deployment


 


What I want to do with multiple machines


I see all this talk about using the cloud with your local dev. environment and linking your local server to it so that you may edit and work on your files across multiple computers. I too am even contemplating this workflow and see why it’s so popular. However, it just seems like an extra step when there is Git available. Tell me if the workflow I am considering makes sense or not please. Although I have been researching for the four days, I am still new to Git and command-line, but I have become familiar with it over the weekend and really want to use a command-line/Git workflow. I have always developed sites the old-fashioned way (code, ftp, rinse, lather, repeat).


Here is what I am contemplating:


  1. Develop locally in the default installation of WAMP.
  2. Commit files into Git local repo which my WAMP would be instructed to reference for it's "www and data" directory
  3. Push local repo to hosted private repo (Bitbucket)
  4. Pull hosted repo into live production server (not automatically since I want full control over when)
  5. If on another computer, pull / clone from Bitbucket and repeat

My thinking is this could work with multiple computers as well, thus eliminating the need for Dropbox since the hosted repo will always have the most current version anyway. If I wanted to work on the site on another computer, couldn't I just clone the repo? The only hiccup I see is having to export/import the database each time. However, I am sure there is a way to automate this as well? I am interested in trying to come up with a way to keep the CMS's and it's database in sync as well. Is it possible to use the method I describe above? I’m curious to hear your thoughts and suggestions. 


 


Thank you very much for your time and knowledge. I would love to start a discussion on this because I really want to implement a better workflow, especially using Git with CMS's and if possible, it's databases. 


  • Like 2
Link to comment
Share on other sites

Keeping the databases between local develop (laptop) and online production in correct sync is always a pain.

Production databases change because of online:

1. visitor interaction with the websites

2. automatic updates

3. blogs

4. automated newsletters

5. guestbooks

6. online subscriptions

7. etc.

These changes are not in sync with the local develop database. Because of this I have crashed a few websites

in the past when syncing a local database with a production database that was online for a while.

Would be good to solve this one day though.

Read more here:

http://processwire.com/talk/topic/1284-best-practice-for-staging-to-live-database-synchronization/
http://processwire.com/talk/topic/3229-version-control-for-mysql-via-git-hooks/

  • Like 2
Link to comment
Share on other sites

Thank you. Interesting reads. 

What is specifically getting exported when you export the database? I ask because I have a fresh PW install on the development machine. I want to export the server database and import on my local/dev environment. I'd like to populate the dev database with at least top-level page content. I don't need every blog post or media upload. Can I selectively choose which tables get exported? I ask because it makes editing the templates easier when I can see some of my live site content. If not, the templates/pages are empty.

Also, what if I want to add/test some new fields in the admin area and only want to update the live server with the new additions only? Is that possible by merging somehow? Or do I have to basically recreate everything on the live server from scratch in terms of fields and the local dev machine is just to used for "experimenting"? Is that how dynamic-site developers work?

Still would like to know more about the Git workflow in conjunction with Wamp and multiple machines if anyone wants to chime in on that.

Sorry I am asking so many questions. I am just trying to wrap my head around all of this. I've always been a static-website kind of developer until now. I just want a workflow that allows me to change/edit templates, add/edit fileds, apply cms updates and have all these managed through Git, synced trough multiple machines  :grin: 

 

Link to comment
Share on other sites

@n0sleeves

Just following up on the idea of using git to control pushing from a dev to production situation (preferably via a test stage) and ignoring the DB sync issue for now. Caveat: Although I haven't exactly done all of what I'm about to suggest here, I have been using elements of these things for a while but never tied them together.

I started using this model of linked github repos a couple of years back1. If you read the article, you'll see that it addresses a lot of what you want to do to a degree - pushing from one of the (possibly many) development copies to the 'hub' automatically gets reflected in 'prime' - his live site. If you also adopted a disciplined use of something like Vincent Driessen's Gitflow Branching Model2 (where the master branch in your repos is the only one representing releases) and make sure that the working directory on prime only ever has master checked out then you could even commit work-in-progress in other branches to your hub (thus backing it up) and not have it show up on your live site. Only when you pushed up a change to the master branch would prime's staging area change and hence the live site get updated.

Anyway, hope that helps with at least part of the problem.

1 Actually, I used that article as the basis for automatically backing up my git repos by (sort-of) reversing his model. I had the bare 'hub' physically located on a remote external hard disk which was then SSHFS mounted into my local filesystem every time my machine booted. I worked on the 'prime' side of things - which was my local development repo, not a live server - and pushing a change locally was automatically mirrored onto my offsite bare 'hub' backup thanks to git hooks.

2 There are repos (original and more up-to-date clones) that provide command line tools to implement his model.

Edited to add: I'll update the readme file for the script I use to create 'hub' and 'prime' repos and put the script up on github in a while.

Edited to add: I no longer use the Gitflow branching model - I find it cumbersome. There are plenty of alternatives.

  • Like 4
Link to comment
Share on other sites

Yeah, I can't help I'm afraid but would also love to know if there is a quick non-phpmyadmin-style method to updating local database from live and vise-versa.

I'm not sure if this is what you are looking for, but I use HeidiSQL on my Desktop to manage all MySQL-DBs. It is possible to transfer complete Data or only selected tables from one DB to another on the fly with just a few clicks. Really quick and simple. (It is native for Windows or for Mac/Linux/*nix via Wine, or from a VirtualBox with a Win-Image)

  • Like 4
Link to comment
Share on other sites

  • 10 months later...

Just thought I'd share my git hook (post-receive) that I've been successfully using for a few months now to update my beta site and production site via git (no more ftp!). Might help someone else. As you can see, I only have the "site" folder under version control. But now when I'm thinking about it, site/templates should probably be enough. 

#!/bin/bash
while read oldrev newrev ref
do
	
    if [[ $ref =~ .*/master$ ]];
    then
        echo -e "Master ref received.  \e[42mDeploying master branch to production site...\e[0m"
        WORKTREE=../site # define your work tree, where you want to push the files
		cd $WORKTREE # navigate to the work tree
		GITDIR=../mysite.git # in relation to the "cd" command above
		# update the working tree
		git --work-tree=./ --git-dir=$GITDIR checkout -f master
		git --work-tree=./ --git-dir=$GITDIR clean -fd master
		# return to git directory
		cd $GITDIR
    else
    	echo -e "Ref $ref received. \e[104mDeploying $ref branch to beta site\e[0m"
    	WORKTREE=../beta/site
		cd $WORKTREE
		GITDIR=../../mysite.git # in relation to the "cd" command above
		# update the working tree
		git --work-tree=./ --git-dir=$GITDIR checkout -f stage
		git --work-tree=./ --git-dir=$GITDIR clean -fd stage
		# return to git directory
		cd $GITDIR
    fi
done

  • Like 6
Link to comment
Share on other sites

  • 2 weeks later...
  • 6 months later...

Some best practices in the Docs or Tutorials section about keeping processwire installations version controlled, could be helpful. So, what's the best way to handle modules and templates. Submodules, subtrees or just one bloating site-repository. Also deployment, with git-hooks (or whatever to recommend), could be mentioned there.

Any thoughts on this?

  • Like 2
Link to comment
Share on other sites

On 03.08.2015 at 6:10 PM, Yannick Albert said:

Some best practices in the Docs or Tutorials section about keeping processwire installations version controlled, could be helpful. So, what's the best way to handle modules and templates. Submodules, subtrees or just one bloating site-repository. Also deployment, with git-hooks (or whatever to recommend), could be mentioned there.

I like the idea. I have been looking for proper way to version control PW sites recently, and have not ended with a clear decision for myself. Of course "it depends on a project". But we can come up with a tutorial helping make the choices.

What to version in git

I never tried initialising a git repo in templates folder, as Ryan suggested here. Custom modules and now site/ready.php should go to version control, as well as config.php. For me the choiсe is either to version the whole installation or only the /site folder. As I usually modify the .htaccess I am leaning to the former.

The next thing is what to ignore. Some think you should ignore /wire, some think it is worth having it in the repo. If we could (I mean If I knew how to) install the needed version of PW core as a dependency via composer or include it in a similar way automatically after deployment / git checkout, it would be better to leave /wire off. But for simplicity it could be better not to exclude it as it is quite small.

That definately should be excluded is some part of site/assets. If the site is a big one, excluding site/assets/files is a must. But for a small near to static it is probably worthless.

You should probably ignore these almost every time not to mess with constantly changing data:

site/assets/cache
site/assets/sessions
site/assets/logs

I found no reason not to include all modules to the VCS.

Deployment

Speaking about deployment, I do not like the idea to deploy every time I commit (which seems to be the case if using git-hooks, if I am getting it right). There are other ways for deployment with git, but I an intrigued by doing it with something like this or this (both are capistrano-like deployment tools in php).

Anyway, I do not seem to find a way to easily deploy database changes. Maybe in the case of semi-static sites it is possible to dump a database and restore in with git-hooks (or to include a sql dump from sites/assets/backup/database to the revision and manually restore it), but it probably will never work with subscription sites and rich-content sites managed by clients.

The only way to keep track of changes made to the database via PW GUI (admin) I came up with is keeping a journal. I made a folder where I include subfolders related to git commits with JSON export files for fields, templates and forms, which should be imported. Pages are just described in .txt as I know of no way to export them to something like JSON.

It would be awesome to have something like the things discussed here available not to do it manually.


Hope it is somehow useful. I would be delighted to hear how others do it, as I am certainly only an amateur in all this stuff.

  • Like 6
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...