Jenkins on Kubernetes with private Git Repositories

Kurt Stam
2 min readSep 9, 2016

--

The default applications all the local gogs repository as their Git repository. If you want to use your existing Github or Bitbucket repository then this post is for you. There are a few place where you need to provide Jenkins with your credentials. The easiest credentials to use are a priv/pub key set for which the public key is already registered with the Git repository. You will need to add secrets in a few places.

First, click on ‘Create App’ and then ‘Import from git’ and for the git url use the one with the ‘git’ protocol, which uses ssh and priv/pub keys to access the git repository.

  1. Add a source secret. In the fabric8 console click on ‘Add new secret’, which brings up a dialog in which you can paste your private and public key. This key set is used by the fabric8 console to access the git repository.
  2. Secondly we need to update two secrets:

example jenkins-ssh-config

{“kind”: “Secret”,“apiVersion”: “v1”,“metadata”: {“name”: “jenkins-ssh-config”,“namespace”: “default”,“selfLink”: “/api/v1/namespaces/default/secrets/jenkins-ssh-config”,“uid”: “7bef4008–768a-11e6-b499–52540085f1fe”,“resourceVersion”: “89841”,“creationTimestamp”: “2016–09–09T12:39:43Z”,“labels”: {“group”: “io.fabric8.devops.apps”,“name”: “cd-pipeline”,“project”: “jenkins”,“provider”: “fabric8.io”,“version”: “2.2.243”}},“data”: {“config”: “<content>”},“type”: “fabric8.io/jenkins-ssh-config”}

where <content> is the content of the .ssh/config file in base64 encoding. It is important to point git to where the keys are as well as that the HostKey does not need checking:

Host bitbucket.orgUser gitIdentityFile /home/jenkins/.ssh-git/ssh-keyStrictHostKeyChecking no

Use ‘cat <content-file> | base64’ to turn this string into base64. Either use ‘oc edit’ of ‘oc delete secret jenkins-ssh-config’ and ‘oc create -f <jenkins-ssh-content-file>’.

example jenkins-git-ssh secret

{“kind”: “Secret”,“apiVersion”: “v1”,“metadata”: {“name”: “jenkins-git-ssh”,“namespace”: “default”,“selfLink”: “/api/v1/namespaces/default/secrets/jenkins-git-ssh”,“uid”: “85f5343e-768a-11e6-b499–52540085f1fe”,“resourceVersion”: “89849”,“creationTimestamp”: “2016–09–09T12:40:00Z”,“labels”: {“group”: “io.fabric8.devops.apps”,“name”: “cd-pipeline”,“project”: “jenkins”,“provider”: “fabric8.io”,“version”: “2.2.243”}},“data”: {“ssh-key”: “<ssh-key>”,“ssh-key.pub”: “<ssh-key.pub”},“type”: “fabric8.io/jenkins-git-ssh”}

These jenkins keys will be mounted inside the jenkins and the jenkins-jnpl containers.

Now you can select the pipeline you wish and it is able to interact with your private git repository.

--

--