可以将评论添加到Jenkins文件中吗?

时间:2017-02-18 01:11:51

标签: jenkins groovy comments jenkins-pipeline

Jenkins文件中是否有可能的注释?如果是这样,语法是什么?

我正在使用声明性管道语法。

我想在下面的“帖子”部分注释掉,直到我的SMTP服务器正常工作。

pipeline {

  agent { label 'docker-build-slave' }

  environment {
    IMAGE = 'registry.gitlab.com/XXXXX/bible-server'
    DOCKER_REGISTRY_CREDENTIALS = credentials('DOCKER_REGISTRY_CREDENTIALS')
  }

  options {
    timeout(10)
  }

  stages {

    stage('Test') {
      steps {
        sh 'yarn'
        sh 'npm test'
      }
    }

    stage('Build') {
      when {
        branch '*/master'
      }
      steps {
        sh 'docker login -u ${DOCKER_REGISTRY_CREDENTIALS_USR} -p ${DOCKER_REGISTRY_CREDENTIALS_PSW} registry.gitlab.com'
        sh 'docker build -t ${IMAGE}:${BRANCH_NAME} .'
        sh 'docker push ${IMAGE}:${BRANCH_NAME}'
      }
    }

    stage('Deploy') {
      when {
        branch '*/master'
      }
      steps {
        echo 'Deploying ..'
      }
    }
  }

  post {
    success {
      mail to: "XXXXX@gmail.com", subject:"SUCCESS: ${currentBuild.fullDisplayName}", body: "Yay, we passed."
    }
    failure {
      mail to: "XXXXX@gmail.com", subject:"FAILURE: ${currentBuild.fullDisplayName}", body: "Boo, we failed."
    }
  }
}

4 个答案:

答案 0 :(得分:178)

Jenkinsfile是用groovy编写的,它使用Java(和C)形式的注释:

/* this
   is a
   multi-line comment */

// this is a single line comment

答案 1 :(得分:6)

您可以为每行使用块(/ *** /)或单行注释(//)。您应该在sh命令中使用“#”。

阻止评论

/*  
post {
    success {
      mail to: "XXXXX@gmail.com", 
      subject:"SUCCESS: ${currentBuild.fullDisplayName}", 
      body: "Yay, we passed."
    }
    failure {
      mail to: "XXXXX@gmail.com", 
      subject:"FAILURE: ${currentBuild.fullDisplayName}", 
      body: "Boo, we failed."
    }
  }
*/

单行

// post {
//     success {
//       mail to: "XXXXX@gmail.com", 
//       subject:"SUCCESS: ${currentBuild.fullDisplayName}", 
//       body: "Yay, we passed."
//     }
//     failure {
//       mail to: "XXXXX@gmail.com", 
//       subject:"FAILURE: ${currentBuild.fullDisplayName}", 
//       body: "Boo, we failed."
//     }
// }

在“ sh”命令中添加注释

        stage('Unit Test') {
            steps {
                ansiColor('xterm'){
                  sh '''
                  npm test
                  # this is a comment in sh
                  '''
                }
            }
        }

答案 2 :(得分:2)

official Jenkins documentation仅提及单行命令,如下所示:

// Declarative //

和(see

pipeline {
    /* insert Declarative Pipeline here */
}

Jenkinsfile的语法基于 Groovy ,因此也可以使用groovy syntax for comments。引用:

/* a standalone multiline comment
   spanning two lines */
println "hello" /* a multiline comment starting
                   at the end of a statement */
println 1 /* one */ + 2 /* two */

/**
 * such a nice comment
 */

答案 3 :(得分:1)

注释在任何常用的Java / Groovy格式中都可以正常工作,但是您目前不能使用groovydoc处理Jenkinsfile

首先,groovydoc对没有扩展名的文件造成了错误的错误

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
    at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.lang.String.substring(String.java:1967)
    at org.codehaus.groovy.tools.groovydoc.SimpleGroovyClassDocAssembler.<init>(SimpleGroovyClassDocAssembler.java:67)
    at org.codehaus.groovy.tools.groovydoc.GroovyRootDocBuilder.parseGroovy(GroovyRootDocBuilder.java:131)
    at org.codehaus.groovy.tools.groovydoc.GroovyRootDocBuilder.getClassDocsFromSingleSource(GroovyRootDocBuilder.java:83)
    at org.codehaus.groovy.tools.groovydoc.GroovyRootDocBuilder.processFile(GroovyRootDocBuilder.java:213)
    at org.codehaus.groovy.tools.groovydoc.GroovyRootDocBuilder.buildTree(GroovyRootDocBuilder.java:168)
    at org.codehaus.groovy.tools.groovydoc.GroovyDocTool.add(GroovyDocTool.java:82)
    at org.codehaus.groovy.tools.groovydoc.GroovyDocTool$add.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
    at org.codehaus.groovy.tools.groovydoc.Main.execute(Main.groovy:214)
    at org.codehaus.groovy.tools.groovydoc.Main.main(Main.groovy:180)
    ... 6 more

...第二,据我所知,groovy脚本开始处的Javadoc风格的注释被忽略。因此,即使将Jenkinsfile复制/重命名为Jenkinsfile.groovy,也不会得到很多有用的输出。

我希望能够使用

/**
 * Document my Jenkinsfile's overall purpose here
 */

在我的Jenkinsfile开头评论。还没有这种运气。

但是,如果您将groovydoc传递给命令,则

Jenkinsfile 处理-private中定义的类和方法。