DevOps Classroom Series – 11/Dec/2021

Working with OS Shells in Declarative Pipeline

  • The sh Step:
    • Refer Here for the official docs
    • The basic syntax sh <command>
    • Sample code for using sh step and redirecting the output to a variable
    def listing = sh script: 'ls -la /', returnStdout: true
    
    • Executing other language scripts
    sh '''#!/usr/bin/perl
       my $name = "Jenkins";
       print "Hello, $name from Perl!\n '''
    
    sh '''#!/usr/bin/python
    name = "Jenkins"
    print(f"Hello {name} from Python!")
    '''
    
  • The bat Step:
    • On the windows systems there is corresponting bat step for windows operations
    • Refer Here
  • The Powershell Step:

Working with Environment Variables

  • To use environmental variables in jenkins pipeline scripts we have multiple ways
echo "${env.PATH}"
echo "${PATH}"
echo env.PATH
echo PATH
  • All of the above will give the same result
  • In scripted pipeline to set environmental variable
env.USER = 'jenkins'
env.PATH = env.PATH + ':/opt/maven'
  • In declarative pipeline
environment {
    USER = 'jenkins'
    PATH = "$PATH:/opt/maven"
}
  • The withEnv Step:
    • Refer Here for the official docs
    • Jenkins includes withEnv, a special step for working with environmental variables
    • Example
    withEnv(['MYTOOL_HOME=/usr/local/mytool', "PATH+MAVEN=${tool 'mvn3.6.3'}/bin]) {
        sh 'echo PATH = $PATH'
    }
    
  • readFile => to read contents of a file Refer Here
  • writeFile => To write the file Refer Here
  • Checking for file Existentense => fileExists Refer Here
  • timeout Refer Here
  • waitUntil Refer Here

SonarQube Integration with Jenkins

  • Lets configure Sonar Scanner for Maven in Jenkins Preview
  • Oneway of configuring sonar scanning is by using the local maven profile Refer Here
  • THe example pipeline looks as shown below
pipeline {
    agent { label 'jdk11-mvn3.8.4' }
    triggers { 
        cron('45 23 * * 1-5')
        pollSCM('*/5 * * * *')
    }
    parameters {
        string(name: 'MAVEN_GOAL', defaultValue: 'package', description: 'This is maven goal' )
        choice(name: 'BRANCH_TO_BUILD', choices: ['declarative', 'master', 'scripted'], description: 'Branch to build')
    }
    stages {
        stage('scm') {
            steps {
                mail from: "devops@qt.com",
                to: 'team@qt.com',
                subject: "Cloning code for project ${env.JOB_NAME} started",
                body: "${env.BUILD_URL}"

                git url: 'https://github.com/GitPracticeRepo/java11-examples.git', branch: "${params.BRANCH_TO_BUILD}"
            }
        }
        stage('build') {
            steps {
                mail from: "devops@qt.com",
                to: 'team@qt.com',
                subject: "Build using maven for project ${env.JOB_NAME} started",
                body: "${env.BUILD_URL}"
                withSonarQubeEnv('SONAR_9.2.1') {
                    sh "/usr/local/apache-maven-3.8.4/bin/mvn ${params.MAVEN_GOAL}"
                    sh "/usr/local/apache-maven-3.8.4/bin/mvn sonar:sonar -Dsonar.login=911506a6dc59c0b962a2dfb8eb82965dfbdeb743"
                }
                
            }
        }
    }
    post {
        always {
            

            emailext attachLog: true,
                body: """<p> Executed: Job <b>\'${env.JOB_NAME}:${env.BUILD_NUMBER}\'
                </b></p><p>View console outpe at "<a href="${env.BUILD_URL}">${env.JOB_NAME}:${env.BUILD_NUMBER}
                </a>"</p> <p><i>Build log is attached </i> </p>""",
                compressLog: true,
                replyTo: "do-not-reply@qt.com",
                to: "qtdevops@gmail.com",
                subject: "${env.JOB_NAME} - Build ${env.BUILD_NUMBER} -Status ${currentBuild.result}"


        }
    }
}

  • without configuring the local settings of maven we can still create sonar reports Refer Here for the changeset

  • Now lets try to create a pipeline for spring petclinic project.

  • For the Jenkinsfile Refer Here

  • Lets build the project and see the sonar report Preview

  • Generally for whatever pipeline script which we have written, Even if Quality Gate Fails our project, Jenkins will not report, for jenkins to report we ned add an extra step Refer Here

  • Added quality gate step Refer Here

  • Note: Right now the build is failed ‘No previous SonarQube analysis found on this pipeline execution. Please use the ‘withSonarQubeEnv’ wrapper to run your analysis.’

    • This fix will be done offline and will be shared with you

Artifact Repository

  • There are many tools which support storing the build artifacts, some organizations use the network shares to store the build artifacts.
  • Every Programming language has its own way of handling dependencies
    • Java => Maven/Gradle
    • .net => Nuget
    • Python => pip
    • Nodejs => npm
    • Docker => Docker registry
    • Ubuntu => apt repositories
    • Redhat => rpm repositories
  • For Java and Maven we have two popular alternatives
    • Nexus
    • Jfrog/artifactory
  • Lets use artifactory oss Refer Here Preview
  • Steps for installation
# find the release name
run lsb_release -c
distribution="focal" #ubuntu 20.04
wget -qO - https://releases.jfrog.io/artifactory/api/gpg/key/public | sudo apt-key add -;
echo "deb https://releases.jfrog.io/artifactory/artifactory-debs {distribution} main" | sudo tee -a /etc/apt/sources.list;
sudo apt-get update && sudo apt-get install jfrog-artifactory-oss -y
sudo systemctl enable artifactory.service
sudo systemctl start artifactory.service
sudo systemctl status artifactory.service
  • Now try to access artifactory using http://publicip:8081

  • default username = admin & password = password

  • Lets configure artifactory Preview Preview

  • Lets create repositories

    • Local Preview Preview Preview
  • NOW lets create two local maven repositories qt-maven-snapshots and qt-maven-releases Preview

  • Configure artifactory in jenkins Refer Here

  • Install artifactory plugin Preview

  • Now Navigate to Manage Jenkins -> Configure System Preview Preview

  • Now create a new maven project in jenkins Preview Preview Preview Preview Preview

  • Lets build the project Preview Preview Preview

  • Lets view the project in jfrog Preview

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube