Some might regard Jenkins as only beautiful on the inside. But with OpenShift, the beauty also runs skin deep. Just like Snuggles. This lab is another in the OpenShift MiniLabs series.
Objectives
This demonstration will show how to set up a Jenkins service and pipeline in a dedicated OpenShift project known as CICD. We then create two separate projects labeled as Development and Testing. A pipeline is defined in the CICD project that first builds in Development “cats” version of the COTD application and then secondly tags the image and promotes these applications to Testing. Repeat the container CIC/CD mantra after me – build, tag, promote!
Setup
Initial Attempt
This tutorial assumes you have completed the OpenShift MiniLabs installation procedure. Then refresh before continuing.
Repeat Attempt
To reset your environment to repeat this tutorial do the following:
$ cd ~/containersascode $ ./oc-cluster-wrapper/oc-cluster up containersascode $ oc login -u developer -p developer $ oc delete project cicd $ oc delete project development $ oc delete project testing
Instructions
OpenShift Projects and Service Accounts
Now let’s set up some projects and service accounts which we can use later to host our Jenkins services to build and promote our applications.
$ oc login -u developer -p developer $ oc new-project cicd --display-name='CICD Jenkins' --description='CICD Jenkins' $ oc new-project development --display-name='Development' --description='Development Environment' $ oc policy add-role-to-user edit system:serviceaccount:cicd:jenkins -n development $ oc new-project testing --display-name='Testing' --description='Testing Environment' $ oc policy add-role-to-user edit system:serviceaccount:cicd:jenkins -n testing $ oc policy add-role-to-group system:image-puller system:serviceaccounts:testing -n development
Create Development Application
$ oc login -u developer -p developer $ oc project development $ oc new-app --name='cats' -l name='cats' php~https://bitbucket.org/emergile/cotd.git -e SELECTOR=cats $ oc expose service cats --name=cats -l name='cats'
Create Testing Application
To do an image promotion across to another project you need to complete the following steps, beginning with finding your REGISTRY_IP.
$ oc login -u system:admin $ oc project default $ oc get services docker-registry $ export REGISTRY_IP=`oc get services docker-registry --no-headers | cut -c 19-30` $ oc login -u developer -p developer $ oc project testing $ oc delete dc cats $ oc delete service cats $ oc delete route cats $ oc create dc cats --image=$REGISTRY_IP:5000/development/cats:promoteQA $ oc deploy cats --cancel $ oc expose dc cats --port=8080 $ oc env dc/cats SELECTOR=cats $ oc set triggers dc/cats --from-config --remove $ oc expose svc cats $ oc patch dc/cats -p '{"spec":{"template":{"spec":{"containers":[{"name":"default-container","imagePullPolicy":"Always"}]}}}}'
OpenShift CI/CD
We now have our project, service accounts, some sample applications, so let’s set up a Jenkins service then finally create a pipeline.
Jenkins Service
$ oc project cicd $ oc delete all -l name='jenkins' $ oc new-app --template=jenkins-ephemeral -l name='jenkins'
Visit the following location to check that the Jenkins service is now accessible: https://jenkins-cicd.127.0.0.1.xip.io using the credentials admin/password .
Pipeline
$ oc project cicd $ oc create -f https://bitbucket.org/emergile/cotd/src/master/etc/cotdcicdpipeline.yaml
Then visit the pipeline at https://127.0.0.1:8443/console/project/cicd/edit/pipelines/cotdcicdpipeline and it should appear as follows:
node('maven') { stage '==> Build&Deploy-In-Development' openshiftBuild(namespace: 'development', buildConfig: 'cats', showBuildLogs: 'true', waitTime: '300000') stage '==> Verify-In-Development' openshiftVerifyDeployment(namespace: 'development', deploymentConfig: 'cats', replicaCount: '1') stage '==> Tag-Image-in-Development' openshiftTag(namespace: 'development', sourceStream: 'cats', sourceTag: 'latest', destinationStream: 'cats', destinationTag: 'promoteQA') stage '==> Deploy-in-Testing' openshiftDeploy(namespace: 'testing', deploymentConfig: 'cats', waitTime: '300000') stage '==> Scale-in-Testing' openshiftScale(namespace: 'testing', deploymentConfig: 'cats', replicaCount: '2', waitTime: '300000') stage '==> Verify-In-Testing' openshiftVerifyDeployment(namespace: 'testing', deploymentConfig: 'cats', replicaCount: '2') }
Start the Pipeline
$ oc login -u developer -p developer $ oc project cicd $ oc start-build cotdcicdpipeline
Verify Lab Success
You can inspect the build progress from the Console at: https://127.0.0.1:8443/console/project/cicd/browse/pipelines . If successful, the pipeline will look similar to the image depicted at the top of this post. You can also visit the Jenkins application using https://jenkins-cicd.127.0.0.1.xip.io/ with your OpenShift developer credentials.
Trivia
Visit https://github.com/openshift/jenkins-plugin for documentation on the OpenShift Jenkins plugin APIs. The pipeline can, of course, be extended to include additional gates and steps leveraging standard capabilities available within Jenkins.
The script cotd/etc/pipeline-setup.sh automates execution of all the above steps for any easy way to repeat this exercise.
This content borrows (steals!) heavily from https://github.com/VeerMuchandi/pipeline-example. Many thanks Veer.