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
oc new-project <project-name>
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.
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
oc new-app --template jenkins-ephemeral -p NAMESPACE=$(oc project -q) -p MEMORY_LIMIT=2Gi
oc create -f https://raw.githubusercontent.com/RHsyseng/openshift-pipeline-library/master/jenkins/openshift/pipeline.yaml
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
-
Configures the Jenkins URL (Manage Jenkins - Configure System - Jenkins Location). This is required to provide the URL when adding the build status to GitHub.
-
Create a
List<hudson.model.ParameterValue>
from a OpenShift ConfigMap to be used for seed job. -
Create Jenkins credentials from OpenShift secret. This adds the GitHub username and token to Jenkins for use in GitHub related plugins.
-
Configure the anonymous user for embedded build status icon.
-
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.
[names: newBuild.names(), buildConfigName: buildConfigName]
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)
}
-
Create a build config from a remote repository.
-
Create a build config from a remote repository using a specific branch.
-
The directory in the repository to be used for the build
-
Delete build config and related objects after complete.
-
Generate a random name (UUID) for the build config objects.
-
External registry location for a container image.
-
Name of an image stream to be used as a builder.
Note
|
Do not use |
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.
runOpenShift {
deletePod = true (1)
branch = scmRef (2)
image = dockerImageRepository (3)
env = ["foo=goo"] (4)
}
-
Delete pod after execution
-
Name of the pod
-
Docker image URL
-
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.
[dockerImageRepository: is.status.dockerImageRepository,
imageStream: imageStream]
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.