Intland's free requirements, development and test management hosting.
This server hosts 100.000+ users on the cloud!
Tags: 

Trigger and Postbuild Examples



Connecting to a Codebeamer Server

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"

  1. connect and obtain a session reference
  2. access the session fields

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"}

Trigger

A CBCI Trigger polls information from a codeBeamer server to trigger a build.

  1. A Scheduler is calling a Groovy Script in a defined interval.
  2. If the script evaluates to true, a build is triggered.

The Groovy script usually uses the CB Scripting API to access a codeBeamer server and queries the status of a project/tracker in some way.

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 Variables

The trigger script may use the injected variables 'node','job','jenkins', 'plugin', and 'trigger' provided by Jenkins:

Name Type Description Example

Job Properties:
node.name String The name of the node this build was executed on. 'master'
node.description String The description the node this build was executed on. 'the master Jenkins node'

Job Properties:
job.name String The name the build job. 'myjob'
job.description String The description of the build job. 'Example job'
job.url String The url of the build job. 'http://localhost:8080/job/myjob/'

Jenkins Properties:
jenkins.url String The jenkins/hudson server Url 'http://localhost:8080/'
jenkins.dir String The jenkins/hudson server dir '/work/cb/cb.jenkins/./work'
jenkins.version String The jenkins/hudson server version '1.424'

CB Plugin Properties:
plugin.name String The name of the CB CI plugin. 'cb'
plugin.longname String The longer name of the plugin. 'codeBeamer CI Plugin'
plugin.version String The version of the plugin. '5.8.0-SNAPSHOT (private-01/23/2012 20:39-od)'
plugin.url String The Url of the plugin's home page. 'http://wiki.jenkins-ci.org/display/JENKINS/codeBeamer+Plugin'

Trigger UI Methods:
trigger.println(String text)
Add a text to the Trigger Log.
trigger.print(String text)
Add a text to the Trigger Log.

Check for implementation status and modification time

Check 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 trigger

If 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)
    }
}

Postbuild

Postbuild 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 Variables

The script may use injected variables provided by Jenkins.

Name Type Description Example

Build Properties:
build.id String The id of the current build. '2012-01-23_19-58-00'
build.number int The current buld number '141'
build.url String The Url of the build 'http://localhost:8080/job/myjob/141/'
build.output String The build console output (stdout) 'build successful.'
build.outputfile File The file storing the build console output (stdout) '/tmp/out.log'
build.workspace String The build workspace path. '/work/cb/cb.jenkins/work/jobs/myjob/workspace'
build.result String The result of the build. 'SUCCESS' | 'FAILURE' | 'UNSTABLE' | 'ABORTED' | 'NOT_BUILD'
build.duration String The duration of the build. '28 ms and counting'
build.time Date The time the build started. 'Mon Jan 23 19:58:00 MST 2012'

Node Properties:
node.name String The name of the node this build was executed on. 'master'
node.description String The description the node this build was executed on. 'the master Jenkins node'

Job Properties:
job.name String The name the build job. 'myjob'
job.description String The description of the build job. 'Example job'
job.url String The url of the build job. 'http://localhost:8080/job/myjob/'

Jenkins Properties:
jenkins.url String The jenkins/hudson server Url 'http://localhost:8080/'
jenkins.dir String The jenkins/hudson server dir '/work/cb/cb.jenkins/./work'
jenkins.version String The jenkins/hudson server version '1.424'

CB Plugin Properties:
plugin.name String The name of the CB CI plugin. 'cb'
plugin.longname String The longer name of the plugin. 'codeBeamer CI Plugin'
plugin.version String The version of the plugin. '5.8.0-SNAPSHOT'
plugin.url String The Url of the plugin's home page. 'http://wiki.jenkins-ci.org/display/JENKINS/codeBeamer+Plugin'

Build UI Methods:
build.textBadge(String text)
Add a text-only badge to the build.
build.textBadge(String text, String fgColor, String bgColor)
Add a text-only badge to the build with custom text and background color.
build.info(String text, String description)
Add a info badge and summary (). The title must not contain html tags, the description however can. the description is optional.
build.cbinfo(String text, String description)
Add a codebeamer info badge and summary (). The title must not contain html tags, the description however can. the description is optional.
build.warning(String text, String description))
Add a warning badge and summary () to the build. The title must not contain html tags, the description however can. the description is optional.
build.error(String text, String description)
Add an error badge and summary () to the build. The title must not contain html tags, the description however can. the description is optional.
build.markUnstable()
Marks the build unstable.
build.markFailure()
Marks the build as failed.
build.markSuccess()
Marks the build as success.
build.logContains(String regex)
returns true if the build log file contains a line matching regexp..

Forum Publisher

Post 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 build

Upload 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".