Kohei Nozaki's blog 

How to specify a Git tag to be processed with Jenkins


Posted on Friday Feb 21, 2014 at 08:35PM in Jenkins


Environment

  • Jenkins 1.551
  • Apache Maven 3.1.1
  • git version 1.8.3.4 (Apple Git-47)
  • Eclipse Kepler SR1
  • Oracle JDK7u51
  • OS X 10.9.1

Create a tag

  1. Open context menu of the project - Team - Advanced - Tag

  2. Enter tag name and Tag message, then click OK

  3. To check created tag, Open context menu of the project - Team - Show in Repositories View

  4. Confirm it appeared.

Modify a resource of HEAD

  • In order to check that build will processed with the tag specified, so just do some modify a resource of HEAD.
    • Make sure that you are in the master branch.
    • Procedure to switch: Team - Switch To - master
  • I have modified a resource that returns a message to Servlet. now it shows like that:
  • After modify, we have to commit it.

Install Git Parameter Plugin

I got trouble that the plugin not showing any tags, so I have stopped using this plugin. there are some people who reporting same problem[6]. If skipped this, we can't see the list of tags in the repository, but we can specify a tag manually.

This plugin makes easy that specify a tag to be processed.

Make a Jenkins job

  1. Copy a job that created in previous post
  2. Check “This build is parameterized”
  3. Click “Add Parameter”
  4. Click “Git Parameter”
  5. Enter “tag” to “Name”
  6. Enter “*/master” to “Branch”
  7. Find the section named “Source Code Management”
  8. Enter “${tag}” to “Branch Specifier”
  9. Click “Save”

Try Parameterized build

  1. Click this:
  2. Select a tag that created above
  3. Click “ビルド”

Check console output of Jenkins

ユーザーanonymousが実行
ビルドします。 ワークスペース: /Users/Shared/Jenkins/Home/jobs/DeploySpecifiedTag/workspace
Fetching changes from the remote Git repository
Fetching upstream changes from file:///Users/kyle/gits1/hellojenkins
Checking out Revision 9d88b2abe381c6e2c915bbbf0ddeec09119b6f04 (v1.0)
Parsing POMs
...
  • We can see that build was processed with tag named “v1.0”

References

  1. Can I get Jenkins to build a git tag from a passed in parameter? - Stack Overflow
  2. Jenkins and the Git Branch Selection - Sourceprojects.org
  3. Git Parameter Plugin - Jenkins - Jenkins Wiki
  4. Jenkinsで外部パラメータで与えたブランチを対象にビルドできるようにしておくと凄惨性あがって墓ドル - ( ꒪⌓꒪) ゆるよろ日記
  5. Hudson Growl Pluginでビルド結果をGrowlへ通知 | skmks
  6. Doesn't show any tags or revisions · Issue #2 · lukanus/git-parameter


How to make git commit to trigger run a Jenkins job


Posted on Friday Feb 21, 2014 at 06:02PM in Technology


Environment

  • Jenkins 1.551
  • git version 1.8.3.4 (Apple Git-47)
  • OS X 10.9.1

Try using curl

  • In this setup, there is no authentication at Jenkins.
curl -X POST http://localhost:18080/job/BuildAndTestHead/build
  • Let's try with dump-header option.
kyle-no-MacBook:Jenkins kyle$ curl -D - -X POST http://localhost:18080/job/BuildAndTestHead/build
HTTP/1.1 201 Created
Location: http://localhost:18080/queue/item/4/
Content-Length: 0
Server: Jetty(8.y.z-SNAPSHOT)

kyle-no-MacBook:Jenkins kyle$ 

Configure a hook of repository

  • Create a file named “post-commit” in .git/hooks directory.
kyle-no-MacBook:hooks kyle$ pwd
/Users/kyle/gits1/hellojenkins/.git/hooks
kyle-no-MacBook:hooks kyle$ ls -l
total 8
-rwxr-xr-x+ 1 kyle  staff  73  2 23 10:50 post-commit
kyle-no-MacBook:hooks kyle$ cat post-commit 
#!/bin/sh
curl -X POST http://localhost:18080/job/BuildAndTestHead/build
kyle-no-MacBook:hooks kyle$ 
  • Now it runs automatically when we commit using git command from cli.

Remarks

  • According to [3], It doesn't work when we commit with Eclipse.
  • So, we might have to config Jenkins to poll the repository periodically.

References

  1. Remote access API - Jenkins - Jenkins Wiki
  2. gitリポジトリの仕込み方 - はこべブログ ♨
  3. eclipse - EGit and post-commit hook - Stack Overflow