Jenkins学习笔记第九篇pipeline 接口自动化持续集成测试

  • Post author:
  • Post category:其他


Scripts Pipeline  基于groovy的语法

Declarative pipeline V2.5之后引入  结构化方式

script pipeline书写形式如下:

node {
    def mvnHome
    stage('Preparation') { // for display purposes
      
        git 'https://github.com/jglick/simple-maven-project-with-tests.git'
        mvnHome = tool 'M3'
    }
    stage('Build') {
        // Run the maven build
        withEnv(["MVN_HOME=$mvnHome"]) {
            if (isUnix()) {
                sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
            } else {
                bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore clean package/)
            }
        }
    }
    stage('Results') {
        junit '**/target/surefire-reports/TEST-*.xml'
        archiveArtifacts 'target/*.jar'
    }
}

script pipeline 控制流程 if else

node{
  stage('Example'){
        if(env.BRANCH_NAME == 'master'){
            echo 'I only execute on the master branch'
        }else{
            echo 'I execute elsewhere'
        }
    }
}
stage('build'){
   node{
		echo 'This is build stage'
   }
}

script pipeline  try catch流程

stage('prepare'){
	node{
        echo "This is stage which run on the slave  agent"
	}
}
stage('Test'){
    node('slave'){
        echo 'this is test stage which run on the slave agent'
     try{
         echo 'this is in the try block'
         }catch(exc){
          echo 'this is in the catch block'
        }finally{
         echo 'this is in the finally block'
        }
    }
}

pipeline script

邮件发送:

node {
     def mvnHome
    stage('Preparation') { // for display purposes
        git 'https://github.com/ITLULU/jenkins-pipeline.git'
        mvnHome = tool 'MAVEN'
    }
    stage('Build') {
        // Run the maven build
        withEnv(["MVN_HOME=$mvnHome"]) {
            if (isUnix()) {
               // sh ' cd "$MVN_HOME/bin/";  mvn clean test package'
                 sh '"$MVN_HOME/bin/mvn" -Dmaven.test.failure.ignore clean package'
               
            } else {
                 bat(/"%MVN_HOME%\bin\mvn" -Dmaven.test.failure.ignore clean package/)
              //  bat 'cd "%MVN_HOME%\\bin\\"  mvn clean test package'
            }
        }
    }
    stage('Results') {
        junit keepLongStdio: true, testResults: 'target/surefire-reports/TEST-*.xml'
        archiveArtifacts artifacts: 'target/*.jar', followSymlinks: false
    }
     stage('junit测试'){
         junit allowEmptyResults: true, testResults: 'target/surefire-reports/TEST-test.SomeTest.xml'
     }
     stage('发送邮件'){
           emailext(
                subject: '构建通知:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
                body: '${FILE,path="./target/surefire-reports/test.SomeTest.txt"}',
                to: '邮箱地址'
           )
     }
  
}

publish TestReport

 pipeline{
     agent{
        node{
             label 'master'
             customWorkspace 'myworkspace'
        }
    }
    environment{
        mvnHome = tool 'MAVEN'
    }
    stages{
        stage('preparation'){
            steps{
                git 'https://github.com/ITLULU/TestNg-Allure.git'
            }
        }
        stage('build'){
            steps{
                    sh " cd ./testNg.versionone/; '${mvnHome}/bin/mvn' -Dmaven.test.failure.ignore clean package "
            }
        }
        stage('result'){
            steps{
                archive './testNg.versionon/target/*.jar'
            }
         post{
            always{
                 publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: './testNg.versionone/target/report', reportFiles: 'Report.html', reportName: 'HTML Report', reportTitles: 'TestNG测试报告'])

            }
        }
        }
      
    }
}

jenkins file 执行pipeline Script from  SCM

配置github

指定jenkinsfile路径