Skip to content

Instantly share code, notes, and snippets.

@tintinweb
Last active May 29, 2016 23:29
Show Gist options
  • Save tintinweb/89c1cf0c6a97e0d00b42a3e9cdfbd5cc to your computer and use it in GitHub Desktop.
Save tintinweb/89c1cf0c6a97e0d00b42a3e9cdfbd5cc to your computer and use it in GitHub Desktop.
Jenkins PostBuild groovy script to access current run result/parameters/environment, save it as json and post it to mongodb.
/*
Title : Jenkins PostBuild groovy script to get run status/parameters/environment, save to a json file in order to post it to mongodb
Author : [email protected] <github.com/tintinweb>
Setup : Jenkins
+ https://wiki.jenkins-ci.org/display/JENKINS/Groovy+Postbuild+Plugin
+ https://wiki.jenkins-ci.org/display/JENKINS/MongoDB+Document+Upload+Plugin
Job:
add post-build action: Groovy Postbuild Plugin (this code)
add post-build action: MongoDB Document Upload: Filename=buildinfo.json
This script stores the run result and parameters in <workspace>/buildinfo.json. This file can be pushed to a mongodb using the MongoDB Document Upload postbuild action.
*/
import groovy.json.JsonBuilder
def build = manager.build
def data = [:]
//data['sysenv'] = [:]
data['env'] = [:]
data['parameters'] = [:]
data['result'] = "${manager.getResult()}"
data['build'] = "${build}"
//System.getenv().each { key, value -> data["sysenv"][key]=value}
manager.build.buildVariables.each { key, value -> data["parameters"][key]=value}
manager.envVars.each { key, value -> data["env"][key]=value}
def jsondata = new JsonBuilder(data)
def jsonfile = new File(manager.build.workspace.toString()+"/buildinfo.json")
jsonfile.write(jsondata.toPrettyString())
manager.addBadge("db_in.gif","mongodb")
manager.listener.logger.println jsondata
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment