Friday, April 5, 2013

making life easier for PHP developers - auto uploading files

Here is a post that explain the solution to the problem I had faced a little while ago.

I was working on a PHP project. Git was my version control system. And FTP was the primary protocol for uploading the PHP files from development server(on local machine) to the deployment server(remote machine).

I felt that uploading the files manually to the server was tedious task (although I could find out what files have changed between given commits etc). This uploading was taking quite a lot of time of ours so I thought of getting some automated solution to this problem. I "googled" a little about it and here is the solution:

We were already using the Git. We used a ready made - open source solution developed by other developer.

The tool that made our life easier was git-ftp. It is hosted on https://github.com/ezyang/git-ftp. You can freely obtain it and use it for your purpose. In our case we used it for uploading PHP files but it might work for other suitable tasks also.

Here is are the steps to install "git-ftp" on windows machine:
Step 1: Install Python 
Obtain Python 2.7.3 from the official Python website. Start the installer. It should have screens as shown below.









Step 2: Put python executable in path

After the last screen appears click on finish button. No you will have Python installed on your machine.
But executing it from the console would give you an error because it is not in path. In order to put python executable in your path you need to do it as shown in the images below:


Copy the path where python is installed.


Right click on My Computer and then click on properties.

Then click on Advanced System Settings.

Then click on "environment variables" button.

Then search for "Path" in "System Variables" and append the ";" and the path in which Python is installed.

Then click on ok ok ok ...

By now you will have python set and you will be able to execute it from command line.

Step 3 : Install Setup Tools
Installation of setup tools is required. Set up tool's windows installer can be obtained from https://pypi.python.org/pypi/setuptools .
Install it as shown in the screenshots.






By now you will have set up tools installed.

Step 4: Install GitPython
You need to install GitPython then. It is required because the Python script will be accessing the git repository. GitPython can be obtained from http://gitorious.org/git-python.
you need to install it from command line by invoking the setup.py with install parameter.
eg:




Now we have got GitPython installed we can move on to our project directory.

Step 1 to step 4 will be one time only. Step 5 to will be repetitive for project.

Step 5: obtain and put the python script in your project directory.
We can obtain the python script from https://github.com/ezyang/git-ftp.
Put the git-ftp.py script in the project directory.

Step 6: Create a file containing FTP credentials.
We need to create a file containing the credentials in the .git directory. If you don't create the file then it will ask you for details every time the script runs. If we put the file with credentials in .git directory then the python script will automatically read the details and upload that to the server.

The name of file must be "ftpdata" (without quotes). File name should be "ftpdata" without any extensions like txt etc.
If you are having multiple branches and each branch is deployed to different server the file might look like(name in square bracket is name of branch):

Put this file in ".git" directory.



Step 7: After doing this upload the "git-rev.txt" file to the root directory of the server. Put the sha1 of the last commit in the file.
The upload script knows which files needs to uploaded from this file. This file is changed by the upload script each time it executes.


Now you can execute the git-ftp.py scrip. It will upload all the files that are committed in git to appropriate server (depending on your branch).

Some users might also want to create a ".bat" file that looks like:

Some users might also want to create a ".bat" file with "git gc" command (make sure that git is in your path to do this).



Now you have everything set up. Just run the script and everything works !! :D
Every time you make the commit, you can just run the script and all the files committed will be uploaded to the server. No overwrites by mistake!


Suggestions:
1. If upload does not work then execute "git gc" command on your project repository.
2. If upload fails then make sure that you and all others are in sync with each other through git.
3. Read the error message from script if something goes wrong.
4. The installation is easier on linux machines.
5. See the read-me of project at https://github.com/ezyang/git-ftp.

Limitation: 
Some other protocols are not supported by the tool which we are using.