groovy Jenkins做了几个工作并得到了他们的结果

4ktjp1zp  于 2022-11-21  发布在  Jenkins
关注(0)|答案(1)|浏览(135)

我这样运行作业:

def job = build job: job_name,  parameters:
  [
    string(name: 'Param1', value: Value1),
    string(name: 'Param2', value: Value2),
    string(name: 'Param3', value: Value3),
  ], wait: false
  
  return job

我使用wait: false是因为我需要同时运行几个任务,然后等待它们全部完成。但在本例中,作业为空,因为我使用wait: false
也许你知道其他方法来运行乘法作业在同一时间,并获得他们的对象?
我尝试其他方法:

def buildsNumberStart = GetLastDockerBuildNumber(DockerBuildJobName)
def finishedJobs = overrideBranches.collect { item -> RunJobBuild(item.Tag, author, GetImageNameFromRepository(item.DockerRepository), item.Name, item.Branch, item.Repository, DockerBuildJobName)}
sleep(30)
def buildNumberFinish = GetLastDockerBuildNumber(DockerBuildJobName)
        
while(true)
{
  if (IsAllDockerBuildJobsFinished(buildsNumberStart, buildNumberFinish, DockerBuildJobName))
  {
    break;
  }
        
  sleep(5)
}
        
for (build in GetFinishedJobs(buildsNumberStart, buildNumberFinish, DockerBuildJobName))
{
  def listener = build.getListener()
  build
    .getEnvironment(listener)
    .each
    {
      // HERE IS PROBLEM, I saw just environment variables that
      // set on start and didn`t see that I added in pipeline 
      println it
    }
 }
}

@NonCPS
def GetFinishedJobs(buildNumbersStart, buildNumberFinish, String dockerBuildJobName)
{
  return jenkins.model.Jenkins
    .instance
    .getItemByFullName(dockerBuildJobName)
    .builds
    .findAll { it.number > buildNumbersStart && it.number <= buildNumberFinish}
}
ffvjumwh

ffvjumwh1#

我发现我的案例的解决方案https://stackoverflow.com/a/40148397/14392639如下所示:

def jobs = [:]
some_source.each
{
  item -> jobs[some_source.Name] = build job: job_name,  parameters:
  [
    string(name: 'Param1', value: Value1),
    string(name: 'Param2', value: Value2),
    string(name: 'Param3', value: Value3),
  ]
}
  
parallel jobs

相关问题