我应该在jenkins文件中写什么?

u91tlkcl  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(458)

我有一个用java编写的自动化项目,使用junit,我正在尝试创建我的新jenkins工作管道。我已经创建了管道和一个新的jenkins文件,但是我不知道这个文件应该包含什么。我需要-
构建项目
按类别运行测试(我不想在一个作业中运行所有测试)
部署
我在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....'
            }
        }
    }
}

但我得到了这样的信息:“检测报告被发现了,但没有一个是新的。“你跑了吗?”
那我该怎么做呢?如何指定要运行的确切类别?

ppcbkaq5

ppcbkaq51#

你需要先设置一些东西。比如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!"

        }
    }

}

相关问题