gpt4 book ai didi

jenkins - 如何使用 Pipeline 引用 Jenkinsfile 目录?

转载 作者:行者123 更新时间:2023-12-03 08:49:41 25 4
gpt4 key购买 nike

我有一个 groovy 文件,我想从 Jenkinsfile 运行。

IE。 load script.groovy
但是,如果它与 Jenkinsfile 存储在同一目录中,我不确定如何引用该文件。我正在从 git 加载 Jenkinsfile。我注意到它创建了一个名为 workspace@script 的文件夹.它不会将它放在工作区目录中。我可以对文件夹进行硬编码,但我不确定这方面的规则,再次 checkout 代码似乎有点多余。

java.io.FileNotFoundException: /opt/jenkins_home/jobs/my_job/workspace/script.groovy (No such file or directory)

默认情况下,它从工作区加载,而不是 workspace@script
我正在尝试将 BuildFlow 脚本转换为管道(工作流)脚本。但我发现,它不像复制和粘贴那么容易。

Jenkins 文件
node {

//get parameters from Job
def builds = builds.tokenize(",")
def ip_address_node = ip_address_node.trim()
def port_node = port_node.trim()
def branch = branch.trim()
def workspace = pwd()

stage 'Checking out code from esb repository'
git branch: branch, url: 'ssh://git@giturl/integration_bus.git'

load '../workspace@script/esb_deploybar_pipeline/deploy_esb.groovy'

}

deploy_esb.groovy(这是来自旧的构建流程,试图在管道中运行)
import groovy.transform.ToString
import groovy.transform.EqualsAndHashCode
@EqualsAndHashCode
@ToString
class BarDeploy {
String barFile
String app
String integrationServer
}


//parse csv
def csvItemsApps = new HashSet<BarDeploy>();
def csvItemsLibs = new HashSet<BarDeploy>();
def deploymentMapFile = new File(workspace + "/ESB_Deployment_Map.csv")
def isFirstLine = true

stage 'Parsing ESB Deployment CSV'
deploymentMapFile.withReader { reader ->
while(line = reader.readLine()) {
if(isFirstLine)
{
isFirstLine = false
continue
}

csvLine = line.split(",")
app = csvLine[0]
intServer = csvLine[1]

def barDeploy = new BarDeploy()
barDeploy.app = app
barDeploy.integrationServer = intServer
csvItemsApps.add(barDeploy)


//get shared libs
if(csvLine.length > 2 && csvLine[2] != null)
{
def sharedLibs = csvLine[2].split(";")
sharedLibs.each { libString ->
if(!libString.isAllWhitespace())
{
def lib = new BarDeploy()
lib.app = libString
lib.integrationServer = intServer
csvItemsLibs.add(lib)
}
};
}
}
};

//get list of bar files to deploy from html and consolidate bar files to deploy with apps in csv
for (int i = 0; i < builds.size(); i+=3)
{
if(builds[i].equals("false"))
{
//Don't deploy bar if checkbox isn't selected
continue
}

foundInCSV = false

appToDeploy = builds[i + 1]
barFileToDeploy = builds[i + 2]

iterator = csvItemsApps.iterator()
while (iterator.hasNext())
{
barDeploy = iterator.next()
if(appToDeploy.equalsIgnoreCase(barDeploy.app))
{
barDeploy.barFile = barFileToDeploy
foundInCSV = true
}
}

iterator = csvItemsLibs.iterator()
while (iterator.hasNext())
{
barDeploy = iterator.next()
if(appToDeploy.equalsIgnoreCase(barDeploy.app))
{
barDeploy.barFile = barFileToDeploy
foundInCSV = true
}
}

if(foundInCSV == false)
{
throw new RuntimeException("App: " + appToDeploy + " not found in ESB_Deployment_Map.csv. Please add CSV Entry.")
}
}


//Do deploy, deploy shared libs first
deployCSVItemsInParallel(ip_address_node,port_node,branch,env_key,csvItemsLibs)
deployCSVItemsInParallel(ip_address_node,port_node,branch,env_key,csvItemsApps)


def deploy(ip_address_node,port_node,branch,deployItem,env_key)
{
def integrationServer = deployItem.integrationServer
def app = deployItem.app
def barFile = deployItem.barFile

if(barFile == null)
{
return;
}

println("Triggering Build -> ESB App = " + app + ", Branch = "
+ branch + ", Barfile: " + barFile + ", Integration Server = " + integrationServer + ", IP Address: " + ip_address_node
+ ", Port: " + port_node + ", Env_Key: " + env_key)

build_closure = { ->
build("esb_deploybar",
ip_address_node: ip_address_node, port_node: port_node,
integrationServer: integrationServer, branch: branch, app: app, barFile: barFile, env_key: env_key)
}

return build_closure
}

def deployCSVItemsInParallel(ip_address_node,port_node,branch,env_key,csvItems)
{
def build_closures = []
iterator = csvItems.iterator()
while (iterator.hasNext())
{
barDeploy = iterator.next()
def build_closure = deploy(ip_address_node,port_node,branch,barDeploy,env_key)

if(build_closure != null)
{
build_closures.add(build_closure)
}
}

if(build_closures?.size() > 0)
{
parallel(build_closures)
}
}

最佳答案

有一种情况我没有看到任何人提到。当作业应该在 Jenkins 上运行时,如何加载 Groovy 脚本 代理/奴隶 ,而不是在 大师 .

由于 master 是从 SCM checkout Jenkins 管道项目的人, Groovy 脚本只能在主文件系统中找到 .所以虽然这会起作用:

node {       
def workspace = pwd()
def Bar = load "${workspace}@script/Bar.groovy"
Bar.doSomething()
}

这只是一个愉快的巧合,因为从 SCM 克隆管道的节点与尝试在其中加载 groovy 脚本的节点相同。但是,只需添加要在其上执行的不同代理的名称:
node("agent1"){
def workspace = pwd()
def Bar = load "${workspace}@script/Bar.groovy"
Bar.doSomething()
}

将失败,导致:
java.io.IOException: java.io.FileNotFoundException: /Jenkins/workspace/Foo_Job@script/Bar.groovy (No such file or directory)

这是因为这条路径:
/Jenkins/workspace/Foo_Job@script/

仅存在于主 Jenkins 盒子上。不在运行 的框中代理1 .

因此,如果您遇到此问题,请确保将 groovy 脚本从 master 加载到全局声明的变量中,以便代理可以使用它们:
def Bar
node {
def workspace = pwd()
if(isUnix()){
Bar = load "${workspace}@script/Bar.groovy"
}
else{
Bar = load("..\\workspace@script\\Bar.groovy")
}
}
node("agent1"){
Bar.doSomething()
}

注: 变量 用于在节点之间传递模块必须声明 节点 block 。

关于jenkins - 如何使用 Pipeline 引用 Jenkinsfile 目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37353963/

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