Openshift Posts

Running A Simple Java Application CI/CD with Jenkins and Openshift

So basically im trying to create a simple CI/CD using Jenkins which runs on top of Openshift. It will do a very simple thing, fetching code from Github, and deploy it automatically to Openshift platform.

For this example, im using my previous Github repository which located at https://github.com/edwin/hello-world. It’s a very simple spring boot app, open an API and shows “hello world”.

But first, lets prepare our Jenkins instance on Openshift.

Once done, we can see Jenkins Dashboard.

And add Maven to Jenkins, on Manage jenkins > Global Tool Configuration

For this example, i want to deploy the app on a different Openshift project (eee project) compare to Jenkins which located on Fuse project. Therefore i need to create a service account specifially for Jenkins to deploy.

Create a simple pipeline item on Jenkins,

Which are triggered by a Poll SCM,

And after that, we can create a simple pipeline script for building the code. Changing project location to “eee”, and deploy it accordingly.

def gitRepo="https://github.com/edwin/hello-world.git"
def branch="master"

pipeline {
  agent any
  tools {
    maven 'M3'
  }
  stages {
    stage('Preparing'){
        steps{
            git branch: branch, url: gitRepo
        }
      }
    stage('Build and Deploy') {    
        steps {
            sh 'oc project eee'
            sh 'mvn -B clean fabric8:deploy'
        }
    }
  }
}

Simple isnt it? ;)

Google+

How to Deploy Source Code from Local Folder to Openshift Using S2I Build

First lets create a simple PHP code, and name it index.php

<?php
echo "hello world";
?>

We want to deploy it on top of PHP 7 images, and make it online on Openshift Platform.

So how to do it basically consist of 3 steps.
1. First we need to create a new binary build, using preferred image stream as its base image

oc new-build --name=my-php --image-stream=php:7.0 --binary=true

2. Next is start building image using sourcecode’s directory

oc start-build my-php --from-dir=.

3. Last is create application using previously built image

oc new-app my-php --name=my-php

So simple right :)

Google+

Backup All Openshift Template and Restore it to Another Openshift Instance

Previously i have to move a lot of Openshift template from one instance to another instance. So instead of copying a template one by one, why not just dump the whole template and import it to the new instance.

Below is the screenshot of source Openshift where we want to export the template from.

 
oc get -n openshift -o yaml --export templates > mytemplate.yaml

Below is the screenshot of the target Openshift,

oc apply -f mytemplate.yaml

This is the end result,

Google+

Creating A Simple Openshift Template

An Openshift Template is, as its name, a template on creating an application deployment. We can define url, imagestream location, service, and other things. We’ll start on a very simple json template which will took a specific image from docker hub, and deploy it to Openshift and generating URL on the fly automatically.

{
  "kind": "Template",
  "apiVersion": "v1",
  "metadata": {
    "name": "create-quarkus-template",
    "annotations": {
      "description": "This example shows how to create a simple template in openshift 3.11",
      "tags": "Quarkus"
    }
  },
  "objects": [
    {
      "kind": "Service",
      "apiVersion": "v1",
      "metadata": {
        "name": "my-quarkus"
      },
      "spec": {
        "ports": [
          {
            "name": "web",
            "protocol": "TCP",
            "port": 8080,
            "targetPort": 8080,
            "nodePort": 0
          }
        ],
        "selector": {
          "name": "my-quarkus"
        },
        "type": "ClusterIP",
        "sessionAffinity": "None"
      },
      "status": {
        "loadBalancer": {}
      }
    },
    {
        "kind": "Route",
        "apiVersion": "v1",
        "metadata": {
          "name": "my-quarkus"
        },
        "spec": {
          "to": {
            "kind": "Service",
            "name": "my-quarkus"
          },
          "tls": {
            "termination": "edge"
          }
        }
    },
    {
      "kind": "ImageStream",
      "apiVersion": "v1",
      "metadata": {
        "name": "my-quarkus"
      },
      "spec": {
        "dockerImageRepository": "edwinkun/my-quarkus"
      },
      "status": {
        "dockerImageRepository": ""
      }
    },
    {
        "kind": "DeploymentConfig",
        "apiVersion": "v1",
        "metadata": {
          "name": "my-quarkus",
          "annotations": {
            "template.alpha.openshift.io/wait-for-ready": "true"
          }
        },
        "spec": {
          "strategy": {
            "type": "Rolling",
            "rollingParams": {
              "updatePeriodSeconds": 1,
              "intervalSeconds": 1,
              "timeoutSeconds": 120,
              "pre": {
                "failurePolicy": "Abort",
                "execNewPod": {
                  "command": [
                    "/bin/true"
                  ],
                  "env": [
                  ],
                  "containerName": "my-quarkus"
                }
              },
              "post": {
                "failurePolicy": "Ignore",
                "execNewPod": {
                  "command": [
                    "/bin/true"
                  ],
                  "env": [
                  ],
                  "containerName": "my-quarkus"
                }
              }
            },
            "resources": {}
          },
          "triggers": [
            {
              "type": "ImageChange",
              "imageChangeParams": {
                "automatic": true,
                "containerNames": [
                  "my-quarkus"
                ],
                "from": {
                  "kind": "ImageStreamTag",
                  "name": "my-quarkus:latest"
                }
              }
            },
            {
              "type": "ConfigChange"
            }
          ],
          "replicas": 3,
          "selector": {
            "name": "my-quarkus"
          },
          "template": {
            "metadata": {
              "labels": {
                "name": "my-quarkus"
              }
            },
            "spec": {
              "containers": [
                {
                  "name": "my-quarkus",
                  "image": "my-quarkus",
                  "ports": [
                    {
                      "containerPort": 8080,
                      "protocol": "TCP"
                    }
                  ],
                  "env": [
                  ],
                  "resources": {},
                  "terminationMessagePath": "/dev/termination-log",
                  "imagePullPolicy": "IfNotPresent",
                  "securityContext": {
                    "capabilities": {},
                    "privileged": false
                  }
                }
              ],
              "restartPolicy": "Always",
              "dnsPolicy": "ClusterFirst"
            }
          }
        },
        "status": {}
      }
  ],
  "labels": {
    "template": "my-quarkus-dockerbuild"
  }
}
Google+