Jenkins is a leading continuous integration and continuous delivery (CI/CD) tool that is used to build, test and deploy application projects continuously. You can build pipelines, as shown in this example, to promote your application across environments such as Development, QA and Production thus enabling DevOps.
OpenShift 3, as an intelligent PaaS, also provides some of the DevOps features with the built in Source to Image process. With this process you can provide the source code and choose a builder image (technology) while OpenShift builds your application docker image from that source code, and then deploys it.
Several enterprises have implemented their CI/CD workflows using Jenkins and are curious to know if Jenkins and OpenShift 3 can complement each other and can be used together. This blog demonstrates and explains how to use external Jenkins to deploy applications to OpenShift with a simple example. You may consider this as a starting point and build upon this example for your specific enterprise needs. Note that improvements such as OpenShift 3 plugin for Jenkins may come up in the future that will further simplify the steps in these scripts. This is not a prescribed or recommended approach, but just an example to show that you can deploy applications into OpenShift from an external CI tool such as Jenkins.
Video
Pre-requisites
- You have an external Jenkins installed and available to use. You will also need plugins in your Jenkins environment viz Environment Injector Plugin, and Build Pipeline Plugin,
- You are authorized to install "oc" client on the server on which Jenkins in set up.
- Familiarity with Jenkins: This blogpost expects that you already know how to use Jenkins and does not teach using Jenkins.
Steps
1. Create OpenShift projects
This example builds upon my earlier
[video blog on how to promote an application across environments]
(https://blog.openshift.com/promoting-applications-across-environments/). In this blog you will see how we can define two projects one for "development" and another one for "testing". That blog explains how an application is initially deployed into "development" project and once the developers are satisfied with the build the tester can pull the same docker image and deploy into "testing" project.
As the first step, using the same process, we will create two projects in OpenShift 3 viz., "development" and "testing". Also we will have to provide image puller access to the "testing" service account on the development project, so that we are able to take an application docker image from the "development" project's image stream and deploy in the "testing" project.
2. Install "oc" CLI client
Log into the server on which Jenkins in running. Download and install "oc" as explained in the CLI reference guide.
Next we will set up two Jenkins projects. The first one will build and deploy a Java application into development project. The second one will use the image from the "development" project to deploy to the "testing" project.
3. Create a Jenkins Project to "Deploy to Development"
We will use a Jenkins project to deploy an application to the "development" project. Use Jenkins->New Item and "Freestyle Project" option to create the project. Now add a build step and copy the script from here. This script expects the following environment variables to be set up. So you need to inject these environment variable to your build step using "Inject environment variables to the build process"
APP_HOSTNAME=<your development application's FQDN, ex: myapp-development.apps.myopenshift.com>
APP_NAME= <your application name,ex: myapp>
APP_GIT=<your java source code from a git repository>
APP_GIT_REF= <git reference, leave it blank if not applicable>
APP_GIT_CONTEXT_DIR= <git context directory, leave it blank if not applicable>
USER_NAME=<username to login to OpenShift3>
OSE_SERVER=<your OpenShift3 master URL>
CERT_PATH= <location where you placed your certificate file, example /var/lib/jenkins/secrets/ca.crt>
DEVEL_PROJ_NAME=<name of your development project, example "development”>
You will also need to inject a password using an environment variable with name USER_PASSWD by selecting the check box next to "Inject passwords to the build as environment variables".
The script will create a new application if one does not exist. It will wait for the build to start, display the build logs and also wait until the application is deployed. You can add your own tests at the end of the script if you want to. If the application already exists, it will start a new build and deploy.
You can test this project by running the Jenkins Project.
4. Create a Jenkins Project to "Deploy to QA"
Now we will set up another Jenkins project "Deploy to QA". Just like the last one, this is a free style project and add a build step with the code from here. This scripts expects the following environment variables.
APP_HOSTNAME=<your QA application FQDN, ex: myapp-testing.apps.myopenshift.com>
APP_NAME=<your application name, ex: myapp>
USER_NAME=<username to login to OpenShift3>
OSE_SERVER=<your OpenShift3 master URL>
CERT_PATH=<location where you placed your certificate file, example /var/lib/jenkins/secrets/ca.crt>
QA_PROJ_NAME=qaproject
DEVEL_PROJ_NAME=<name of your development project, example "development”>
You will also need to inject a password using an environment variable with name USER_PASSWD by selecting the checkbox next to "Inject passwords to the build as environment variables".
This script will create a new application in the QA project based on the latest docker image from the development project. It first tags the docker image in the development project as "promote" and deploys it. If the application was already deployed before, i.e., this is not the first time you are running this script, it assumes that the latest docker image is the one to be deployed. So it tags the latest docker image in the development project as "promote". Once the new docker image is tagged, since the application was already deployed earlier based on this tag, OpenShift triggers a redeploy. So, the script waits for the application to be deployed.
You can add a script that executes test cases at the end of this script if you want.
As before, you can test this project by running the Jenkins Project.
5. Create a Build Pipeline
In order to set up a build pipeline, get back to the "Deploy to Development" Jenkins Project and add the post build action to "Deploy to QA". If you have Build Pipeline plugin installed, you can add a new pipeline starting with "Deploy to Development". This will create a pipeline with two steps, "Deploy to Development" and "Deploy to QA".
You can now execute the pipeline by running it. If the first step (Deploy to Development) fails, the pipeline will not proceed further. If not, it allows the second step (Deploy to QA) to be run. You can extend this pipeline to "Deploy to Prod" as well following the same approach.
Conclusion
In this post we have a simple example to demonstrate how Jenkins can integrate with OpenShift using "oc" CLI. There are several other possibilities. For example, you could use OpenShift Rest APIs instead of CLIs. Jenkins is available to run as a container within OpenShift (registry.access.redhat.com/openshift3/jenkins-1-rhel7). A Jenkins plugin to deploy applications to OpenShift may be developed. So, as additional work is done in this area, you can choose the best path moving forward. With this example, we merely demonstrate an approach to do it today.
About the author
More like this
Browse by channel
Automation
The latest on IT automation that spans tech, teams, and environments
Artificial intelligence
Explore the platforms and partners building a faster path for AI
Open hybrid cloud
Explore how we build a more flexible future with hybrid cloud
Security
Explore how we reduce risks across environments and technologies
Edge computing
Updates on the solutions that simplify infrastructure at the edge
Infrastructure
Stay up to date on the world’s leading enterprise Linux platform
Applications
The latest on our solutions to the toughest application challenges
Original shows
Entertaining stories from the makers and leaders in enterprise tech
Products
- Red Hat Enterprise Linux
- Red Hat OpenShift
- Red Hat Ansible Automation Platform
- Cloud services
- See all products
Tools
- Training and certification
- My account
- Developer resources
- Customer support
- Red Hat value calculator
- Red Hat Ecosystem Catalog
- Find a partner
Try, buy, & sell
Communicate
About Red Hat
We’re the world’s leading provider of enterprise open source solutions—including Linux, cloud, container, and Kubernetes. We deliver hardened solutions that make it easier for enterprises to work across platforms and environments, from the core datacenter to the network edge.
Select a language
Red Hat legal and privacy links
- About Red Hat
- Jobs
- Events
- Locations
- Contact Red Hat
- Red Hat Blog
- Diversity, equity, and inclusion
- Cool Stuff Store
- Red Hat Summit