【问题标题】:What should i write in jenkins file?我应该在詹金斯文件中写什么?
【发布时间】:2021-03-08 11:26:56
【问题描述】:

我有一个用 Java 编写的自动化项目,使用 Junit,我正在尝试创建新的 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....'
            }
        }
    }
}

但我收到了这条消息: “找到了测试报告,但没有一个是新的。leafNodes 运行了吗?”

那么我该如何让它工作呢?以及如何指定要运行的确切类别?

【问题讨论】:

  • 为什么即使 make 失败你也尝试部署? (你为什么还要在 Java 中使用 make 呢?)
  • IDK,我从 Jenkins 管道文档中获取了这个示例
  • 你的 jenkins 中有 maven 的实例吗?或者你可以使用 docker 代理吗?
  • 我想我的 Jenkins 中确实有一个 maven 实例...

标签: java jenkins automation jenkins-pipeline


【解决方案1】:

您需要先进行几项设置。像 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!"
          
        }
    }
   
}
 

【讨论】:

  • 非常感谢您的回答!不过我有一些问题。 1) 我怎样才能找到我的 git credentialId?我用谷歌搜索并没有找到任何答案。也许它是一个 git 令牌? 2)在测试步骤中我可以添加这个: clean test -e -f /home/omgili/jenkins/automation/pom.xml -Dgroups=categories.regression 吗? 3)标签是什么,我在哪里可以定义标签?
  • 好的,所以我已经尝试过了,但我删除了凭据,因为我在 Jenkins 中设置了它们。我删除了阶段('将战争文件传输到服务器')因为我不知道在那里写什么......我收到了这条消息:WorkflowScript:34:未定义部分“阶段”@第34行,第5列。阶段('Build Artifacts') { WorkflowScript: 46: stage section @ line 46, column 5. stage('Unit Test') { WorkflowScript: 46: Undefined section "stage" @ line 46, column 5. stage( '单元测试'){ WorkflowScript:1:缺少必需的部分“阶段”@第1行,第1列。
  • 我分享的代码是基于你需要做的一些预配置。例如,您需要在全局配置中配置 jdk,还需要在同一位置配置 maven。对于 git,您需要将其添加到您的全局配置中。但为此,您需要先安装 git 插件。我会建议您通过一些 youtube 视频来配置每个部分,然后您可以使用我的代码并根据需要进行修改。
  • 谢谢,我会检查是否可以在 youtube 上找到一些东西。我有什么私人问题可以联系你吗?
  • 对不起,我不能在这个平台上分享我的信息。如果您遇到任何问题,请在此处留言,我会尽快回复您。或者,如果您想分享您的电子邮件地址,我可以给您发送电子邮件。
猜你喜欢
  • 1970-01-01
  • 2013-04-16
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
  • 2017-07-13
  • 2023-03-23
  • 2012-09-09
相关资源
最近更新 更多