Tags:
Trigger and Postbuild ExamplesTable of Contents
When connecting to a CB server (same method for Trigger and Postbuild), you have to provide an URL and credentials for authentication. // connect and create a new session jf= CB.login("http://10.177.19.63:8080", "bond", "007") // 2. assert jf.user.name == "bond" // 3. assert jf.url == "http://10.177.19.63:8080"
There is a more descriptive way to specify parameter: jf = CB.login( url: "http://10.177.19.63:8080", login: "bond", password:"007") - or - jf = CB.login('http://bond:007@localhost:8081/cb') Accessing Session Information Some read-only meta information can be obtained about the session. jf = CB.login("http://localhost:8080", "bond", "007") assert jf.user.name == "bond" assert jf.url == "http://localhost:8080" assert jf.user.realName == "Default System Administrator" Working with Projects From a session object (e.g jf) a reference to a list of all projects can obtained. Since this is a collection, a closure makes it easy to iterate and find/select/filter one or more projects. The session object has the projectsproperty. The code above prints for each project its Id and name. jf.projects.each { println " id: ${it.id} name : ${it.name}" } This fragment finds the project with the name "Moose" and prints its id assert jf.projects.find{it.name == "Moose"}.id == 2374 Now, print out the name of the project with the id 2374 assert jf.projects.find{it.id == 2374}.name == "Moose" Find all the projects that are created by a user bond and print out the project name. The findAll call creates a sub-list, that later is being processed with each jf.projects.findAll{ it.createdBy.name == "bond" }.each{ println it.name } You can also obtain a reference to a project for later use. The project object will be used in the following examples. def project = jf.projects.find{it.name == "Moose"} TriggerA CBCI Trigger polls information from a codeBeamer server to trigger a build.
The Groovy script usually uses the CB Scripting API The codeBeamer Trigger provides a scripting text field as shown below:
You can use the privided trigger variables and code examples as shown in the following subsections. Adjust them to your needs. Provided Trigger VariablesThe trigger script may use the injected variables 'node','job','jenkins', 'plugin', and 'trigger' provided by Jenkins:
Check for implementation status and modification timeCheck which tasks in 'Feature' requests in the 'oms' project were implemented within the last 30 days. jf.projects.find{it.name == "oms"}.trackers.find{it.name == "Features"}.items.any{ it.status == "Implemented" && withinDays(it.modifiedAt, 30) } Find all the bugs of a certain status and age.Find all the bugs that are still not closed and were submitted before April 1st 2012 jf.projects.find{it.name == "oms"}.trackers.find {it.name == "Bugs"}.items.any{ it.status != "Closed" && it.submittedAt.before(new Date(2012, 1, 4)) } The project and the tracker is obtained using a find closure. Is any of the task items ready for a build?Check if in a project's Task tracker any of the items have the status "ReadyToBuild". project.trackers.find {it.name == "Task"}.items.any { it.status == "ReadyToBuild" } Use a file within 'Documents' as triggerIf the file (forcebuild.txt exists in the project's myproj Documents folder tmp and is not older than 45 minutes than trigger the build. import org.codehaus.groovy.runtime.TimeCategory cb = CB.login('http://bond:007@localhost:8081/cb') use(TimeCategory) { // find a file in Documents. def force = cb.project('myprj').documents('/tmp/forcebuild.txt') // use this file as a trigger it exists and is of a certain age. force.exists() && force.age < 45.minutes.toMilliseconds() } The example takes advatage of the TimeCategory DSL. Trigger based on Forum posts.If the Discussion forum contains a post with the subject Build please and the text has "ASAP" in it, and it was submitted today, .... then build in Jenkins.import org.codehaus.groovy.runtime.TimeCategory cb = CB.login('http://bond:007@localhost:8081/cb') use(TimeCategory) { /// find a file in Discussions. def dis = cb.project('myprj').forums.find{ it.name == 'Discussion'} dis.posts.any{ post -> post.subject.contains("Build please!") && post.description.contains("ASAP") && post.submittedAt.before(1.day.ago) } } PostbuildPostbuild publishes to a codeBeamer server using a Groovy script within Jenkins.Typically, the script posts to Tracker, Forums, and/or Documents according to the build result, puts badges next to the build in the build history and/or displays information on the build summary page. The codeBeamer Postbuild UI provides a scripting text field for a script to be executed after a build. Provided variables allow read access to Jenkins build resources and permit jenkins user interface operations. See below:
Provided Postbuild VariablesThe script may use injected variables provided by Jenkins.
Forum PublisherPost build info to a forum as a forum post. cb = CB.login('http://bond:007@localhost:8081/cb') def forum = cb.project('myprj').forums.find{it.name=='News'} forum.post(subject:"New Build ", description:"Build started: ${build.time}") 'build.time' is an internal variable provided by CBCI File upload in a Post buildUpload a file to a folder in Documents. Use the << operator to stream the file to codeBeamer. cb = CB.login('http://bond:007@localhost:8081/cb') def releaseFolder = cb.project('myprj').documents('/releases/build') // make the directory if not present releaseFolder.mkdirs() // file upload to the CB release folder def artifact = releaseFolder << "/od/tmp/Product.zip" Post build tracker item generation and file attachment.Here, we submit a new item to a tracker with all the build information and also attach the build product. jf = CB.login("http://localhost:8081/cb", "bond", "007") def issue = jf.tracker(6).submit( summary:"Build ", description:"${build.output}", format:CB.TEXT, status:" status : ${build.status}", duration:build.durationString ) issue.attach(file:"${build.workspace}/commons-product.zip", description:"buid product.") Note the use of the build variables, such as "build". |
Navigation Bar for CB-CI
Shortcuts
Report a bug Feature Request You must login to see this link. Register now, if you have no user account yet.
|