在 Jenkins 预构建阶段通过 ssh 访问托管文件、本地文件和远程文件

Access managed files, local files and remote files ssh during Jenkins pre-build stages

我有一个参数化构建,我想根据 files/directories 的内容填充参数值,在本地从 and/or 上,在可通过 ssh 访问的远程机器上。

在构建阶段访问本地和远程文件不是问题,但我需要让它在 Active Choice 插件(或类似插件)中工作。

显然,sh 函数不起作用,但一些 Java-like Groovy API 仍然可用(如此处所述:https://wiki.jenkins.io/display/JENKINS/Active+Choices+Plugin

jenkinsURL=jenkins.model.Jenkins.instance.getRootUrl()

def propFile=vPropFile //name of properties file
def propKey=vPropKey // name of properties key
def relPropFileUrl=vRelPropFileUrl // userContent/properties/
def propAddress="${jenkinsURL}${relPropFileUrl}$propFile"
def props= new Properties()
props.load(new URL(propAddress).openStream())
def choices=[]

props.get(propKey.toString()).split(",").each{
    choices.add(it)
}

return choices

我想知道是否可以使用 SSH 以相同或更好的方式远程访问某些内容来访问托管文件。

有 API 吗?

我找不到在 Active Choices 参数脚本执行期间允许 SSH 的解决方案。

但是,我能够使用由 Jenkins 管理的配置文件。下面是来自 Active Choices 参数脚本的 运行 代码:

def gcf = org.jenkinsci.plugins.configfiles.GlobalConfigFiles.get()
// Read different file based on referencedParameter ENVIRONMENT
def deploymentFileName = 'deployment.' + ENVIRONMENT + '.properties'
def deploymentFile = gcf.getById(deploymentFileName)
def deploymentProperties = new Properties();
deploymentProperties.load(new java.io.StringReader(deploymentFile.content))
def choices = []
// Make use of Properties object here to return list of choices
return choices

稍后在管道的主要 Groovy 脚本中,可以用相同的方式更新文件,但由于脚本上下文不同,文件必须再次 read/loaded:

def gcf = org.jenkinsci.plugins.configfiles.GlobalConfigFiles.get()
def deploymentFile = gcf.getById(deploymentFileName)
def deploymentProperties.load(new java.io.StringReader(deploymentFile.content))

// Update deploymentProperties as necessary here.

def stringWriter = new java.io.StringWriter()
deploymentProperties.store(stringWriter, "comments")

// Content of the deploymentFile object is immutable.
// So need to create new instance and reuse the same file id to overwrite the file.
def newDeploymentFile = deploymentFile.getDescriptor().newConfig(
    deploymentFile.id, deploymentFile.name, deploymentFile.comment, stringWriter.toString())
gcf.save(newDeploymentFile)

当然,必须在 Jenkins 中授予所有必要的 API 权限。