OpenShift Pipeline Builds

You are viewing documentation for a release that is no longer supported. The latest supported version of version 3 is [3.11]. For the most recent version 4, see [4]

You are viewing documentation for a release that is no longer supported. The latest supported version of version 3 is [3.11]. For the most recent version 4, see [4]

Introduction

Whether you are creating a simple website or a complex web of microservices, use OpenShift Pipelines to build, test, deploy, and promote your applications on OpenShift.

In addition to standard Jenkins Pipeline Syntax, the OpenShift Jenkins image provides the OpenShift Domain Specific Language (DSL) (through the OpenShift Jenkins Client Plug-in), which aims to provide a readable, concise, comprehensive, and fluent syntax for rich interactions with an OpenShift API server, allowing for even more control over the build, deployment, and promotion of applications on your OpenShift cluster.

This example demonstrates how to create an OpenShift Pipeline that will build, deploy, and verify a Node.js/MongoDB application using the nodejs-mongodb.json template.

Creating the Jenkins Master

To create the Jenkins master, run:

  1. $ oc project <project_name> (1)
  2. $ oc new-app jenkins-ephemeral (2)
1Select the project that you want to use or create a new project with oc new-project <project_name>.
2If you want to use persistent storage, use jenkins-persistent instead.

If Jenkins auto-provisioning is enabled on your cluster, and you do not need to make any customizations to the Jenkins master, you can skip the previous step.

For more information about Jenkins autoprovisioning, see Configuring Pipeline Execution.

The Pipeline Build Configuration

Now that the Jenkins master is up and running, create a BuildConfig that employs the Jenkins pipeline strategy to build, deploy, and scale the Node.js/MongoDB example application.

Create a file named nodejs-sample-pipeline.yaml with the following content:

  1. kind: "BuildConfig"
  2. apiVersion: "v1"
  3. metadata:
  4. name: "nodejs-sample-pipeline"
  5. spec:
  6. strategy:
  7. jenkinsPipelineStrategy:
  8. jenkinsfile: <pipeline content from below>
  9. type: JenkinsPipeline

For more information about configuring the Pipeline Build Strategy, see Pipeline Strategy Options.

The Jenkinsfile

Once you create a BuildConfig with a jenkinsPipelineStrategy, tell the pipeline what to do by using an inline jenkinsfile. This example does not set up a Git repository for the application.

The following jenkinsfile content is written in Groovy using the OpenShift DSL. For this example, include inline content in the BuildConfig using the YAML Literal Style, though including a jenkinsfile in your source repository is the preferred method.

The completed BuildConfig can be viewed in the OpenShift Origin repository in the examples directory, nodejs-sample-pipeline.yaml.

  1. def templatePath = 'https://raw.githubusercontent.com/openshift/nodejs-ex/master/openshift/templates/nodejs-mongodb.json' (1)
  2. def templateName = 'nodejs-mongodb-example' (2)
  3. pipeline {
  4. agent {
  5. node {
  6. label 'nodejs' (3)
  7. }
  8. }
  9. options {
  10. timeout(time: 20, unit: 'MINUTES') (4)
  11. }
  12. stages {
  13. stage('preamble') {
  14. steps {
  15. script {
  16. openshift.withCluster() {
  17. openshift.withProject() {
  18. echo "Using project: ${openshift.project()}"
  19. }
  20. }
  21. }
  22. }
  23. }
  24. stage('cleanup') {
  25. steps {
  26. script {
  27. openshift.withCluster() {
  28. openshift.withProject() {
  29. openshift.selector("all", [ template : templateName ]).delete() (5)
  30. if (openshift.selector("secrets", templateName).exists()) { (6)
  31. openshift.selector("secrets", templateName).delete()
  32. }
  33. }
  34. }
  35. }
  36. }
  37. }
  38. stage('create') {
  39. steps {
  40. script {
  41. openshift.withCluster() {
  42. openshift.withProject() {
  43. openshift.newApp(templatePath) (7)
  44. }
  45. }
  46. }
  47. }
  48. }
  49. stage('build') {
  50. steps {
  51. script {
  52. openshift.withCluster() {
  53. openshift.withProject() {
  54. def builds = openshift.selector("bc", templateName).related('builds')
  55. timeout(5) { (8)
  56. builds.untilEach(1) {
  57. return (it.object().status.phase == "Complete")
  58. }
  59. }
  60. }
  61. }
  62. }
  63. }
  64. }
  65. stage('deploy') {
  66. steps {
  67. script {
  68. openshift.withCluster() {
  69. openshift.withProject() {
  70. def rm = openshift.selector("dc", templateName).rollout().latest()
  71. timeout(5) { (9)
  72. openshift.selector("dc", templateName).related('pods').untilEach(1) {
  73. return (it.object().status.phase == "Running")
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. }
  81. stage('tag') {
  82. steps {
  83. script {
  84. openshift.withCluster() {
  85. openshift.withProject() {
  86. openshift.tag("${templateName}:latest", "${templateName}-staging:latest") (10)
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }
1Path of the template to use.
2Name of the template that will be created.
3Spin up a node.js slave pod on which to run this build.
4Set a timeout of 20 minutes for this pipeline.
5Delete everything with this template label.
6Delete any secrets with this template label.
7Create a new application from the templatePath.
8Wait up to five minutes for the build to complete.
9Wait up to five minutes for the deployment to complete.
10If everything else succeeded, tag the $ {templateName}:latest image as $ {templateName}-staging:latest. A pipeline BuildConfig for the staging environment can watch for the $ {templateName}-staging:latest image to change and then deploy it to the staging environment.

The previous example was written using the declarative pipeline style, but the older scripted pipeline style is also supported.

Creating the Pipeline

You can create the BuildConfig in your OpenShift cluster by running:

  1. $ oc create -f nodejs-sample-pipeline.yaml

If you do not want to create your own file, you can use the sample from the Origin repository by running:

  1. $ oc create -f https://raw.githubusercontent.com/openshift/origin/master/examples/jenkins/pipeline/nodejs-sample-pipeline.yaml

For more information about the OpenShift DSL syntax used here, see OpenShift Jenkins Client Plug-in.

Starting the Pipeline

Start the pipeline with the following command:

  1. $ oc start-build nodejs-sample-pipeline

Alternatively, you can start your pipeline with the OpenShift Web Console by navigating to the Builds → Pipeline section and clicking Start Pipeline, or by visiting the Jenkins Console, navigating to the Pipeline that you created, and clicking Build Now.

Once the pipeline is started, you should see the following actions performed within your project:

  • A job instance is created on the Jenkins server.

  • A slave pod is launched, if your pipeline requires one.

  • The pipeline runs on the slave pod, or the master if no slave is required.

    • Any previously created resources with the template=nodejs-mongodb-example label will be deleted.

    • A new application, and all of its associated resources, will be created from the nodejs-mongodb-example template.

    • A build will be started using the nodejs-mongodb-example BuildConfig.

      • The pipeline will wait until the build has completed to trigger the next stage.
    • A deployment will be started using the nodejs-mongodb-example deployment configuration.

      • The pipeline will wait until the deployment has completed to trigger the next stage.
    • If the build and deploy are successful, the nodejs-mongodb-example:latest image will be tagged as nodejs-mongodb-example:stage.

  • The slave pod is deleted, if one was required for the pipeline.

The best way to visualize the pipeline execution is by viewing it in the OpenShift Web Console. You can view your pipelines by logging into the web console and navigating to Builds → Pipelines.

Advanced Options for OpenShift Pipelines

With OpenShift Pipelines, you can launch Jenkins in one project and then have the OpenShift Sync Plugin monitor a group of projects in which the developers work. The following sections outline the steps to complete this process.

  • To disable Jenkins auto=provisioning, see Configuring Pipeline Execution.

  • To enable the Jenkins Service Account to have access to each of the projects that will run OpenShift Pipelines, see Cross Project Access.

  • To add projects to monitor, either:

    • Log into the Jenkins console.

      • Navigate to Manage Jenkins, then Configure System.

      • Update the Namespace field under OpenShift Jenkins Sync.

    • Or extend the OpenShift Jenkins image using the S2I extension option to update the Jenkins configuration file.

Avoid monitoring the same project from multiple Jenkins deployments running the OpenShift Sync Plugin. There is no coordination between those instances and unpredictable results can occur.