我应该在詹金斯文件中写什么?

What should i write in jenkins file?

我有一个使用 Junit 用 Java 编写的自动化项目,我正在尝试创建新的 Jenkins 作业管道。 我已经创建了管道和一个新的 Jenkins 文件,但我不知道这个文件应该包含什么。 我需要 -

  1. 构建项目
  2. 运行 测试 按类别 (我不想 运行 一项工作中的所有测试)
  3. 部署

我在 Jenkins 文档中找到了这个

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....'
            }
        }
    }
}

但我收到了这条消息: “已找到测试报告,但其中 none 是新的。是否有 leafNodes 运行?”

那么我该如何让它发挥作用呢?以及如何为 运行?

指定一个确切的类别

您需要先设置几项。像 jdk 、maven、git 凭据。然后你的管道看起来像这样。

   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!"
          
        }
    }
   
}