Focus / Goals

  • Show the benefits of using Jenkins and OpenShift together

  • The Jenkins master should be ephemeral:

    • Configuration stored in OpenShift

    • Pipeline, Jobs and libraries in source control

  • GitHub Organizational level and project pull request testing

Getting Started

Installation

Create New Project
oc new-project <project-name>
Add Template to Project
oc create -f https://raw.githubusercontent.com/RHsyseng/openshift-pipeline-library/master/jenkins/openshift/template.yaml

At this point you will need to create a GitHub token to include when processing the template.

Process Template
oc new-app --template docker-image-testing \
  -p JENKINS_ORG_FOLDER_NAME=RHsyseng \
  -p JENKINS_GITHUB_OWNER=RHsyseng-user \
  -p JENKINS_GITHUB_REPO=openshift-client-library \
  -p JENKINS_GITHUB_CRED_ID=github \
  -p GITHUB_USERNAME=RHsyseng-user \
  -p GITHUB_TOKEN=token
Add Jenkins to the project
oc new-app --template jenkins-ephemeral -p NAMESPACE=$(oc project -q) -p MEMORY_LIMIT=2Gi
Add the pipeline
oc create -f https://raw.githubusercontent.com/RHsyseng/openshift-pipeline-library/master/jenkins/openshift/pipeline.yaml
Start the build (pipeline)
oc start-build createcred-pipeline

How does this work?

Source to Image Jenkins

The Jenkins image is S2I enabled which is a huge benefit to this project because it requires additional items to be successful. This includes:

  • Additional plugins:

    • OpenShift Jenkins client (master)

  • Jobs:

    • Seed

    • Organizational Folder

  • Configuration:

    • Global Libraries

    • Script approval

OpenShift

ConfigMap

The orgfolder ConfigMap stores the configuration parameters for the seed job.

Secret

The github secret stores the github username and the token.

Jobs / Pipeline

OpenShift Pipeline
Files
  • jenkins/jobs/credPipelineJob.groovy

  • jenkins/openshift/pipeline.yaml

Steps
  1. Configures the Jenkins URL (Manage Jenkins - Configure System - Jenkins Location). This is required to provide the URL when adding the build status to GitHub.

  2. Create a List<hudson.model.ParameterValue> from a OpenShift ConfigMap to be used for seed job.

  3. Create Jenkins credentials from OpenShift secret. This adds the GitHub username and token to Jenkins for use in GitHub related plugins.

  4. Configure the anonymous user for embedded build status icon.

  5. Finally run the seed job with the parameters is ran to build the Organizational Folder job.

Seed Job
Files
  • jenkins/configuration/jobs/seed

Details

This is the seed job that will be executed to create the orgFolderJob using the Jenkins Job DSL.

Organizational Folder Job DSL
Files
  • jenkins/jobs/orgFolderJob.groovy

Details

This Job DSL will create a Organizational Folder job configured for:

  • GitHub

  • Specific repository owner (or organization)

  • Repository pattern

  • Checkout credentials

Jenkinsfile
Files
  • Jenkinsfile

Details

This is used for the openshift-pipeline-library project but it can also be used as an example for your own project.

Library Methods

Function Variables

newBuildOpenShift

Starts a oc new-build process and monitors while the related objects are executing.

Returned HashMap
[names: newBuild.names(), buildConfigName: buildConfigName]
Example
def newBuild = newBuildOpenShift() {
    url = scmUrl                                    (1)
    branch = scmRef                                 (2)
    contextDir = path                               (3)
    deleteBuild = false                             (4)
    randomName = true                               (5)
    image = "docker.io/aweiteka/playbook2image"     (6)
    imageStream = "project/isname"                  (7)
}
  1. Create a build config from a remote repository.

  2. Create a build config from a remote repository using a specific branch.

  3. The directory in the repository to be used for the build

  4. Delete build config and related objects after complete.

  5. Generate a random name (UUID) for the build config objects.

  6. External registry location for a container image.

  7. Name of an image stream to be used as a builder.

Note

Do not use image and imageStream options together.

runOpenShift

Starts a oc run process and monitors the pod object while executing. Current expectations for this function is that it will do some process and exit. The exit code will determine success or failure.

Example
runOpenShift {
    deletePod = true                (1)
    branch = scmRef                 (2)
    image = dockerImageRepository   (3)
    env = ["foo=goo"]               (4)
}
  1. Delete pod after execution

  2. Name of the pod

  3. Docker image URL

  4. Environmental variables that the pod requires to run.

getImageStreamRepo

Both the newBuildOpenShift and runOpenShift require either the ImageStream or Docker image repoistory url. This function returns both as a HashMap for a given ImageStream name.

Returned HashMap
[dockerImageRepository: is.status.dockerImageRepository,
                 imageStream: imageStream]
Example
isRepo = getImageStreamRepo(newBuild.buildConfigName)

Utils

createJobParameters(HashMap configMap)

Converts an OpenShift ConfigMap to a List of ParameterValues which can be used in a parameterized Jenkins Job.

getGitHubPR(String login, String oauthAccessToken, String changeUrl)

Returns the originating repository and branch name of a pull request.

configureRootUrl(String url)

Sets the Jenkins Location Configuration URL in an ephemeral Jenkins instance.

createCredentialsFromOpenShift(HashMap secret, String id)

From an OpenShift secret creates Jenkins credentials to be used in a Jenkins Job or plugin.