[OpenShift] – Understanding Jenkins Pipelines and how to integrate it on OpenShift.

You can implement via Jenkins a workflow with many steps which can be used to automate your CI and CD process. In OpenShift it is possible to see the pipelines as integrate it to run Openshift commands in the stages of this workflow.

Following a pipeline create in Jenkins to perform the stages for an application in OpenShift.

Screen Shot 2018-04-20 at 09.52.04

Following the same pipeline integrated with OpenShift.

Screen Shot 2018-04-20 at 17.15.16

Understanding how to create a Pipeline in Jenkins

## Go to Jenkins dashboard and click on “New Item”

Screen Shot 2018-04-20 at 17.29.55

## Select Jenkins Pipeline

Screen Shot 2018-04-20 at 17.30.13

## Configure your pipeline.

Note that Jenkins has a lot of triggers and plugins to help you with it. However, goes to the field Pipeline at the end of this page. This place you can implement a script in Groove to define the stages and steps of your workflow as you can check the Jenkins examples.

Screen Shot 2018-04-20 at 17.31.17

Each stage will be a box of your pipeline. You must define the steps that need to be done for each stage. Following an example to do a git checkout.

// Checkout Source Code from a git repository
stage('Checkout Source') {
  git credentialsId: 'bf466039-2e8f-435b-aa4c-ca173358bfb8', url: 'https://github.com/camilamacedo86/exampleShortPath.git'

## Common usage

  • To print a text in the execution logs of your pipeline.
stage('Build war') {
   echo "Building version ${devTag}"
   sh "${mvnCmd} clean package -DskipTests"
  • To define variables.
def devTag = "${version}-${BUILD_NUMBER}"
  • To execute commands in a UNIX environment using `sh` and for a windows `bat`.
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
  • To perform conditionals use theinput
input "Would you like to deploy it in production?"

## Use commands to perform actions in the OpenShift as the follows examples. 

// Deploy the development application.
openshiftDeploy depCfg: 'tasks', namespace: 'my-app-dev', verbose: 'false', waitTime: '', waitUnit: 'sec'
openshiftVerifyDeployment depCfg: 'tasks', namespace: 'my-app-dev', replicaCount: '1', verbose: 'false', verifyReplicaCount: 'false', waitTime: '', waitUnit: 'sec'
openshiftVerifyService namespace: 'my-app-dev', svcName: 'tasks', verbose: 'false'

## Openshift plugins :

You can use the commands specified in the following projects for OpenShit in the code implementation. Check the ReadMe of this projects to know the available resources.

## Use the Pipeline Syntax to get a help to implement your workflow

You can get a help from Jenkins to generated your steps and get the code implementations with this feature really easy.

Screen Shot 2018-04-20 at 18.05.08Screen Shot 2018-04-20 at 18.05.19Screen Shot 2018-04-20 at 18.06.53

For further information check the following documentation Jenkins Pipeline documentation

Understanding how to integrate a Jenkins Pipeline with OpenShift

## Add Pipeline to Source Code Repository

Add the Pipeline script to a repository as the following example. Screen Shot 2018-04-20 at 18.10.36Screen Shot 2018-04-20 at 18.10.58

## Create a trigger to start the pipeline 

You need to create a trigger to define when the pipeline will be started. For example, to start the process when the master branch is updated. It can be made in many ways.

In the GitHub go to the settings of the project and check the WebHooks, also it can be made via the Integrations & Services. Following few images to illustrate it.

Screen Shot 2018-04-20 at 18.18.54Screen Shot 2018-04-20 at 18.21.23Screen Shot 2018-04-20 at 18.20.13

For further information check  Customizing Git – Git Hooks documentation.

Following an example with Gogs to trigger any push made just against to the master branch and ignore the other branches of the repository. Also, the following example is using the Jenkins Token API to trigger the build.


while read oldrev newrev refname
  // get the branch name 
  branch=$(git rev-parse --symbolic --abbrev-ref $refname)
  // check if is master branch
  if [[ "$branch" == "master" ]]; then
     // Call the Jenkins endpoint/URL which will start to build the pipeline. It will be the same of click on the build button in the Jenkins UI.
     curl -k -X POST --user <jenkinsUserID>:<JenkinsUserIDTokenGenerated> http://<jenkinsOpenShiftSVC>.svc.cluster.local/job/<pipelineName>/build

NOTE: To use and the OpenShift token goes to `User >> Configure` and click on `Show API Token` then get the UserID and API Token to be used.

For further information about how to create triggers for Openshift check the Builds/Triggering docs.

## Create a Build Config to integrate the pipeline with the OpenShift web console

To do it is required to create a build config, bc, pointing to the Jenkinsfilein the repository. Following an example of a command with will create the file and will use it to create the build configuration in OpenShift.

echo "apiVersion: v1
- kind: "BuildConfig"
  apiVersion: "v1"
    name: "my-pipeline"
      type: "Git"
        uri: "https://github.com/camilamacedo86/exampleShortPath"
      type: "JenkinsPipeline"
        jenkinsfilePath: Jenkinsfile
kind: List
metadata: []" | oc create -f - -n <JenkinsNameProject>

For further information check Build OpenShift documentation






Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s