Jenkins:注释可以添加到Jenkins文件中吗?

评论可能在一个詹金斯档案?如果是,语法是什么?

我使用的是声明式管道语法。

我想注释掉下面的“post”部分,直到我的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."
}
}
}
182717 次浏览

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

/* this
is a
multi-line comment */


// this is a single line comment

注释在任何常见的Java/Groovy表单中都可以正常工作,但是您不能目前使用groovydoc来处理您的Jenkinsfile (s)。

首先,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

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

我希望能够使用a

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

评论在我的詹金斯档案开始。(目前)还没有这样的运气。

不过,如果将-private传递给命令,则groovydoc 处理在Jenkinsfile中定义的类和方法。

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

官方Jenkins文档只提到单行命令,如下所示:

// 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
*/