Jenkins Pipeline Jenkinfiles Example Code

Example 1


      node {
         def mvnHome
         stage('Preparation') { // for display purposes
            // Get some code from a GitHub repository
            git 'https://github.com/scmgalaxy/helloworld-java-maven'
            // Get the Maven tool.
            // ** NOTE: This 'M3' Maven tool must be configured
            // **       in the global configuration.           
            mvnHome = tool 'maven'
         }
         stage('Build') {
            // Run the maven build
            if (isUnix()) {
               sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package"
            } else {
               bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean package/)
            }
         }
         stage('UNIT') {
            // Run the maven build
            if (isUnix()) {
               sh "'${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean test"
            } else {
               bat(/"${mvnHome}\bin\mvn" -Dmaven.test.failure.ignore clean package/)
            }
         }
         stage('Results') {
            junit '**/target/surefire-reports/TEST-*.xml'
            archive 'target/*.jar'
         }
      }
      

Example 2

#!/usr/bin/env groovy pipeline { agent { docker { image 'node' args '-u root' } } stages { stage('Build') { steps { echo 'Building...' sh 'npm install' } } stage('Test') { steps { echo 'Testing...' sh 'npm test' } } } }

Example 3


      pipeline {
          agent any 
          stages {
              stage('Stage 1') {
                  steps {
                      echo 'Hello world!' 
                  }
              }
          }
      }
      

Example 4


      pipeline {
          agent any
      
          stages {
              stage('Build') {
                  steps {
                      echo 'Building..'
                  }
              }
              stage('Test') {
                  steps {
                      echo 'Testing..'
                  }
              }
              stage('Deploy') {
                  steps {
                      echo 'Deploying....'
                  }
              }
          }
      }
      

Example 5


      pipeline {
          agent any
      
          stages {
              stage('Build') {
                  steps {
                      sh 'make' 
                      archiveArtifacts artifacts: '**/target/*.jar', fingerprint: true 
                  }
              }
          }
      }
      

Example 6


      pipeline {
          agent any
      
          stages {
              stage('Test') {
                  steps {
                      /* `make check` returns non-zero on test failures,
                      * using `true` to allow the Pipeline to continue nonetheless
                      */
                      sh 'make check || true' 
                      junit '**/target/*.xml' 
                  }
              }
          }
      }
      

Example 7


pipeline {
    agent any

    stages {
        stage('Deploy') {
            when {
              expression {
                currentBuild.result == null || currentBuild.result == 'SUCCESS' 
              }
            }
            steps {
                sh 'make publish'
            }
        }
    }
}

Avail Rajesh Kumar as trainer at 50% Discount
Puppet Online Training
Puppet Classroom TrainingEnroll Now