Search code examples
javajenkinsautomationjenkins-pipeline

What should i write in jenkins file?


I have an automation project written in Java, using Junit, and I'm trying to create my new Jenkins job pipeline. I've created the pipeline and a new Jenkins file, but I don't know what this file should contain. I need to -

  1. build the project
  2. Run the tests by category (I don't want to run all the test in one job)
  3. Deploy

I've found this one in Jenkins documentation

pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                echo 'Building..'
            }
        }
        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 'pom.xml'
             }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
            }
        }
    }
}

But I got this message: "Test reports were found but none of them are new. Did leafNodes run?"

So how do I make it work? and how can I specify an exact category to run?


Solution

  • There are a couple of things you will need to setup first. Like jdk , maven, git credentials. Then your pipeline will looks something like this.

       pipeline {
    
      agent {
            node { 
                label 'the label of your agen or have "any" if you didnt specidy one'
            }
        }
      environment {
          //maven home as it is configured in Global Configuration
           mvnHome = tool 'maven'
    
           
        }
        
     options{
        // remove older builds and artifacts if they exceed 15 builds
        buildDiscarder(logRotator(numToKeepStr: '100', artifactNumToKeepStr: '100'))
        //add the time stamp to the logs
        timestamps()
     }
    
       
       
       stages {
        stage("Git CheckOut") {
          steps {
            script{
            //CheckOut from the repository
            def scmVars = checkout([$class: 'GitSCM', 
            branches: [[name: 'master']], //here you can enter branch name or SHA code
            userRemoteConfigs: [[credentialsId: 'credential that  you set for you git here', 
            url: "your git url here"]]]) 
            }
          }
    
        } 
        
        stage('Build Artifacts') {
            steps {
            sh "echo Packaging the artifacts!"
            //packaging the project
            sh "${mvnHome}/bin/mvn clean package  "
            //archiving the artifacts after the build
            sh "echo Archiving the artifacts!"
            archiveArtifacts 'target/*.war' // you can deploy to nexus if you setup nexus
            
            }
        }
    
        stage('Unit Test') {
            steps {
            //running the unit tests
            sh "${mvnHome}/bin/mvn clean test"
            }
            
        }
    
    
         
         
            stage('Transfer war file to Servers') {
                steps {
                    sshagent(['agent name that was setup in your server where you want to deploy artifacts']) { 
                    sh "echo Trasnfering files to servers!"
                    //copy war file  servers
                        sh 'scp -o StrictHostKeyChecking=no $projPath/target/your war file /your server path'
    
                    }
                }
            }
        
       }
       
       
        post {
    
            always {
                sh "echo Jenkins Job is Done"
    
            }
            success {
                sh "echo Sending Success Email!"
            }
            failure {
                sh "echo Sending Failed Email!"
              
            }
        }
       
    }