You can implement via Jenkins a workflow with many steps which can be used to automate your CI and CD process. In OpenShift it is possible to see the pipelines as integrate it to run Openshift commands in the stages of this workflow.
Following a pipeline create in Jenkins to perform the stages for an application in OpenShift.
Following the same pipeline integrated with OpenShift.
Understanding how to create a Pipeline in Jenkins
## Go to Jenkins dashboard and click on “New Item”
## Select Jenkins Pipeline
## Configure your pipeline.
Note that Jenkins has a lot of triggers and plugins to help you with it. However, goes to the field Pipeline at the end of this page. This place you can implement a script in Groove to define the stages and steps of your workflow as you can check the Jenkins examples.
Each stage will be a box of your pipeline. You must define the steps that need to be done for each stage. Following an example to do a git checkout.
// Checkout Source Code from a git repository stage('Checkout Source') { git credentialsId: 'bf466039-2e8f-435b-aa4c-ca173358bfb8', url: 'https://github.com/camilamacedo86/exampleShortPath.git' }
## Common usage
- To print a text in the execution logs of your pipeline.
stage('Build war') { echo "Building version ${devTag}" sh "${mvnCmd} clean package -DskipTests" }
- To define variables.
def devTag = "${version}-${BUILD_NUMBER}"
- To execute commands in a UNIX environment using `sh` and for a windows `bat`.
sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
- To perform conditionals use the
input
input "Would you like to deploy it in production?"
## Use commands to perform actions in the OpenShift as the follows examples.
// Deploy the development application. openshiftDeploy depCfg: 'tasks', namespace: 'my-app-dev', verbose: 'false', waitTime: '', waitUnit: 'sec' openshiftVerifyDeployment depCfg: 'tasks', namespace: 'my-app-dev', replicaCount: '1', verbose: 'false', verifyReplicaCount: 'false', waitTime: '', waitUnit: 'sec' openshiftVerifyService namespace: 'my-app-dev', svcName: 'tasks', verbose: 'false'
## Openshift plugins :
You can use the commands specified in the following projects for OpenShit in the code implementation. Check the ReadMe of this projects to know the available resources.
- OpenShift V3 Plugin for Jenkins – https://github.com/openshift/jenkins-plugin (Deprecated)
- OpenShift Jenkins Pipeline (DSL) Plugin – https://github.com/openshift/jenkins-client-plugin – (Recommended)
## Use the Pipeline Syntax to get a help to implement your workflow
You can get a help from Jenkins to generated your steps and get the code implementations with this feature really easy.
For further information check the following documentation Jenkins Pipeline documentation
Understanding how to integrate a Jenkins Pipeline with OpenShift
## Add Pipeline to Source Code Repository
Add the Pipeline script to a repository as the following example.
## Create a trigger to start the pipeline
You need to create a trigger to define when the pipeline will be started. For example, to start the process when the master branch is updated. It can be made in many ways.
In the GitHub go to the settings of the project and check the WebHooks, also it can be made via the Integrations & Services. Following few images to illustrate it.
For further information check Customizing Git – Git Hooks documentation.
Following an example with Gogs to trigger any push made just against to the master branch and ignore the other branches of the repository. Also, the following example is using the Jenkins Token API to trigger the build.
#!/bin/bash while read oldrev newrev refname do // get the branch name branch=$(git rev-parse --symbolic --abbrev-ref $refname) // check if is master branch if [[ "$branch" == "master" ]]; then // Call the Jenkins endpoint/URL which will start to build the pipeline. It will be the same of click on the build button in the Jenkins UI. curl -k -X POST --user <jenkinsUserID>:<JenkinsUserIDTokenGenerated> http://<jenkinsOpenShiftSVC>.svc.cluster.local/job/<pipelineName>/build fi done
NOTE: To use and the OpenShift token goes to `User >> Configure` and click on `Show API Token` then get the UserID and API Token to be used.
For further information about how to create triggers for Openshift check the Builds/Triggering docs.
## Create a Build Config to integrate the pipeline with the OpenShift web console
To do it is required to create a build config, bc, pointing to the Jenkinsfile
in the repository. Following an example of a command with will create the file and will use it to create the build configuration in OpenShift.
echo "apiVersion: v1 items: - kind: "BuildConfig" apiVersion: "v1" metadata: name: "my-pipeline" spec: source: type: "Git" git: uri: "https://github.com/camilamacedo86/exampleShortPath" strategy: type: "JenkinsPipeline" jenkinsPipelineStrategy: jenkinsfilePath: Jenkinsfile kind: List metadata: []" | oc create -f - -n <JenkinsNameProject>
For further information check Build OpenShift documentation