简体   繁体   中英

Get BuildInfo From Artifactory Using Jenkins

Using Jenkins DSL I can create and publish build info using Artifactory.newBuildInfo but am looking for the complementary method to read the BuildInfo JSON data that is generated on Artifactory. Have trolled through many resources. Any suggestions would be appreciated.

From Artifactory REST API it sure looks like you can retrieve buildInfo. I'd expect this must be exposed from the jenkins plugin as well.

Build Info
Description: Build Info
Since: 2.2.0
Security: Requires a privileged user with deploy permissions (can be anonymous)
Usage: GET /api/build/{buildName}/{buildNumber}
Produces: application/vnd.org.jfrog.build.BuildInfo+json
...

JFrog's project examples on github is a fabulous resource as is their jenkins plugin

From a quick search it looks like you'd define a download spec and then use server.download method (see Working with Pipeline Jobs in Jenkins

def buildInfo1 = server.download downloadSpec

The previous answer creates a new buildInfo, it does not download the original buildInfo into I've been trying for days to try to figure out how to do what the original poster wants to do. The best I've succeeded at is downloading the buildinfo into a hashtable, work with that, then upload the changes doing REST calls.

                    def curlstr = "curl -H 'X-JFrog-Art-Api:${password}' ${arturl}api/build/${buildName}/${buildNumber}"
                    def buildInfoString = sh(
                            script: curlstr,
                            returnStdout: true
                    ).trim()
                    buildInfo = (new JsonSlurperClassic().parseText(buildInfoString))
                    sh("echo '${JsonOutput.toJson(buildInfo)}'|curl -XPUT  -H 'X-JFrog-Art-Api:${password}' -H 'Content-Type: application/json'  ${arturl}api/build --upload-file - ")

I was able to modify the buildInfo in the artifactory repository using this technique. Not as clean as I would like. I've been unable to get the jfrogCLI to modify existing buildInfo files either.

For whatever it's worth the intent of what I'm trying to do is promote a docker artifact and change the name while doing it. There is no way I've found to express this to artifactory not involving downloading the artifact to docker and then pushing it again. I'd love it if someone from @jfrog could clue me in how to do it.

UPDATE: Attention! I've got the question wrong. This is how you get the local BuildInfo-Object in a declarative pipeline script.


I managed this by using an internal api from jenkins-artifactory-plugin.

    // found in org.jfrog.hudson.pipeline.declarative.utils.DeclarativePipelineUtils

     /**
     * Get build info as defined in previous rtBuildInfo{...} scope.
     *
     * @param rootWs            - Step's root workspace.
     * @param build             - Step's build.
     * @param customBuildName   - Step's custom build name if exist.
     * @param customBuildNumber - Step's custom build number if exist.
     * @return build info object as defined in previous rtBuildInfo{...} scope or a new build info.
     */
    public static BuildInfo getBuildInfo(FilePath rootWs, Run<?, ?> build, String customBuildName, String customBuildNumber, String project) throws IOException, InterruptedException {
      ...
    }

Whith this code you can fetch the BuildInfo inside a declarative pipeline script step.

def buildInfo = org.jfrog.hudson.pipeline.declarative.utils.DeclarativePipelineUtils.getBuildInfo(new hudson.FilePath(new java.io.File(env.WORKSPACE)), currentBuild.rawBuild, null, null, null);

UPDATE: Beware of custom build names and numbers. I you have defined a custom build name and/or build number, you have to provide it with the getBuildInfo call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM