简体   繁体   English

在Groovy和Jenkins管道构建中使用变量

[英]Using variables in Groovy and Jenkins pipeline build

I have a Jenkins pipeline build that needs to obtain the commit id (sha), so from what i can see the only way to get this is by doing the following 我有一个需要获取提交ID(sha)的Jenkins管道构建,因此从我可以看到的唯一方法是通过执行以下操作

sh "git rev-parse --long HEAD > .git/commit-id"                        
GIT_COMMIT = readFile('.git/commit-id')

I need to use the GIT_COMMIT var within a curl command but am having trouble using it 我需要在curl命令中使用GIT_COMMIT var,但是在使用它时遇到了麻烦

sh '''curl "https://api.github.com/repos/myRepo/myBuild/statuses/${GIT_COMMIT}"}"''' 
sh '''curl "https://api.github.com/repos/myRepo/myBuild/statuses/GIT_COMMIT"}"'''

At the moment GIT_COMMIT is not being passed through, it is always blank, but yet if i echo it i get the value back 目前未传递GIT_COMMIT,它始终为空,但是如果我回显它,我会得到返回的值

echo GIT_COMMIT
--long
12345678910

Can anyone advise on how i achieve what i require please 任何人都可以建议我如何实现我的要求

Thanks 谢谢

UPDATE UPDATE

I have tried the following but still no luck 我尝试了以下方法,但还是没有运气

sh '''GIT_COMMIT=$(git rev-parse HEAD)'''
sh '''curl "https://api.github.com/repos/myBuild/statuses/$GIT_COMMIT"'''

Unless you have a need for assigning the commit hash to a variable, you can do this in a single line without outputting to a file: 除非您需要将提交哈希分配给变量,否则您可以在一行中完成此操作而无需输出到文件:

sh "curl https://api.github.com/repos/myRepo/myBuild/statuses/\$(git rev-parse HEAD)"

Keep in mind that triple quotes are only necessary for multiline strings in Groovy http://docs.groovy-lang.org/latest/html/documentation/index.html#_triple_double_quoted_string 请记住,仅对于Groovy中的多行字符串,必须使用三引号引起来http://docs.groovy-lang.org/latest/html/documentation/index.html#_triple_double_quoted_string

If you want to do away with having to write to a file to keep the commit hash you can use returnStdout: true on sh and pass it directly. 如果您不想写文件来保留提交哈希,则可以在sh上使用returnStdout: true并将其直接传递。

GIT_COMMIT = sh script: 'git rev-parse HEAD',  returnStdout: true
sh """curl "https://api.github.com/repos/myBuild/statuses/${GIT_COMMIT}""""

Variable/expression expansion (interpolation) only works in Groovy for certain types of string literal syntax. 变量/表达式扩展(插值)仅在Groovy中适用于某些类型的字符串文字语法。 The syntax you are using: 您使用的语法:

'''content${interpolation}etc'''

is not one of them. 不是其中之一。 Instead replace the triple-single-quotes ( ''' ) with triple-double-quotes ( """ ), like this: 而是用三重双引号( """ )替换三重单引号( ''' ),如下所示:

sh """curl "https://api.github.com/repos/myRepo/myBuild/statuses/${GIT_COMMIT}"}""""

EDIT: I'm also a bit concerned that there may still be syntax errors in this statement, particularly the extra }" at the end. Maybe the following is more like what you really need?: 编辑:我也有点担心,该语句中可能仍然存在语法错误,尤其是末尾的多余}" 。以下内容可能更像您真正需要的内容:

sh """curl "https://api.github.com/repos/myRepo/myBuild/statuses/${GIT_COMMIT}""""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jenkins管道环境变量 - Jenkins pipeline environment variables 无法使用 Groovy Jenkinsfile 读取 Git 环境变量 Jenkins - Not able to read Git Environment variables Jenkins using Groovy Jenkinsfile Jenkins Groovy(脚本)管道的Git轮询设置 - Git poll setup for Jenkins groovy (scripted) pipeline 在Jenkins Pipeline中使用全局变量将标签推送到Git - Using global variables to push tags to Git in Jenkins Pipeline Jenkins-多分支流水线在推送上构建 - Jenkins - Multibranch pipeline build on push 自上次成功构建以来,如何使用Groovy获取有关Jenkins的提交信息? - How to get commits information on Jenkins using Groovy since last successful build? Jenkins Pipeline Groovy脚本:批处理脚本问题'%20'被替换为'0' - Jenkins Pipeline Groovy script: batch script issue '%20' is replaced with '0' 是否有正确的方法在 jenkins 管道 groovy 脚本中调用 git clean ? - Is there a proper way to call git clean in jenkins pipeline groovy script? 在 Jenkins 管道 groovy 脚本中转义 git 密码的特殊字符 - Escape special character of git password in Jenkins pipeline groovy script Jenkins Pipeline基于groovy:无法推入git => Permission denied(publickey) - Jenkins Pipeline based on groovy : unable to push into git => Permission denied (publickey)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM