Overview
In modern software projects many teams utilize the concept of continuous integration and continuous delivery (CI/CD). By setting up a tool chain that continuously builds, tests, and stages software releases a team can ensure that their product can be reliably released at any time. OpenShift can be an enabler in the creation and managecment of this tool chain. In this lab we will walk through creating a simple example of a CI/CD pipeline utlizing Jenkins, all running on top of OpenShift!
Start by installing Jenkins
First we will start by installing Jenkins to run in a pod within your workshop project. Because this is just a workshop we use the ephemeral template to create our Jenkins sever (for a enterprise system you would probably want to use the persistent template). Follow the steps below:
Goto the terminal and type the following:
$ oc new-app --template=jenkins-ephemeral
$ oc expose svc jenkins
$ oc policy add-role-to-user edit -z default
Copy hostname and paste in browser's address bar...
$ oc get routes | grep 'jenkins' | awk '{print $2}'
Go into Jenkins project
Click "Add to Project", select jenkins-ephemeral, click "Create". Accept all defaults.
Click "continue to overview", wait for it to start
Click the service link to open jenkins, login as admin/password
The OpenShift pipeline plugin
Now let’s make sure we have the OpenShift Pipeline plugin properly installed within Jenkins. It will be used to define our application lifecycle and to let our Jenkins jobs perform commands on our OpenShift cluster.
Click "Manage Jenkins"
Click on "Manage Plugins"
Click on "Available" tab, and filter on "openshift". Find and install "Openshift Pipeline Jenkins Plugin"
You can read more about the plugin here.
Our sample web app and its automated tests
In this example pipeline we will be building, testing, and staging a Node.js webapp. We wrote all the code for you already, so don’t worry you won’t be coding in this lab. You will just use the code and unit tests to see how CI/CD pipelines work. And keep in mind that these principles are relevant whether your programming in Node.js, Ruby on Rails, Java, PHP or any one of today’s popular programming languages.
Fork the project into your own GitHub account
-
Create a new app in your the Jenkins project using the URL to your forked project. You can follow the steps to create a new project and Image Stream HERE.
-
Where do my unit tests execute? As part of the process of building the new image, the best practice is to execute unit tests after building the image but before executing the build. This allows you to run unit tests in the context of the container environment, but will prevent a deployment if your checks do not pass. For more information about how to modify the s2i process to include your tests, refer here.
Setting up our OpenShift environment to match our lifecycle stages
Setup Jekins jobs to use their openshift image stream (which is off your GitHub fork)
click "New Item"
call it yourname-ci-devel, select freestyle, click OK
under source code management select the OpenShift Image Streams and populate
Click "Poll SCM", set a schedule of: * * * * *
In the "Post-build actions" subsection click "Add post-build action" and select "Build other projects". Type in "yourname-ci-deploytotest"
Click "Save", don't worry about the error here, we are about to build that Jenkins job.
Notes: You will not need the URL of the OpenShift api endpoint or the Authorization Token
to get this to work
Connecting the pipeline for dev->test
Click "Back to dashboard"
Click "New Item"
Call it "yourname-ci-deploytotest", select "freestyle", click "OK"
Click add build step and choose "Execute shell"
echo "inside my jenkins job"
Click add build step and choose "Tag OpenShift Image". Enter in all the info, tag as "readyfortest"
Watch me release!
At this point you should see the following scenario play out: * Once you initiate a git push, the first Jenkins job will execute. You will see Jenkins notice the the change to the image in the registry, and begin running the first job.
-
When this job completes, a second job will execute. This second job will use the OpenShift Pipeline plugin to create a new tag of the image called “ready for test”.
-
You can see the history of this new tag by browsing to initiate two jobs in the pipeline with the final step being the new tag of “readyfortest”. The new tag can then be used for automatic or manual builds of the new test application. You can view the status of the new tag in OpenShift by browsing to Image Streams -> your image stream
Summary
Coming soon… Read more about usage of Jenkins on OpenShift here. Read more about the concepts behind pipelines in Jenkins here.