Friday, 13 October 2017

Learning Times - Or Git being a bit of a Git

I'm on a bit of a voyage of discovery this week, having previously been tinkering with Elasticsearch and Kibana a few days back: -

Further stories of a tinkerer - this time it's IBM BPM, Apache Kibana and Elasticsearch

Now I'm playing with Jenkins and Git, the latter of which is running on Docker, with the intention of automating the push of Java code to WebSphere Liberty Profile, also running in Docker.

I also intend to get to grips with Maven at the same time, what's not to like ?

Anyway, back to Git and Jenkins.

I started the Docker container hosting Git as follows: -

git=`docker run -d -p 2222:22 -v ~/git-server/keys:/git-server/keys -v ~/git-server/repos:/git-server/repos jkarlos/git-server-docker`

So I've created a Git repository on a Docker image running on one of my Macs, which is accessible via SSH, and then created a basic Jenkins workflow to pull some Java source (, of course ) from Git, compile it, and execute it.

The next step will then be to push the compiled code to the Liberty container, and execute it. But that's the future …

So this is how I created the Git repository on the remote Mac: -

Make a directory for the myrepo.git repository - note that the path is actually a volume that's used by the Docker container ( see above ) : -

mkdir ~/git-server/repos/myrepo.git

Change to that directory

cd ~/git-server/repos/myrepo.git

Git initialise the repository

git init --shared=true

Add the contents of the current directory to the repository

git add .

Commit the first change

git commit -m "my first commit"

Copy in my source Java from ~/Desktop

cp ~/Desktop/ .

Add the Java source to the repository

git add

Commit the change

git commit -m "Hello World"

Having done this, I then cloned the repository to my main Mac, in order to allow me to work on my Javacode even when remote from the remote Mac: -

Clone the remote repository

Change to the newly created clone repository

cd ~/myrepo

Create a Readme file

touch Readme

Add the Readme file to the remote repository

git add Readme

Edit the Java source


Add the updated Java source to the remote repository

git add

Commit the changes

git commit -m "Adding changes"

Push the updates

git push

Enter passphrase for key '/Users/davidhay/.ssh/id_rsa': 
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 368 bytes | 368.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error: 
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error: 
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To ssh://
 ! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'ssh://git@'

At this point, I reverted to normal, and dived into Google for answers.

Eventually, I found this: -

This error is showing up because you didn't create the original repo as a bare repository, thus it's "protected" in a way you can't just push to it.

You can do as the error message says and set receive.denyCurrentBranch in the server side, where this repository is originally located. You can do this by navigating to the repository in the original location and running:

git config receive.denyCurrentBranch ignore

So, if I understand correctly, when I created the original repository, on the remote Mac, I should've specified that it be "bare" :-)

I tried the circumvention on the remote Mac: -

cd ~/git-server/repos/myrepo.git
git config receive.denyCurrentBranch ignore

and then tried the git push again: -

git push

Enter passphrase for key '/Users/davidhay/.ssh/id_rsa': 
Counting objects: 4, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (4/4), 368 bytes | 368.00 KiB/s, done.
Total 4 (delta 1), reused 0 (delta 0)
To ssh://
   894abb8..ea975c5  master -> master

This time it worked … the updated files ( Readme and ) appear in the remote repository.

Even better than that, my Jenkins workflow works …

In the next post, I will talk about the Jenkins -> remote Git plumbing, a problem that I hit with SSH certificates, and the workflow …

No comments: