0

I'm new to git and I am looking at what are the possibilities when integrating it into our CI (Jenkins) environment.

Based on several answers I found here at SO (mainly How to configure Git post commit hook), I understand that it a common wish (or practice) to configure git post-commit hooks to trigger your CI builds, instead of simply relying on SCM polling.

A post-commit hook is triggered when the user runs git commit. Since commiting (in git terminology) means commiting to your local repository, those changes are not yet available in the central repository (i.e., they haven't been pushed).

Assuming you have a central CI server, my question is: what is the use of "triggering your CI build" after commit, if the CI system cannot see those changes yet?

Community
  • 1
  • 1
E.Z.
  • 6,393
  • 11
  • 42
  • 69

1 Answers1

1

Assuming you have a central CI server, my question is: what is the use of "triggering your CI build" after commit, if the CI system cannot see those changes yet?

There are plenty of workflows to choose from. Trying to include a complete picture here:

# as a new member of your team
git clone https://your-repo your-repo
cd your-repo
git remote add ci-server https://ci-server
git checkout -b my-feature-branch ci-server/master
# code stuff
git add .
git commit -m "lala"
# code stuff
git add .
git commit -m "lala"
# ...
git push my-feature-branch ci-server
# rinse and repeat
git fetch ci-server/master
git checkout my-next-feature ci-server/master

Now, the ci-server reacts to the pushed commits, builds them, perform tests etc, and finally merges the commit to its master, which you as a developer pull/fetch later on (or use as a base for new feature branches as demonstrated above).

That's what I would call a minimal working example. Also see something like gitflow or Atlassians comparison of git branching models

chelmertz
  • 20,399
  • 5
  • 40
  • 46
  • Hi @chelmertz. The first call to `git commit -m "lala"` will trigger your post-commit hook (i.e. your the client-side hook - http://www.git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#Client-Side-Hooks - defined on your `.git/hooks/post-commit` shell script). The repository however doesn't know about your changes yet (you haven't pushed them!), so triggering a build seems pointless. A "post-push" hook would seem to do what you describe, but that doesn't exist (http://stackoverflow.com/a/1799624). – E.Z. Oct 16 '15 at 13:12
  • Hello @E.Z. From my experience (which may be wrong), I've always worked locally and in separate branches for each feature. Tests should be run locally if they could. Once I feel like it's good enough to be ready to share my code, I push it to the CI server. The CI server could use `post-receive`. See ["Server-Side Hooks" on git-scm.com](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#Server-Side-Hooks) – chelmertz Oct 16 '15 at 13:16
  • To expand a bit further: I play around with the code and may test a lot of different ways to solve issues. Sometimes I use TDD, sometimes I do small fixes that requires manual verification. So, running a complete test suite in between local commits doesn't sound too feasible for me. The "integration" part of CI is aimed at integrating your work with others work, not "test everything all the time" IMO. – chelmertz Oct 16 '15 at 13:19
  • So you don't really use the [client side] `post-commit` hook to trigger a CI build. For that you would use a [server side] `post-receive` hook, right? – E.Z. Oct 16 '15 at 13:41
  • Yeah, because my personal workflow involves a lot of "save this temp", "try new stuff" kind of commits, like an extended save button. Then I rebase them and merge some commits, and split some of them, so that they look good on their own. If you do want to force developers to test every commit (which can be perfectly fine, no judgement here) you could probably use post-commit to build a diff and send the diff somewhere. You can't tell the CI system to "test commit X" because you never pushed it anywhere, it's still local. The diff is however just a blob so CI could apply the blob and test it. – chelmertz Oct 16 '15 at 14:33