One of my favorite parts of any work day is the end. I close down my apps one by one, tying up the loose ends for each, clean off my computer’s Desktop and call it a night.
When developing with Magento on a team, it’s easy for developers to accidentally make changes to the core (source) or even intentionally. While this does make development a lot easier, it makes upgrades painful and it’s just bad practice in general. Ideally, we would maintain a separation of concerns. That’s right, it shouldn’t just apply to your application logic, it should apply to its infrastructure as well.
Benefits to this development strategy:
- Upgrades to the Magento core are much simpler
- Faster deployments — less data is stored in the customization repository
- Core patches are applied to the customization repository so core upgrades stay separate
- Easier to identify areas that need test coverage
To prepare both your repositories, there are a few steps you’ll need to go through in order to get things working for your developers. For the purposes of this post, we’ll call our example company Hemingway and our example repositories will be named after it.
Create Empty Repositories
Create new empty repositories on Github:
- hemingway-magento-core (we’ll call this your core repository)
- hemingway-magento-site (we’ll call this your site repository)
Download Magento & Extract
Go to the Magento Downloads page and download Magento CE. I’ve downloaded Magento v18.104.22.168 to my Downloads folder and extracted it to a folder named magento.
Commit Magento to ‘Core Repository’ & Push
cd ~/Downloads/magento/ git init git add . git commit -m "Initial commit of Magento CE v22.214.171.124" git remote add origin git@github .com:tdlm/hemingway-magento-core.git git push -u origin master
Notice that I’ve used tdlm for the username here. You should replace it with your Github username for this to work properly. This step may take a few minutes as you’re uploading the entire Magento code base to Github.
Commit .gitignore File to ‘Site Repository’ & Push
Now that you have your core repository set up, it’s time to set up your site repository.
Click here to download my .gitignore that I use. You can either download the site repository by cloning it or you can create an empty directory and add the file. We’re going to do it that way for this example.
cd ~/Downloads/ mkdir hemingway-site cd hemingway-site/ git init cp ../.gitignore . git add .gitignore commit -m "Initial commit for Hemingway; adding base .gitignore file" git remote add origin git@github .com:tdlm/hemingway-magento-site.git git push -u origin master
Clean Up Shop
Just to make sure we have all our core files covered in our .gitignore file, let’s copy our core repository files into the site repository directory we just created. If anything shows up as needing to be committed, we’ll add it to the .gitignore until it’s clean.
cp -rp ~/Downloads/magento/ . git status # On branch master nothing to commit (working directory clean)
Once we see this message, we’re cleared to move forward.
Magento Wizard & Database Export
At this point, you can either copy the contents of the site repository to your web folder to point your web server at it, or you can point your web server at the directory we just created. Either way, I recommend going through the Magento wizard like you normally would in order to get your database set up, your local.xml file generated and an export (MySQL dump) created for your developers. I won’t detail this process as it’s tailored to the needs of your site.
Once your database is set up and distributed to developers, all further changes to the database should be made through migration files whenever possible. This will keep all your environments aligned.
Now that your local.xml is created, make certain it does not show up when running a git status. If it does, add it to your .gitignore. Otherwise, we’ll copy this file after removing the password and commit that up for developers to use in their locals:
cd app/etc/ cp local.xml local.xml.dev git add local.xml.dev git commit -m "Adding developer copy of local.xml as local.xml.dev so developer can just rename it to get started" git push -u origin master
Local Environment Setup
This next part will be geared toward the developer who you’ve set things up for.
- You’ve already set up the MySQL Database from the SQL Export from above
- You know your MySQL host, username, password and database name to plug into the local.xml
- You have proper access to the Git repositories needed (core repository and site repository)
Clone Repositories to Temporary Location
cd ~/Downloads/ mkdir temp && cd temp/ git clone git@github .com:tdlm/hemingway-magento-core.git core git clone git@github .com:tdlm/hemingway-magento-site.git site
Create Web Directory & Copy
Now that our repositories are in their temporary locations, we’ll first create our web directory, then copy the core repository in, followed by the site repository. Note the use of rsync here as this will guarantee the .git folders get copied over from the site repository. If you don’t have rsync, just make sure you use a command that gets everything.
cd ~/Sites/ mkdir hemingway && cd hemingway cp -r ~/Downloads/temp/core/* . cd ~/Downloads/temp/site/ rsync -av . ~/Sites/hemingway/ # Now we can check our directory to make sure it works as a git repository cd ~/Sites/hemingway/ git status # On branch master nothing to commit (working directory clean)
Clean Up Shop
This is basically a placeholder to let you know you can delete your temporary repository folders:
rm -rf ~/Downloads/temp/
Create Local.xml & Start Server
Now we can go into our directory, create our local.xml, change its settings to point to our database. Once that’s done, we can fire up the web server.
cd ~/Sites/hemingway/ cp app/etc/local.xml.dev app/etc/local.xml # Now edit local.xml to point to your database you set up previously
Once the proper details are filled in for our database, it’s time to fire up the web server.
Now when we make changes to non-core files, Git will see the change and tell you to commit it just as you normally would. You’ll probably notice that when we make changes to core files in our site repository, Git doesn’t seem to notice.
There are a couple of ways around that.
Changes to Core
Not that it’s the best practice, but if you have to, there are a couple of ways you can make changes to core files given this new setup.
# 1. Add file to .gitignore file as exception app/code/core/ !app/code/core/Mage/Core/Model/Cache.php # 2. Force add the file to Git git add -f /app/code/core/Mage/Core/Model/Cache.php
The nice thing about both of these methods is that they force us to be much more aware of the changes we make to core. In other words, unless we really want to change core, we can’t.
While this does add some overhead to setting up your local development environment, it can help to create a much more stable environment when working with a lot of developers on Magento. As you can imagine, there are other points to go into, such as production deployments. However, I think the information I’ve covered here more than paints the picture on how to do that.
- Freed up about 50GB on my Mac’s SSD
- Cleaned Desktop completely
- Cleaned out applications I’m not using
- Cleaned/organized Downloads & Documents folders
- Cleared about 6GB of space on my iPhone
- Synced photos to my Mac on iPhoto & deleted them on the phone
- Deleted applications I’m not using
- Moved less-often-used application icons off the main screen
And, as usual, I keep my Gmail at Inbox Zero. Feels good man.