Installing Gogs Git Server on OpenShift and make it using WebHooks to trigger builds

Preparation

Based on the following github.com project, we are going to set up a Gogs Git-Server on our local OpenShift Environment, which we have set up here:

http://www.opensourcerers.org/setting-enterprise-openshift-3-5-platform-macos-virtualbox/

https://github.com/OpenShiftDemos/gogs-openshift-docker

As we want to reuse our Gogs Server for some situations, we need to use the persistent version of the Template. First of all we need to have 2 persistent volumes:

$ cat <<-EOF | oc create -f -
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0001
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteOnce
  nfs:
    path: /var/exports/openshift/pv0001
    server: 192.168.56.11
  persistentVolumeReclaimPolicy: Recycle
EOF

$ cat <<-EOF | oc create -f -
apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv0002
spec:
  capacity:
    storage: 1Gi
  accessModes:
  - ReadWriteOnce
  nfs:
    path: /var/exports/openshift/pv0002
    server: 192.168.56.11
  persistentVolumeReclaimPolicy: Recycle
EOF

We then need to create a new project where we are going to serve Gogs from:

$ oc new-project ci

And finally we are creating the new application:

$ oc process -f http://bit.ly/openshift-gogs-persistent-template --param=HOSTNAME=gogs.ci.shift.ocp.lan  —param=GOGS_VERSION=0.11.4 | oc create -f -

Please have a look here to see which version of Gogs is available for use in OpenShift:

https://hub.docker.com/r/openshiftdemos/gogs/tags/

Once Gogs is up and running successfully, try to access it via Browser: http://gogs.ci.shift.ocp.lan/

Configure Gogs

If that works, go to openshift Web UI, login as user who has created the Gogs project, select into the Gogs project and go to Resources —> Config Maps —> gogs-config

Click on Actions —> Edit

And edit the following entries:

  • Set INSTALL_LOCK to False
  • Set SKIP_TLS_VERIFY to True

While the first property tells Gogs to provide an /install URL, the second property tells Gogs to ignore that we are using self signed certificates. Otherwise, Gogs would not be able to access the OpenShfit API.

After ‘Saving’ the config map, trigger a redeployment of the Gogs system (only Gogs, not the gogs-postgresql service). This makes sure that Gogs has the new parameters in use.

Now visit the URL gogs.ci.shift.ocp.lan again and Gogs will automatically send you over to the /install URL and lets you specify certain parameters for installation.

You have to specify database password and you should create a new user (which is automatically an admin user then). I called my user ‘gogs’ and provided a password ‘gogs’.

Click on ‘Save’ and you’ll be redirected to the overview page of Gogs.

If that all works, you should now go back to OpenShift and edit the gogs-config Config Map again to change INSTALL_LOCK to become True again (otherwise, you would always end up in the configuration page, once you’ve restarted Gogs. Or OpenShift).

Creating a new Gogs-Repository and let OpenShift be triggered by Gogs

Go to Gogs and create a new repository. I called mine ‚Sinatra‘ as I’d like to use the simple application called ‚simple-openshift-sinatra-s2i‘:

https://github.com/openshift/simple-openshift-sinatra-sti.git

Go and clone it to your desktop and add the files to your newly created repository inside Gogs. Now go to OpenShift, login via developer user and do the following:

$ oc new-project sinatra
$ oc new-app http://gogs.ci.shift.ocp.lan/gogs/sinatra.git
$ oc status

After a while you should see a new app up and running. Go and test it. Now we want to use the generated Generic WebHook URL in Gogs to trigger a new Build in OpenShift. For this either do an:

$ oc describe bc/sinatra

And you’ll see something like this:

Wanjas-MBP:devel wanja$ oc describe bc/sinatra
Name: sinatra
Namespace: sinatra
Created: 17 hours ago
Labels: app=sinatra
Annotations: openshift.io/generated-by=OpenShiftNewApp
Latest Version: 2
Strategy: Source
URL: http://gogs.ci.shift.ocp.lan/gogs/sinatra.git
From Image: ImageStreamTag openshift/ruby:2.3
Output to: ImageStreamTag sinatra:latest
Build Run Policy: Serial
Triggered by: Config, ImageChange
Webhook GitHub:
URL:https://master:8443/oapi/v1/namespaces/sinatra/buildconfigs/sinatra/webhooks/CD44sGonn6RuF20wiK-1/github

Webhook Generic:
URL:https://master:8443/oapi/v1/namespaces/sinatra/buildconfigs/sinatra/webhooks/yW0PSrCU2PoIVKC0XYBZ/generic

AllowEnv:false
Build Status Duration Creation Time
sinatra-2 complete 28s 2017-08-28 18:52:10 +0200 CEST
sinatra-1 complete 1m13s 2017-08-28 18:48:52 +0200 CEST

Just take the URL of „WebHook Generic“ and use it inside Gogs to trigger the build once there is a new push into this repository.

That’s it. You’ll now be able to do automated test builds on every commit (=push).

Thank you for reading.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: