可以将注释添加到Jenkinsfile吗?


177

Jenkinsfile中可能有注释吗?如果是这样,语法是什么?

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

我要注释掉下面的“发布”部分,直到我的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."
    }
  }
}

2
每当您对Jenkinsfiles遇到此类语言问题时,请记住它们主要是GROOVY脚本。因此,如果添加关键字“ groovy”而不是“ Jenkinsfile”,则可能会发现更多结果。
劳尔·萨利纳斯-蒙塔古多

Answers:


282

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

/* this
   is a
   multi-line comment */

// this is a single line comment

3
我在sh节内的jenkinsfile上使用声明性管道,但它失败了,也许它可以在块级使用。pipeline { ... stage('Set Tagging') { steps { sh ''' echo "env.m_time='$m_time'" > ${params_file} echo "env.m_comp_tag='${BRANCH_NAME}_${m_time}_${BUILD_NUMBER}'" >> ${params_file} /* echo "env.docker_ws='/usr/local/lib/node_modules/${repo}'" >> ${params_file} */ ''' < 导致错误/ bash权限错误
Chen

6
sh部分中,您需要使用shell注释字符:#
BMitch

18

您可以为每行使用块(/ *** /)或单行注释(//)。您应该在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
                  '''
                }
            }
        }


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风格的注释被忽略了。因此,即使将您的代码复制/重命名JenkinsfileJenkinsfile.groovy,也不会得到很多有用的输出。

我希望能够使用

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

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

groovydoc 但是Jenkinsfile如果传递-private给命令,它将处理您中定义的类和方法。


1

负责人詹金斯文件只提到类似下面的一行命令:

// Declarative //

和(请参阅

pipeline {
    /* insert Declarative Pipeline here */
}

Jenkinsfile的语法基于Groovy,因此也可以对注释使用groovy语法。引用:

/* 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
 */
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.