Skip to content

Latest commit

 

History

History
126 lines (101 loc) · 5.5 KB

devops-pipeline-scm.adoc

File metadata and controls

126 lines (101 loc) · 5.5 KB

Pipeline Definition as Code

In this lab you will get familiar with definine a pipeline as code and manage its versions via a version control system.

Background

Although it is convenient to embed a Jenkinsfile into an OpenShift Pipeline and edit it directly in the OpenShift Web Console, there is no history of changes made to the pipeline through the UI. Furthermore, switching back and forth between multiple versions of the pipeline becomes challenging with an embedded Jenkinsfile. Therefore, it is generally considered a best practice to create a Jenkinsfile and check it into the source control repository in order to manage its versions and history of changes applied to it.

Using OpenShift Pipeline, you can store Jenkinsfiles in a Git repository and reference it in the JenkinsPipeline buildconfig. Whenever the pipeline starts running, OpenShift fetches the latest version the references Jenkinsfile from the Git repository and adjusts the pipeline steps accordingly. In this lab you will create an OpenShift Pipeline with the Jenkinsfile stored in a Git repository.

Although CI/CD is a fairly new in many organizations, teams have been building Continuous Integration flows for a long time and have invested lots of time improving and refining that process. For Java projects, the CI flow generally moves around building the application JAR/WAR/EAR, running the unit-tests suits and integration tests and if successful, releasing the application binaries into a repository manager like Sonatype Nexus and Artifactory. In this lab, you will modify the pipeline in order to extend existing CI processes and build upon the Maven build and test flow that exists in many development teams.

The following diagram shows the pipeline stages. For the sake simplicity, pushing the artifacts to an artifact repository is not included in this pipeline however you can include that using the Nexus Artifact Uploader Plugin for Sonatype Nexus and Artifactory Plugin for Artifactory.

Pipeline Diagram

Pipeline from Git Repository

Let’s start with cloning the cart-service Git repository where you will store the Jenkinsfile:

Caution
Replace {{PROJECT_SUFFIX}} with the number provided to you by the instructor.
Tip

If you are using Eclipse Che, click on Import Project…​ and then enter the Git url as the URL and click on Import. Make sure to replace your username and password in the Git url:

Choose Java > Maven as the project type and click on Save.

You can now right-click on the cart-service project and click on New > File to create the Jenkinsfile

$ cd
$ git clone http://{{GIT_SERVER_URL}}/{{GIT_USER}}/cart-service.git
$ cd cart-service
$ touch Jenkinsfile

Open the created Jenkinsfile in a text editor and paste the following pipeline definition in the editor, and then save the Jenkinsfile:

pipeline {
  agent {
    label 'maven'
  }
  stages {
    stage('Build App') {
      steps {
        sh "mvn clean package -s src/main/config/settings.xml"
      }
    }
    stage('Integration Test') {
      steps {
        sh "mvn verify -s src/main/config/settings.xml"
      }
    }
    stage('Build Image') {
      steps {
        script {
          openshift.withCluster() {
            openshift.withProject() {
              openshift.startBuild("cart", "--from-file=target/cart.jar").logs("-f")
            }
          }
        }
      }
    }
    stage('Deploy') {
      steps {
        script {
          openshift.withCluster() {
            openshift.withProject() {
              dc = openshift.selector("dc", "cart")
              dc.rollout().latest()
              timeout(10) {
                  dc.rollout().status()
              }
            }
          }
        }
      }
    }
    stage('Component Test') {
      steps {
        script {
          sh "curl -s -X POST http://cart:8080/api/cart/dummy/666/1"
          sh "curl -s http://cart:8080/api/cart/dummy | grep 'Dummy Product'"
        }
      }
    }
  }
}

Notice that you don’t have to use Git to clone the code repository in the pipeline anymore. The reason for that is that Jenkins has to clone the given code repository for fetching the pipeline definition, Jenkinsfile, and if the code for the application is also in the same repository then there is no need to clone the code, again.

You need to push the Jenkinsfile to the Git repository in order to make it available on the Git server. Enter your Git username and password if asked:

Caution
Replace {{PROJECT_SUFFIX}} with the number provided to you by the instructor.
  • Username: {{GIT_USER}}

  • Password: {{GIT_PASSWORD}}

$ git add Jenkinsfile
$ git commit -m "Added cart pipeline"
$ git push origin master

You can now create an OpenShift Pipeline and refer the pipelines Git repository to pull the Jenkinsfile and execute it

$ oc project dev--{{PROJECT_SUFFIX}}
$ oc new-build . --name=cart-pipeline-git --strategy=pipeline -e NEXUS_URL={{NEXUS_INTERNAL_URL}}

Click on Builds → Pipelines on the left sidebar menu. A new instance of the cart-pipeline-git pipeline has started running using your Jenkinsfile from the Git repository.

Pipeline from Git Repository