gpt4 book ai didi

linux - Jenkins (linux) 和 EnvInject on node (Windows) 文件访问

转载 作者:太空宇宙 更新时间:2023-11-04 12:03:12 25 4
gpt4 key购买 nike

我在 Linux 主机上安装了 Jenkins,并向该 Jenkins 安装添加了一个 Windows 节点。通过将标签应用于节点,我可以控制在哪个节点上构建项目。

我还在这个 Jenkins 服务器上使用 EnvInject 插件,并且(在多次尝试使用脚本内容之后)使用 Groovy 脚本来准备要在正在构建的项目中使用的版本号信息,并在 Jenkins 构建流程中使用能够跟踪构建的版本。对于后者,我需要读取一个存储版本号的文件。虽然这在主机与从机(Windows 机器)相同时有效,但是当项目构建在不同的 Windows 从机上时则不起作用。

然后我创建了一个单独的项目来调试正在发生的事情,最后得到了以下 Groovy 脚本:

def envVars = Thread.currentThread()?.executable.parent.builds[0].properties.get('envVars')
def line=""
def separator='/'
if (envVars['OS'] == 'Windows_NT') {
separator = '\\'
}
// Where am I?
println new File(".").getCanonicalPath().toString()
new File(envVars['WORKSPACE'] + separator + 'Input.txt').withReader { line = it.readLine() }
return [
SYSTEM: envVars['OS'],
WS: envVars['WORKSPACE'] + separator + 'input.txt',
TESTVAR: line,
]

这是这个的输出:

Started by user jadaml
[EnvInject] - Loading node environment variables.
Building remotely on Obsidian (Win32 Win32NT Win64 .NET WinNT Windows Win DotNet) in workspace D:\Jenkins\workspace\Rights Test
[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Evaluating the Groovy script content
/var/lib/jenkins
[EnvInject] - [ERROR] - Problems occurs on injecting env vars defined in the build wrapper: org.jenkinsci.lib.envinject.EnvInjectException: Failed to evaluate the script. java.io.FileNotFoundException: D:\Jenkins\workspace\Rights Test\Input.txt (No such file or directory). See system log for more info
Finished: FAILURE

如您所见,println new File(".").getCanonicalPath().toString() 行打印了 /var/lib/jenkins 表明Gradle 脚本在主机上运行,​​而不是在 Windows 从机上运行。

在这一点上,它归结为一个问题,即一个项目可能在运行 Jenkins 服务器的不同机器上构建并且可能必须读取远程文件,除非它在主机上运行。我该如何解决此限制?


我正在阅读 Jenkins java docs我觉得我正在乘坐“如此之近又如此之远”的过山车。

最佳答案

Jenkins java docs中摸索之后并在互联网上搜索,我得出的结论是,就我而言,我需要 FilePath对象,更准确地说是this constructor获取表示从站上远程文件的文件对象。

在我终于能够看到我需要的最后一 block 之后,我搜索并找到了this answer .这是我最终得到的实验脚本:

import hudson.*
import jenkins.model.*

def envVars = Thread.currentThread()?.executable.parent.builds[0].properties.get('envVars')
def line=''
def separator='/'
// Needs to be tested if this works on 'master'
def channel = null

if (envVars['OS'] == 'Windows_NT') {
separator = '\\'
}

if (envVars['NODE_NAME'] != 'master') {
channel = Jenkins.instance.getComputer(envVars['NODE_NAME']).channel
}

def file = new FilePath(channel, envVars['WORKSPACE'] + separator + 'input.txt')

// Just read the first line
file.read().withReader { line = it.readLine() }

return [
TESTVAR: line,
]

input.txt 文件是在工作区中手动创建的,其中有一行内容为“Wazup!”稍后可以在构建作业中从 TESTVAR 变量中读取。

附言:很抱歉提出了一个可能重复的问题,但最后我非常沮丧,甚至最终都没有意识到我错过了什么。

另一件事是,EnvInject 的 Groovy 的工作方式似乎与......无论他们在哪里编写此 Groovy 脚本都略有不同:一些变量不可用,例如 env 变量。

关于linux - Jenkins (linux) 和 EnvInject on node (Windows) 文件访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51616252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com