jenkins 使用 REST api 下载工件

问题描述 投票:0回答:2

有一个 Maven 项目已配置为保留最后 5 个构建并丢弃旧的构建,因此在访问 uri 时:

http://localhost:8080/jenkins/job/jobname/buildId/api/json

神器为空

"artifacts":[]

有什么方法可以下载上次成功构建的工件吗?

注: 文物出现在这个位置:

http://localhost:8080/jenkins/job/jobName/default/ws/

java rest jenkins continuous-integration
2个回答
0
投票

试试这个网址 http://localhost:8080/jenkins/job/jobName/lastSuccessfulBuild/artifact/


0
投票

最近需要这样做,我想我应该在这里添加我的答案。可能有更简单的方法可以做到这一点,但我没有在任何地方看到它们的记录。希望这至少可以帮助人们开始。

我使用了 jenkins-API 文档中提到的 jenkins-rest 库:

//  Added to my build.gradle dependencies     
classpath "io.github.cdancy:jenkins-rest:1.0.2"
classpath "commons-io:commons-io:2.6"
import org.apache.commons.io.FileUtils
import com.cdancy.jenkins.rest.JenkinsClient
import com.cdancy.jenkins.rest.domain.job.JobInfo
import com.cdancy.jenkins.rest.domain.job.BuildInfo
import com.cdancy.jenkins.rest.features.JobsApi



//  The name of the job whose build artifacts we want
String jobName = "jobName"


//  Create connection to server
JenkinsClient client = JenkinsClient.builder().endPoint("https://your.jenkins.server:8443")
     .credentials("USERNAME:API_TOKEN")
     .build()


//  Create object to interact with jobs
JobsApi jobsApi = client.api().jobsApi()


//  Find the number of the lastSuccessfull build.
JobInfo info = jobsApi.jobInfo(null, jobName)
BuildInfo buildInfo = info.lastSuccessfulBuild()
int buildNumber = buildInfo.number


//  Downloads all artifacts from build as a .zip directory
File outputFile = new File(DOWNLOAD_PATH, "archive.zip")
outputFile.createNewFile()
try (InputStream inputStream = jobsApi.artifact(null, jobName, buildNumber, '*zip*/archive.zip')) {
     println("Downloading artifact")
     FileUtils.copyInputStreamToFile(inputStream, outputFile)
}
© www.soinside.com 2019 - 2024. All rights reserved.