Kohei Nozaki's blog 

Entries tagged [git]

Configuring automatic push by successfully build


Posted on Sunday Mar 01, 2015 at 11:19PM in Jenkins


Making Jenkins to push to an another remote repository if build finishes successfully.

Recipe

  1. Create a bare repository

    $ mkdir trader-stable.git
    $ cd trader-stable.git
    $ git init --bare
    Initialized empty Git repository in /Users/Shared/trader-stable.git/
    $
  2. Click Add Repository

    4a49f571 4135 43a8 915f e96db6eb902b
  3. Enter Repository URL

    e33f505c a52b 4752 83f9 44bf45b9db47
  4. Click Advanced…​

    507dc8b1 d86b 4cde b24c b59da15e84ed
  5. Enter stable to Name

    0f54d0a9 5317 40f5 9c60 ee13804a9904
  6. Click Add post-build actionGit Publisher

    44eeab12 4cf2 4c4a 81a9 785b99514659
  7. Check Push Only If Build Succeeds

  8. Click Add Tag

    60946cca e837 4245 a2c8 8e74c98c1f23
  9. Enter $BUILD_NUMBER to Tag to push

  10. Check Create new tag

  11. Enter stable to Target remote name

  12. Click Save

    2a9e7146 7663 414f 9499 91b65e028a43

Test

$ pwd
/Users/kyle/tmp/trader
$ echo 'push if succeeds test' >> hi.txt
$ git commit -am 'push if succeeds test'
$ git push origin master

Jenkins said Pushing tag 8 to repo stable

3e1e0e9a 2732 4054 8a20 82da0f67a7ab
$ git remote add stable /Users/Shared/trader-stable.git
$ git fetch stable
remote: Counting objects: 1, done.
remote: Total 1 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (1/1), done.
From /Users/Shared/trader-stable
 * [new tag]         8          -> 8
$


Configuring automatic build by push for git repository


Posted on Sunday Mar 01, 2015 at 07:15PM in Jenkins


We can start the build by send request to following URL.

http://YOURHOST/jenkins/job/PROJECTNAME/build

Recipe

In my case, Jenkins is deployed in /, and the server is running on port 18080. so the command to start a build of the job named trader will be:

curl http://localhost:18080/job/trader/build

So put following shell script into $ORIGIN_BARE_REPOSITORY/hooks/post-receive and execute chmod +x post-receive.

#!/bin/sh
curl http://localhost:18080/job/trader/build

Testing

Push some modification as follows:

$ echo hook test >> hi.txt
$ git add hi.txt
$ git commit -m 'hook test'
$ git push origin master

Check the build was run automatically as expected, and the output.

b0731e82 30d0 4638 b412 1e1bba91d3c9


Creating a simplest Jenkins job against a git repository


Posted on Sunday Mar 01, 2015 at 05:45PM in Jenkins


  1. Click New Item

    ecdf88ac ba06 4f30 aaa6 889b72d7a6be
  2. Enter trader in Item name and select Build a free-style software project

    81952af0 a4ae 40d7 a387 e02647d31f23
  3. Select Git and enter Repository URL

    9ac2c2af 2a4a 4b4d 90b4 7eed0a73433e
  4. Click Add build step - Execute shell

    dc7e02f0 9e6f 47ed 8049 e7a1a3078c11
  5. Enter cat hi.txt into Command

    95537b80 e9a0 4e42 89df d8dfb548de90
  6. Click Save

    659ed88d 0455 400c 91e4 f4b3ab714ea3
  7. Click Build Now

    9d4ef446 7b26 4a7a a2a2 010268e05e84
  8. Click a Build History which just created by build

    44f32959 4d18 4a89 8895 c35c1861be51
  9. Click Console Output

    f3da0b60 f9a2 4963 a12f 4fc3a8c1f392


Creating a shared git repository on local filesystem


Posted on Sunday Mar 01, 2015 at 05:04PM in Git


Tested with OS X 10.9.5.

Requirement

There are 2 users (kyle and jenkins). they want to have a shared git repository named trader on local filesystem. both of them can push changes into the repository.

Recipe

  1. Create a group named trader

    $ sudo dseditgroup -o create trader
  2. Let join kyle nad jenkins into the group trader

    $ sudo dseditgroup -o edit -a kyle -t user trader
    $ sudo dseditgroup -o edit -a jenkins -t user trader
  3. Create a bare repository

    $ cd /Users/Shared
    $ mkdir trader.git
    $ cd trader.git
    $ git init --bare --shared
  4. Change owning group of trader.git to trader

    $ cd ../
    $ chown -R :trader trader.git
  5. Make the repository writable by users in trader group

    $ chmod -R g+w trader.git

Testing

  1. Clone the repository

    $ git clone /Users/Shared/trader.git
  2. Make first commit by kyle

    $ cd trader
    $ echo 'hi there' > hi.txt
    $ git add hi.txt
    $ git commit -m 'first commit from kyle'
  3. Push to the parent repository

    $ git push origin master
  4. Switch to user jenkins

    $ sudo su - jenkins
  5. Clone the repository

    $ git clone /Users/Shared/trader.git
  6. Make first commit by jenkins

    $ cd trader
    $ echo hi there from jenkins >> hi.txt
    $ git add hi.txt
    $ git commit -m 'first commit from jenkins'
  7. Push to the parent repository

    $ git push origin master
  8. Pull the change made by jenkins from user kyle

    $ whoami
    kyle
    $ cat hi.txt
    hi there
    $ git pull origin master
    $ cat hi.txt
    hi there
    hi there from jenkins


Using rebase


Posted on Sunday Mar 09, 2014 at 08:17PM in Technology


Make multi commits to one commit with rebase

Commit

kyle-no-MacBook:hello kyle$ echo commit1 >> README.md
kyle-no-MacBook:hello kyle$ git commit -am 'commit1'
[b4 2447554] commit1
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ echo commit2 >> README.md
kyle-no-MacBook:hello kyle$ git commit -am 'commit2'
[b4 50e8dee] commit2
 1 file changed, 1 insertion(+)
kyle-no-MacBook:hello kyle$ git log -2
commit 50e8deed471c20abc9b86eb1cb7d6b6af4c9fcfd
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 20:17:23 2014 +0900

    commit2

commit 2447554c8495ff5f407b4dffb84278840d7fdba6
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 20:17:15 2014 +0900

    commit1

Rebase

  • Exec this (number 2 means how many commits to be one commit):
git rebase -i HEAD~2
  • Editor launches with this text:
pick 2447554 commit1
pick 50e8dee commit2

# Rebase 9ccae64..50e8dee onto 9ccae64
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
  • Edit to:
pick 2447554 commit1
squash 50e8dee commit2
  • Another editor will launch so that we can edit commit message:
# This is a combination of 2 commits.
# The first commit's message is:

commit1

# This is the 2nd commit message:

commit2

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# HEAD detached at 2447554
# You are currently editing a commit while rebasing branch 'b4' on '9ccae64'.
#
# Changes to be committed:
#   (use "git reset HEAD^1 <file>..." to unstage)
#
#       modified:   README.md
#
  • Gone like this:
kyle-no-MacBook:hello kyle$ git rebase -i HEAD~2
[detached HEAD ada1147] commit1
 1 file changed, 2 insertions(+)
Successfully rebased and updated refs/heads/b4.
kyle-no-MacBook:hello kyle$ 
  • Log:
kyle-no-MacBook:hello kyle$ git log -2
commit ada114798c5727e8461afc36a10ad0b36e5e214a
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 20:17:15 2014 +0900

    commit1

    commit2

commit 9ccae643e8ceee10c5f7bcb4103857bc1a38cdd4
Author: lbtc-xxx <lbtc-xxx@example.com>
Date:   Sun Mar 9 19:09:56 2014 +0900

    cancel
kyle-no-MacBook:hello kyle$ 
  • README.md
kyle-no-MacBook:hello kyle$ cat README.md 
b4-local1
CANCEL
commit1
commit2
kyle-no-MacBook:hello kyle$