Skip to content

Commit

Permalink
[buildkite] Upload build artifact and add to build scan (elastic#100842)
Browse files Browse the repository at this point in the history
(cherry picked from commit 17ef0af)
  • Loading branch information
brianseeders committed Oct 13, 2023
1 parent f4dc18e commit d64fa22
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import java.nio.file.Files
String buildNumber = System.getenv('BUILD_NUMBER') ?: System.getenv('BUILDKITE_BUILD_NUMBER')
String performanceTest = System.getenv('BUILD_PERFORMANCE_TEST')
if (buildNumber && performanceTest == null && GradleUtils.isIncludedBuild(project) == false) {
File uploadFile = file("build/${buildNumber}.tar.bz2")
def uploadFilePath = "build/${buildNumber}.tar.bz2"
File uploadFile = file(uploadFilePath)
project.gradle.buildFinished { result ->
println "build complete, generating: $uploadFile"
if (uploadFile.exists()) {
Expand Down Expand Up @@ -64,5 +65,27 @@ if (buildNumber && performanceTest == null && GradleUtils.isIncludedBuild(projec
} catch (Exception e) {
logger.lifecycle("Failed to archive additional logs", e)
}

if (uploadFile.exists() && System.getenv("BUILDKITE") == "true") {
try {
println "Uploading buildkite artifact: ${uploadFilePath}..."
new ProcessBuilder("buildkite-agent", "artifact", "upload", uploadFilePath)
.start()
.waitFor()

println "Generating buildscan link for artifact..."

def process = new ProcessBuilder("buildkite-agent", "artifact", "search", uploadFilePath, "--step", System.getenv('BUILDKITE_JOB_ID'), "--format", "%i").start()
process.waitFor()
def artifactUuid = (process.text ?: "").trim()

println "Artifact UUID: ${artifactUuid}"
if (artifactUuid) {
buildScan.link 'Artifact Upload', "https://buildkite.com/organizations/elastic/pipelines/${System.getenv('BUILDKITE_PIPELINE_SLUG')}/builds/${buildNumber}/jobs/${System.getenv('BUILDKITE_JOB_ID')}/artifacts/${artifactUuid}"
}
} catch (Exception e) {
logger.lifecycle("Failed to upload buildkite artifact", e)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,17 @@ buildScan {
def branch = System.getenv('BUILDKITE_BRANCH')
def repoMatcher = System.getenv('BUILDKITE_REPO') =~ /(https:\/\/github\.com\/|git@github\.com:)(\S+)\.git/
def repository = repoMatcher.matches() ? repoMatcher.group(2) : "<unknown>"
def jobName = (System.getenv('BUILDKITE_LABEL') ?: '').replaceAll(/[^a-zA-Z0-9_\-]+/, '_').toLowerCase()

tag 'CI'
link 'CI Build', buildKiteUrl
value 'Job Number', System.getenv('BUILDKITE_BUILD_NUMBER')

value 'Pipeline', System.getenv('BUILDKITE_PIPELINE_SLUG')
tag System.getenv('BUILDKITE_PIPELINE_SLUG')

value 'Job Name', jobName
tag jobName

// Add SCM information
def prId = System.getenv('BUILDKITE_PULL_REQUEST')
Expand Down

0 comments on commit d64fa22

Please sign in to comment.