Pages - Menu

Showing posts with label Source Control. Show all posts
Showing posts with label Source Control. Show all posts

Getting Started with Git 101

Scope

I have used many SCM in the past. VSS, SVN, TFS, ClearCase, Mercurial etc... There are so many of them yet they are so similar, so that they are not even worthy for a spot in a resume.

However, git was a little more challenging to me as their structures and architectures are different. I have now used git for just over a year now, and put the followings together that covered what I believe is a good starting point to learn git commands.

Technical

Config

I use posh-git and found the default dark red color was a bit hard to read against a dark background color in Windows console. I changed them to yellow and magenta by updating the ~/.git/config.

[color]
    ui = true 
[color "status"]
    changed = magenta bold
    untracked = yellow bold

Settings

# Change a global setting
$ git config --global --edit

# Change editor to notepad globally
$ git config --global core.editor notepad

# Setup git diff / merge tool globally
# for example, if we are using p4merge as a diff / merge tool.
$ git config --global diff.tool p4merge
$ git config --global merge.tool p4merge

# Git merge generates unwanted .orig file
$ git config --global mergetool.keepBackup false

Basic Changes

# Check pending check-in status
$ git status

# Get latest files
$ git pull

# Change branch
$ git checkout <branchName>

# Add files for pending check in
$ git add <filename>

# Undo a git add
$ git reset <filename>

# Delete files for pending check in
$ git rm <filename>

# Undo pending delete files 
$ git reset head <filename>

# Amend last commit
$ git commit --amend

# Undo commit
# This will reset the the branch to a previous commit 
$ git reset HEAD~

# Hard reset is a potentially dangerous command
# Changes are destructive and may not be recovered
$ git reset --hard <commit-id>

# a force push will force the origin to point to the same commit as local
$ git push origin HEAD --force

# Discard changes in working directory
$ git checkout <filename>

# Discard untracked files in working directory
# Double check what to delete
$ git clean -f -n

# The actual deleting
$ git clean -f 

# Discard untracked folders in working directory
$ git clean -df 

Stash


It is similar to shelve in TFS.

# All unstaged and staged dirty files will be "stashed", 
# and the working directory will be cleaned.
$ git stash

# shows the list of stash
$ git stash list

# shows content of stash
$ git stash show -p

# retrieve then remove changes from the stash 
$ git stash pop

# apply changes (and not removing) from the stash
$ git stash apply

# remove changes from the stash
$ git stash drop

# remove all stash history
$ git stash clear

Branch

# Delete a local branch
$ git branch -d <branchName>

# Delete a remote branch
$ git push origin --delete <branchName>

# Rename current branch
$ git branch -m <newname>

Merge

# Merge a branch from source to destination
$ git checkout destination-branch
$ git merge source-branch

# Resolve a merge conflict
$ git mergetool

# Resolve merge conflict with theirs or ours preference during a conflicted state.
# Take their changes
$ git checkout --theirs *
$ git add *

# Take our changes
$ git checkout --ours *
$ git add *

Tag



# listing tags
$ git tag
  
# add tag
$ git tag -a <tagName> -m "A message for tagging"
  
# push local tags to remote
$ git push origin --tags

# branch out from a tag
$ git checkout -b <newBranchName> <fromTag>

Rebase

# Rebase branch from parent branch
$ git rebase <parentBranch>

# Conflict during rebase
$ git rebase --[abort|skip|continue]

Fork and Submodule

# Add a remote repo to current repo as a subfolder
$ git submodule add <gitRepo>
  
# Get latest in submodule
$ git submodule update

Bitbucket Continuous Integration with Bitbucket Pipelines

Scope

We use Bitbucket as our SCM and other few Atlassian products in our development team. Happy to say that I am pleased with the tools that it made our daily works very productive and hassle free. 

Recently, I am looking into a CICD solution for our deployment process. I have already previously Setting Up Jenkins for GitHub, Setting Up Octopus Deploy for Jenkins with nopCommerce Projects or Setup Continuous Integration with Visual Studio Online. I like the design of Octopus Deploy, but given we are now happily living with our Atlassian suite, I want to see what the Bitbucket Pipelines (formerly Bamboo) can offer me. 

I just signed up for the Bitbucket Pipelines Beta and already received the beta invitation within about 6 hours :)




Goal

  • Setup and integrate Bamboo with Bitbucket.
  • Explore possibility to automatically run the Demandware Grunt Build Suite when we commit to master.

Setup

I clicked the link to install the Bitbucket Pipelines add-on.
Went through a simple setup procedure and enabled it in my repository.


It adds a Pipelines link in my repository.

Going through the wiki to configure the bitbucket-pipelines yml.

It is essential to understand the Docker Image used by Bitbucket pipelines. The default image uses ubuntu and is good enough in our scenario.

In the setting tab, we are able to setup environment variables for username and password.


We created a bitbucket-pipelines.yml script in the root folder.



Check-ins to the master will trigger the Bamboo to run the build job and script.



Thoughts

I have not had much chance to explore further, but it looks quite promising and does what I wanted. I like the fact that it is tightly integrated with our bitbucket so I do not need to login to another service, the setup was simple and straightforward.

"Build servers build" - Bitbucket Pipeline is a CI server and not a CICD server. We will leverage Bitbucket Pipelines for our continuous integration and our Demandware Business Manager for deployment purpose. I am pleased with the Bamboo Pipelines, this should work well and fits in our CICD strategy.



Git - How to Handle Emergency Deploy to Live Environment

Master always have our latest changes in our development and could be dirty and not production worthy. In order for us to do an emergency stable deploy to our live environment, it can be achieved as follow.


  1. Suppose we have our Master M. On our last well known tag called mytag deployed to our live environment.
  2. Subsequently C1 and C2 changes are commited to our Master.
  3. And, we have an emergency deployment required for C3 change that needs to be pushed to live with out C1 and C2.
This can be achieved by the following.


# branch out to master-e from mytag
$ git checkout -b master-e mytag

# After committing change C3 to our new branch, we are then good for live deployment.
# Last thing to do is to apply C3 to Master Origin, so that our next deploy will have C3
# This can be achieved by a simple merge.
$ git checkout master
$ git merge master-e

The new branch master-e is then kept until the next master deploy, if we need other emergency deploys before our next master deploy, we can reuse master-e.

Contribute to Mercurial in Simple Steps

Mercurial

Mercurial is a distributed source control management tool / system that allow developers around the globe to collaborate source codes. Recently engaged in an ecommerce project that utilize nopCommerce framework, which is an open source project. I found a little bug in there and made my very first contribution in open source. The nopCommerce is hosted on www.codeplex.com

Simple Steps

  1. Logon to CodePlex.
  2. Create a fork for the project
  3. Clone the fork
  4. Make local modifications.
  5. Check-in and commit to the local repository.
  6. Push changes from local fork to the remote server. In my case, it is https://hg.codeplex.com/forks/swon/austposthandlingchargefix
  7. After all changes are completed and checked. Create a pull request to merge the fork back to the project.

SVN Convention

Disregard the differences of the underlying technology how distributed source repository and non-distributed (classic?) repository works, a fork is basically a branch in SVN. Once you branch off, you can make local modifications that you do not conflict with the trunk (default). When you are done with the fork, you are then required to merge back and is done by the "pull request". In an open source environment, there will be some core developers of the project with higher access that will merge the changes.

Setup Jenkins on TFS 2008 for MSBuild project

Installation

Download TFS plugin from https://wiki.jenkins-ci.org/display/JENKINS/Team+Foundation+Server+Plugin

Download MSBuild plugin from https://wiki.jenkins-ci.org/display/JENKINS/MSBuild+Plugin

You may go to the jenkins-ci wiki page to download the plugin manually or it can be downloaded via Manage Plugin page, either way is fine.

Install plugin under Manage Jenkins.






Now TFS is available as an option in Source Code Management.



Plugin can also be found and installed by using the plugin manager.



Troubleshooting during installation


The following error maybe encountered if TF.exe is not installed or path to access TF.exe is not configured in Jenkins.
Started by user anonymous
Building in workspace C:\Program Files (x86)\Jenkins\jobs\XXX\workspace
[workspace] $ tf workspaces -format:brief -server:tfs.server.com ********
java.io.IOException: Cannot run program "tf" (in directory "C:\Program Files (x86)\Jenkins\jobs\XXX\workspace"): CreateProcess error=2, The system cannot find the file specified
 at java.lang.ProcessBuilder.start(Unknown Source)
 at hudson.Proc$LocalProc.<init>(Proc.java:244)
 at hudson.Proc$LocalProc.<init>(Proc.java:216)
 at hudson.Launcher$LocalLauncher.launch(Launcher.java:773)
 at hudson.Launcher$ProcStarter.start(Launcher.java:353)
 at hudson.plugins.tfs.TfTool.execute(TfTool.java:99)
 at hudson.plugins.tfs.model.Server.execute(Server.java:47)
 at hudson.plugins.tfs.model.Workspaces.getListFromServer(Workspaces.java:38)
 at hudson.plugins.tfs.model.Workspaces.populateMapFromServer(Workspaces.java:50)
 at hudson.plugins.tfs.model.Workspaces.exists(Workspaces.java:76)
 at hudson.plugins.tfs.actions.CheckoutAction.checkout(CheckoutAction.java:36)
 at hudson.plugins.tfs.TeamFoundationServerScm.checkout(TeamFoundationServerScm.java:175)
 at hudson.model.AbstractProject.checkout(AbstractProject.java:1408)
 at hudson.model.AbstractBuild$AbstractBuildExecution.defaultCheckout(AbstractBuild.java:676)
 at jenkins.scm.SCMCheckoutStrategy.checkout(SCMCheckoutStrategy.java:88)
 at hudson.model.AbstractBuild$AbstractBuildExecution.run(AbstractBuild.java:581)
 at hudson.model.Run.execute(Run.java:1603)
 at hudson.model.FreeStyleBuild.run(FreeStyleBuild.java:46)
 at hudson.model.ResourceController.execute(ResourceController.java:88)
 at hudson.model.Executor.run(Executor.java:247)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
 at java.lang.ProcessImpl.create(Native Method)
 at java.lang.ProcessImpl.<init>(Unknown Source)
 at java.lang.ProcessImpl.start(Unknown Source)
 ... 20 more
Finished: FAILURE

The missing file can be downloaded in here.

http://www.microsoft.com/en-us/download/details.aspx?id=16338

Install in progress….



The following error maybe encountered if path to TF is not given.




The next step is to tell Jenkins where TF is. In Jenkins –> Configure System, depends on the version of .Net Framework, apply the path as follow.



Setup for MSBuild Project

Once the Jenkins is setup, by default it is only getting the files. The next step is to setup the MSBuild for the project.



In [Project] –> configuration –> Build –> Add build step, we will setup as follow.

Add build step will now have an option to use MSBuild.



As described in the jenkins-ci wiki page, we need to configure the MSBuild. A simple setup for .Net framework 4.0 as follow.


Trigger a build from check in

Perhaps, one of the most interesting feature of CI is the ability to continuously ensure the integrity of the build. Hence the name continuous integration. With the ability to trigger a build from any check-ins from developers, we have immediate feedback if any piece of check-ins causing troubles. This can be achieved by polling the SCM periodically. The online help with Jenkins is quite comprehensive, it follows the cron syntax.