繁体   English   中英

如何通过github通过jenkins读取文件

[英]How to read file through jenkins from github

我正在尝试使用readFile从 github 读取文件,


                        sshagent (credentials: ["${github_cred}"]) {
                        script {
                                sh """
                                    git checkout test_branch
                                """
                               def file = readFile(file: "/myrepo/${params.value}.txt") 

但在其他情况下,此文件对于传递的某些参数不可用。 所以我想检查该文件是否存在于 github 中,如果可用则继续下一步,否则它应该跳过该阶段。

第一次尝试:

当我尝试使用上面的代码时,它在不可用时抛出NoSuchFileException 我尝试使用fileExists function,它实际上仅适用于 controller 节点,而不适用于 github。是否有可能实现这一点?

第二次尝试:

我也尝试使用git show如下,但illegal string body or character after dollar sign ,我不知道这里出了什么问题。

git show HEAD~1:/myrepo/"${params.value}".txt > /dev/null 2>&1

Jenkins机器是windows还是macos还是linux?

sh """

pwd
git checkout test_branch
"""

如果是 linux 或 macos 添加 pwd 以查看 repo 的本地完整路径然后在读取文件中使用此完整路径

fileExists应该在运行 Pipeline 的当前节点中运行。 所以它应该理想地工作。

if (fileExists("/myrepo/${params.value}.txt") {
   def file = readFile(file: "/myrepo/${params.value}.txt") 
}

另一个简单的解决方法是用 try-cath 包装你的readFile ,因为你知道如果文件不可用,readFile 将失败。

def isSuccess = true
def file = null

try {
 file = readFile(file: "/myrepo/${params.value}.txt") 
}catch(e) {
 isSuccess = false
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM