Blue Ocean in Jenkins

Blue Ocean is a modern, user-friendly interface for Jenkins. It's designed to improve the CI/CD experience with an easy-to-use design. It makes pipeline creation simple, provides real-time updates, and works smoothly with GitHub and Bitbucket for better collaboration.

Let’s Create a Pipeline with Blue Ocean GUI

Step 1 : Install Blue ocean plugin

  • Open Jenkins Dashboard and click on Manage Jenkins in the left sidebar.

  • Click on Manage Plugins under the "System Configuration" section.

  • Go to the Available tab and use the search box to find Blue Ocean.

  • Select the Blue Ocean plugin from the search results and click Install without Restart.

  • Wait for the installation to complete.

  • Once installed, go back to the Jenkins Dashboard and click the Open Blue Ocean link on the left sidebar to start using Blue Ocean.

Step 2 : Create a Repository

  • Create a repository if one is not available and add all your code to it.

  • Ensure there is no existing Jenkinsfile in the repository.

Step 3 : Create a pipeline using Blue Ocean

  • Click on Open Blue Ocean from the Jenkins dashboard.

  • Click on New Pipeline to start creating your pipeline.

  • Select the Git option if you want to clone the repository.

  • Get the HTTPS link of the repository (e.g., https://sonarqubescanning-admin@bitbucket.org/sonarqubescanning/test_blue_ocean.git).

  • While adding the repository URL in Blue Ocean, make sure to remove the username (e.g., sonarqubescanning-admin@) from the link and use the URL in this format:
    https://bitbucket.org/sonarqubescanning/test_blue_ocean.git.

  • Add your details and select the credentials you’ve added in Jenkins to connect to Bitbucket.

  • If it ask like - You don't have any branches that contain a Jenkinsfile → click on Create pipeline

Step 4 : Create Pipeline Flow

  • Select the Agent as any for your local system or as per your requirements.

  • In the Blue Ocean GUI, define the pipeline flow by writing the necessary pieces of code in a visual manner.

  •         environment {
              SONAR_HOST_URL = 'http://localhost:9000/'
              SONAR_PROJECT_KEY = 'my_blue_ocean_test'
              SONAR_PROJECT_NAME = 'my_blue_ocean_test'
              SONAR_PROJECT_VERSION = '1.0'
            }
    
  • Add all this environment details as well without single quote (‘)

Step 5 : Add Stage to Clone repository

  • Click on plus ( + ) to add stages

  • Will try to configure this pipleline code on GUI under stage which will clone BitBucekt Repository

  •                   stage('Clone Repository') {
                        steps {
                          git(url: 'https://bitbucket.org/sonarqubescanning/my_blue_ocean_test.git', branch: 'master', credentialsId: 'bb_coding')
                        }
                      }
    
  • After click on plus ( + ) → Enter stage name as Clone Repository to clone BitBucket repo.

  • Click on Add Step option → Select Git

  • After adding all you detais → Just click back arrow. In this way the to clone BitBucket Repository stage is added to pipeline flow

Step 6 : Add Stage to Scan Code via SonarQube

  • Click the plus (+) button again to add another stage.

  • Configure this stage to scan the repository's code using Sonar Scanner.

  • Will try to create this logic via Blue ocean GUI

  •                   stage('SonarQube Analysis') {
                        steps {
                          // Use withSonarQubeEnv to set up the environment variables for the analysis
                          withSonarQubeEnv('SonarQube') {
                            script {
                              bat """
                              "${tool 'sonar-scanner'}\\sonar-scanner.bat" ^
                              -Dsonar.host.url=%SONAR_HOST_URL% ^
                              -Dsonar.projectKey=%SONAR_PROJECT_KEY% ^
                              -Dsonar.projectName=%SONAR_PROJECT_NAME% ^
                              -Dsonar.projectVersion=%SONAR_PROJECT_VERSION% ^
                              -Dsonar.sourceEncoding=UTF-8 ^
                              -Dsonar.sources=. ^
                              -Dsonar.verbose=true ^
                              -Dsonar.qualitygate.wait=true ^
                              -Dsonar.python.version=3.8
                              """
                            }
                          }
                        }
                      }
    
  • Add this SonarQube Installation Name and Click on back arrow

  • Note : You may get the SonqrQube Installation Name → Manage JenkinsSystemSonarQube installations

  • Add one more Step under the Prepare SonarQube Scanner environment

  • Adding the following piece of code which run scanning via Sonar scanner via windows

  •                           bat """
                              "${tool 'sonar-scanner'}\\sonar-scanner.bat" ^
                              -Dsonar.host.url=%SONAR_HOST_URL% ^
                              -Dsonar.projectKey=%SONAR_PROJECT_KEY% ^
                              -Dsonar.projectName=%SONAR_PROJECT_NAME% ^
                              -Dsonar.projectVersion=%SONAR_PROJECT_VERSION% ^
                              -Dsonar.sourceEncoding=UTF-8 ^
                              -Dsonar.sources=. ^
                              -Dsonar.verbose=true ^
                              -Dsonar.qualitygate.wait=true ^
                              -Dsonar.python.version=3.8
                              """
    
  • After adding the code just click on back arrow. Successfully added the Sonar Analysis Stage.

  • Now Save it

  • This will create the Jenkinsfile script automatically on master branch and in parallel the job starts executing.

  • Executing flow

  • Scanning as successfully executed

Step 7 : Add final stage - Quality Gate Check

  • Add a final stage to your pipeline to perform a Quality Gate Check.

  • This stage will determine whether the Quality Gate passes or fails based on the results of the SonarQube scan.

  • Open your flow on Blue ocean → test_blue_ocean → Branch → edit

  • Click on plus ( + ) to add one more stage

  • Select option : Run arbitary Pipeline script and add below code

  •       def qualityGate = waitForQualityGate()
                    if (qualityGate.status != 'OK') {
                      // Fail the build if the Quality Gate fails
                      error "Quality Gate failed: ${qualityGate.status}"
                    } else {
                      echo "Quality Gate passed: ${qualityGate.status}"
                    }
    
  • After adding code click on back arrow it will save

  • After saving click on Save button → Commit to master

  • After commit the pipeline will be start running …

  • Once the scanning is successfully completed, navigate to the SonarQube dashboard to review the detailed analysis report.

  • Note:
    The report may not contain extensive details as only a single piece of code was added for the demonstration of Blue Ocean.

Step 8: Adding Post Cleanup

  • The post-cleanup step requires manual editing as it cannot be handled directly via Blue Ocean.

  • Open the Jenkinsfile in your Bitbucket repository.

  • Add the cleanup code at the end of the stage definitions in the Jenkinsfile and commit it.

  • Try runnig the Job

  •         post {
              always {
                echo 'Cleaning up the workspace...'
                cleanWs()
              }
    
              success {
                echo 'Build completed successfully.'
              }
    
              failure {
                echo 'Build failed. Workspace cleaned.'
              }
            }
    

One key advantage of Blue Ocean is that it automatically generates the Jenkinsfile in your Bitbucket repository, simplifying pipeline setup and version control.

Entire code for your reference

pipeline {
  agent any
  stages {
    stage('Clone Repository') {
      steps {
        git(url: 'https://bitbucket.org/sonarqubescanning/test_blue_ocean.git', branch: 'master', credentialsId: 'bb_coding')
      }
    }

    stage('SonarQube Analysis') {
      steps {
        withSonarQubeEnv('SonarQube') {
          script {
            bat """
            "${tool 'sonar-scanner'}\\sonar-scanner.bat" ^
            -Dsonar.host.url=%SONAR_HOST_URL% ^
            -Dsonar.projectKey=%SONAR_PROJECT_KEY% ^
            -Dsonar.projectName=%SONAR_PROJECT_NAME% ^
            -Dsonar.projectVersion=%SONAR_PROJECT_VERSION% ^
            -Dsonar.sourceEncoding=UTF-8 ^
            -Dsonar.sources=. ^
            -Dsonar.verbose=true ^
            -Dsonar.qualitygate.wait=true ^
            -Dsonar.python.version=3.8
            """
          }

        }

      }
    }

    stage('Quality Gate Check') {
      steps {
        script {
          def qualityGate = waitForQualityGate()
          if (qualityGate.status != 'OK') {
            // Fail the build if the Quality Gate fails
            error "Quality Gate failed: ${qualityGate.status}"
          } else {
            echo "Quality Gate passed: ${qualityGate.status}"
          }
        }

      }
    }

  }

  post {

    always {
      echo 'Cleaning up the workspace...'
      cleanWs()
    }

    success {
      echo 'Build completed successfully.'
    }

    failure {
      echo 'Build failed. Workspace cleaned.'
    }
  }


  environment {
    SONAR_HOST_URL = 'http://localhost:9000/'
    SONAR_PROJECT_KEY = 'test_blue_ocean'
    SONAR_PROJECT_NAME = 'test_blue_ocean'
    SONAR_PROJECT_VERSION = '1.0'
  }
}
0
Subscribe to my newsletter

Read articles from vikas bhaskar vooradi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

vikas bhaskar vooradi
vikas bhaskar vooradi

In my free time, I enjoy coding, blogging, and exploring technology-related content on the internet.