Skip to content

Jenkins Configuration

Shea Phillips edited this page May 13, 2017 · 11 revisions

Intro

Jenkins is one of the most common continuous integration/delivery tools used in open source and commercial software development. OpenShift provides a means to easily deploy a Jenkins instance within OpenShift.

Our recommended approach for product teams working on the OpenShift DevOps platform is to run a Jenkins instance in their "tools" project. Generally, this project will have been provisioned at project inception, along with one or more of "dev", "test", and "prod".

The section immediately below describes the process to use the OpenShift Pipeline feature (now recommended). Subsequent sections describe more details on provisioning and (advanced) custom Jenkins image creation.

Pipeline and Jenkinsfile

The recommended approach for CI functionality for new projects is to leverage the Pipeline functionality.

A JenkinsPipeline BuildConfig strategy includes a reference to a Jenkinsfile, either inline, or a pointer to a GitHub source. Our recommended practice is to use a Jenkinsfile stored in a GitHub repository alongside your application code. Changes to this file will automatically be picked up with every execution of the pipeline defined in OpenShift/Jenkins.

You can use the following as a starter Jenkinsfile. It should be placed in the root of your GitHub repository and named Jenkinsfile. It can be adapted for a project's specific needs once the other required elements for pipeline support are all in place.

node {
  stage('build') {
         echo "Building..."
         openshiftBuild bldCfg: 'myapp', showBuildLogs: 'true'
         openshiftTag destStream: 'myapp', verbose: 'true', destTag: '$BUILD_ID', srcStream: 'myapp', srcTag: 'latest'
         openshiftTag destStream: 'myapp', verbose: 'true', destTag: 'dev', srcStream: 'myapp', srcTag: 'latest'
  }
  stage('deploy-test') {
      input "Deploy to test?"
      openshiftTag destStream: 'myapp', verbose: 'true', destTag: 'test', srcStream: 'myapp', srcTag: '$BUILD_ID'
  }
  stage('deploy-prod') {
      input "Deploy to prod?"
      openshiftTag destStream: 'myapp', verbose: 'true', destTag: 'prod', srcStream: 'myapp', srcTag: '$BUILD_ID'
  }
}

Note The above will need to be adapted to your specific project resources' names - specifially, you would need to change the value of destStream and srcStream to match your application's imagestream name, and the value of bldCfg to match the name of your application's BuildConfiguration.

Pipeline BuildConfig Creation

OpenShift has a special BuildConfiguration strategy - JenkinsPipeline - that integrates OpenShift deeply with Jenkins, providing triggering and tracking of pipeline execution from within the OpenShift web console.

A specifically configured and named Jenkins instance is required within your project in order for the Jenkins<->OpenShift pipeline synchronization to work. This instance can be provisioned explicitly (as explained in #ProvisioningJenkins, below), or it will be provisioned "lazily" upon creation of the first JenkinsPipeline BuildConfig within a project. The latter is the recommended approach.

When a JenkinsPipeline BuildConfig is created, it will cause a pipeline resource to be added to the OpenShift UI, as well as adding a pipeline to Jenkins. The state of the pipeline will be synchronized between Jenkins and OpenShift. This is achieved through direct support in OpenShift and the "OpenShift Sync" plugin on the Jenkins side.

Follow the steps below to create a BuildConfiguration with a JenkinsPipeline build strategy, pointing to the Jenkinsfile you created above.

  1. Run the following:
oc process -f https://raw.githubusercontent.com/BCDevOps/openshift-tools/master/provisioning/pipeline/resources/pipeline-build.json -v NAME=<desired pipeline name> -v SOURCE_REPOSITORY_URL=<github repo containing Jenkinsfile> | oc create -n <your tools project> -f -

Provisioning Jenkins

As referenced above, in order for the pipeline feature to function, a suitable Jenkins instance must exist in the project where JenkinsPipeline-startegy BuildConfigs are created.

There is a capability to dynamically provision a Jenkins instance within a project at the time a BuildConfig with a JenkinsPipeline strategy is created. This automatic Jenkins provisioning be triggered if no service with the exact name of jenkins-pipeline-svc exists within the project. The instance that will be auto-provisioned will be derived based on a set of platform-wide defaults. This may or may not be suitable for a given project, as the Jenkins instance will have only a minimal set of plugins.

For cases where a project requires a Jenkins instance configured in a manner other than the default pipeline-supporting instance, the Add to Project UI or oc new-app may be used. If you require an image with specific plugins pre-installed or other config, please refer to the section below on creating a custom Jenkins image. Note that most teams should not need to do this.

ADVANCED Custom Jenkins Image Creation

Creating a custom Jenkins image can be useful where the default image does not contain plugins required for your project, or you'd like to pre-populate the instance with configuration values rather than configured manually post-instantiation.

There is a repo located here that can be used as a starting point for creating a custom image.

The specific steps to follow to instantiate a custom pipeline-compatible Jenkins instance and service within your project are as found below.

Note: The commands below would typically be executed against your '-tools' project.

  1. Create a BuildConfiguration for your custom Jenkins image using the command below:

oc process -f https://raw.githubusercontent.com/BCDevOps/pathfinder-jenkins/master/openshift/custom-jenkins-build.yaml | oc create -f -

  1. Create a DeploymentConfig to deploy the custom image created above.

oc process jenkins-pipeline -n openshift -v JENKINS_SERVICE_NAME=jenkins-pipeline-svc -v JNLP_SERVICE_NAME=jenkins-jnlp -v NAMESPACE=<your-tools-namepsace> -v JENKINS_IMAGE_STREAM_TAG=custom-jenkins:latest | oc create -f -