Salesforce + Git + Eclipse + EGIT = Better and Distributed Source Control
During my J2EE and .Net days, I was much dependent on the subversion repository. I had never thought my code without SVN. When I moved to Salesforce few years back, the first thing I missed is code repository to have better source code control. As svn creates either “.svn” or “_svn” folder, it was not supported by the force.com IDE and I struggled much to get it work out. Salesforce saves only real time information. So it is very difficult to know what was changed in past related to any issue as well as complete code backup.
Era of Git
Git has emerged as the most powerful source code repository and widely accepted by the developer communities. If I will talk about Git this article is going to be out of discussion scope.
Above figure describes Architect of Git. The one thing to notice in above figure is that Git has local repository as well as remote repository which make it better and more flexible than any other code repository tool.
As you can see in above diagram also, user can work on completely different branch which is stored on his local repository and when he thinks that he is ready to push the code on Git, just commit the changes on remote repository.
Step 1:
So, let’s start with using Git with Salesforce using Eclipse plug-in EGit.
So, let’s start with using Git with Salesforce using Eclipse plug-in EGit.
EGit is an Eclipse Team provider for the distributed version control system Git. EGit allows performing Git commands from the Eclipse IDE.
EGit is based on the JGit library. JGit is a library which implements the Git functionality in Java.
Most Eclipse 4.2 packages from Eclipse.org contain EGit in there default configuration. In this case no additional installation is required.
Most Eclipse 4.2 packages from Eclipse.org contain EGit in there default configuration. In this case no additional installation is required.
If EGit is missing in your Eclipse installation, you can install it via the Eclipse Update Manager via: Help ?Install new Software. EGit can be installed from the following URL:http://download.eclipse.org/egit/updates
Step 2:
Gets the Salesforce code in local workspace using force.com IDE.
Gets the Salesforce code in local workspace using force.com IDE.
Step 3:
Now, it is the time to configure repository on your local system which is also known as “Local Repository”. Navigate to “Right click on Project | Team | Share Project”
Now, it is the time to configure repository on your local system which is also known as “Local Repository”. Navigate to “Right click on Project | Team | Share Project”
On the basis of your eclipse configuration, you will get options like CVS, Git, SVN etc. Select “Git” and click on Next button.
Step 4:
Click on “create” button as shown in below image
Click on “create” button as shown in below image
A new pop up will open where we can give Local repository name as well as location where we want to save the local repository. In this article, I am using the name “GITDemo” name. Now click on Finish button.
If you check the Salesforce project, the folder will look something like above image with questions marks (“?”) on folders. Question marks means Git does not know what to do with files, either you can schedule them for commit or add few of them in ignore list (means they will not saved in Git).
Step 5:
Now we will commit our files and folder in Local repository. Before commit we have to mention that which files and folder should be committed and ignored. In this case I am adding complete salesforce directory for commit. To do this, write click on “Project | Team | Add to Index”. After this operation your icons should be something like below image:
Now we will commit our files and folder in Local repository. Before commit we have to mention that which files and folder should be committed and ignored. In this case I am adding complete salesforce directory for commit. To do this, write click on “Project | Team | Add to Index”. After this operation your icons should be something like below image:
It’s time to commit all indexed files and folders in local repository. Again navigate to “Project | Team | Commit”. New Popup window will appear and ask for comment. It is mandatory to provide meaningful comment for each commit as it will be very easy for diagnosis in case needed in future.
After commit the folder structure will appear with following icons:
Step 6:
Until here, we have done configuration for Local repository. Let’s move on to create a remote repository. There are lots of options available for hosting your code which supports Git. Few of them are
Until here, we have done configuration for Local repository. Let’s move on to create a remote repository. There are lots of options available for hosting your code which supports Git. Few of them are
Choice is up to us, which depends on license cost and many other factors. For demo, I am using the “BitBucket”.
Create a free new account at “BitBucket” and create a repository. After successful creation of Git repository you will get the URL for your repository. There will be two URL provided, https and SSH.
For this demo, I am using https URL. The URL format will be something like
For this demo, I am using https URL. The URL format will be something like
https://YOURUSERNAME@bitbucket.org/YOURUSERNAME/YOUR_REPOSITORY_Name.git
In Eclipse, at upper right corner, change the perspective to “Git Repository Exploring” as shown in below image.
You will get the list of all Local Repository in this perspective. Now in Remotes, right click to create new Remote as shown in below image.
One new popup will appear like below
First select the option “Push” and give name of remote repository Name. “Push” means you are going to save changes on remote repository and “Fetch” means you are going to fetch code from remote repository.
When you will click on Ok button new window will open.
When you will click on Ok button new window will open.
In new window click on “Change” (1) button as shown in above image. Again new pop up window will open. At location 2 provide your complete Git https URL. At location 3 provide your username and password. Don’t forget to check the box at location 4 which says “Store in Secure Store”. Now click on Finish Button.
On Parent window click on Add (1) button. In new window, in Local branch text box and Remote branch enter “refs/heads/master” or you can press “Ctrl + Space Bar”.
After everything, click on “Dry-run” button to check, whether everything is properly configured or not? If everything is properly configured then it will show the repository name else it will show the error message.
Now click on “Save and Push” button to move changes from local repository to remote repository.
Question : Why I am getting an error something like “Git Push Failed , Non-fast forward updates were rejected“.
Solution : Mostly in my case, I am getting this error if you are trying to push something to remote server first time. Simplest way to resolve this error is to Fetch (Pull) first and then Push.
In the same way you can configure “fetch” to get the code from remote repository.
I hope it will be help full for learners. I am waiting for your feedback. In next article I will show how to connect using SSH instead of https protocol.
Creating First Application in Heroku using Eclipse
Hello Readers, in this article we will see step by step procedure to deploy your first (Hello World) program in Heroku. There are two ways, first going through series of Commands of “Heroku” and “GIT” and other simple and smart way is to use Eclipse Capability with Heroku. To make things easier we will go by second method.
Prerequisites:
- Heroku Account, you can sign up here – https://api.heroku.com/signup/devcenter
- Eclipse 3.7 or Higher
- E-Git Plugin , more information on set up of E-Git –http://shivasoft.in/blog/salesforce/salesforce-git-eclipse-egit-better-and-distributed-source-control/
Installation and Set up:
First we will need to Install Heroku Plugin in Eclipse. To install it, Navigate to “Help | Install New Software” and click on “Add” button.
First we will need to Install Heroku Plugin in Eclipse. To install it, Navigate to “Help | Install New Software” and click on “Add” button.
Give any name in website like “Heroku Plugin” and enter this URL in Location https://eclipse-plugin.herokuapp.com/install and click on “Ok” and Finish Installation. It will install the Heroku Plugin required to start your first Plugin.
Next step is setting up your Credentials for Heroku :
Navigate to “Window | Preferences | Heroku”, Here either enter your UserName and Password of Heroku Account and click on “Login” or enter you API key, which you can find by logging into your Heroku Account.
Creating SSH Key :
Heroku uses Git to push and pull code from Heroku. For this you can use E-Git plugin of Eclipse. You can also visit this article for complete tutorial. So, for communication between Heroku and Git we need SSH. Navigate to “Windows | Preferences |General | Network Connections | SSH2 “. Navigate to “Key Management“ tab and click on “Generate RSA Key”. After Generation of key click on “Save Private Key”.
Now, after generation of RSA key Navigate to “Heroku” and click on “Load SSH Key” and select file which you have saved recently.
Now, till this we have done all the configurations. Start with creating our first “Hello World” program from Template using Jetty.
Navigate to “New | Project | Heroku | Create Heroku App from Template” and click on Next.
Navigate to “New | Project | Heroku | Create Heroku App from Template” and click on Next.
On Next screen select “Embedded Jetty-Servlet application” and click on Finish. After creation of sample project, open “Main.java” file and run it.
Now open browser with URL http://localhost:8080/ and you should able to see Jetty Page. If you are able to see the Jetty Startup page, means everything is good. Now navigate tohttp://localhost:8080/hello , you will see text “Hello Heroku” just like below screen.
You can make modifications in “HelloServlet.java” by replacing below line with some other Text.
1 | <br />out.write( "Hello Heroku" .getBytes());<br /> |
Deploying Modifications to Heroku from Local System:
Now, as we are done with making modifications in local code, commit the changes in Git by“Right Click on Project | Team | Commit”. You need to Enter Commit Message like this:
After Committing the Changes, Navigate to “Right Click on Project | Team | Push to Upstream”.
Now, if everything is good, Heroku will receive all you local Changes and build application again on its server.
Now you can run this application by logging into Heroku and make modifications that’s suits you like Salesforce Integration or any other.
You can also visit this official Heroku tutorial, which explains in very detail about Heroku Deployment using Eclipse and other important Configurations.
If you are getting an error “eclipse SSH key is not matching the SSH key(s) that is associated with your Heroku account“, then follow this article to resolve this.
No comments:
Post a Comment