Bamboo 2.1 : Tutorial 1 - Getting Started with a Simple Post Build Labeller
This page last changed on Aug 21, 2008 by bmccoy.
The source code to the plugin used in this tutorial is available on the Atlassian public source repository. You can check out the source code here Step 1 - Setting up the project.The first thing you need to do is to set up your Bamboo Plugin project and source directories. See the Getting started with Atlassian plugins guide and the The Bamboo Plugin Guide for instructions. In the atlassian-plugin.xml located under /src/main/resources/, you will need to give the plugin a unique key, as well as some meta information about this plugin. As our plugin simply labels, we have called it "labeller". Below is the atlassian-plugin.xml for our labelling plugin: <atlassian-plugin key="com.atlassian.bamboo.plugin.labeller" name="Build Labeller"> <plugin-info> <description>Bamboo Labeller</description> <version>1.0</version> <application-version min="1.0" max="1.0"/> <vendor name="Atlassian Software Systems Pty Ltd" url="http://www.atlassian.com"/> </plugin-info> </atlassian-plugin> Now we are ready to move onto writing some code to make our plugin do something. Step 2 - Adding the first Build Complete Labeller ModuleIn this plugin, we want Bamboo to perform a custom action immediately after a build has completed. To do this, we write a Build Complete Action Module. You can see all the available Bamboo module types here. To start things off, we would like to keep our custom action pretty simple and make sure things work. Our first cut of the BuildLabeller will simply label the build as "out_of_memory" if the "OutOfMemoryError" was found in the logs. public class BuildLabeller implements CustomBuildCompleteAction { private static final Logger log = Logger.getLogger(BuildLabeller.class); /** * Dependency on labelManager. Bamboo's Spring IOC will automatically inject manager * into this class via the setter. */ private LabelManager labelManager; /*** * This action will run after a build has completed. * * The build will be labelled with "out_of_memory" if the "OutOfMemoryError" was dected in the logs. * * @param build * @param buildResults */ public void run(Build build, BuildResults buildResults) { List logs = buildResults.getBuildLog(); for (Iterator iterator = logs.iterator(); iterator.hasNext();) { SimpleLogEntry log = (SimpleLogEntry) iterator.next(); if(log.getLog().indexOf("OutOfMemoryError") != -1) { getLabelManager().addLabel("out_of_memory", buildResults, null); break; } } } /** * This method is used to validate a build configuration for a build plan * * This is used if the CustomBuildCompleteAction needs to have configuration stored * against the build plan. * * @param buildConfiguration * @return */ public ErrorCollection validate(BuildConfiguration buildConfiguration) { return null; } // ----------------------------------------------------------------------------------------------- Getters & Setters public LabelManager getLabelManager() { return labelManager; } public void setLabelManager(LabelManager labelManager) { this.labelManager = labelManager; } } Our custom module must implement the CustomBuildCompleteAction interface, which defines a run method and a validate method. The run method is what gets called when a build completes. Our run method in this plugin is fairly simple. It loops through each line of the build logs and searches for the exact string - "OutOfMemoryError". Once found, it stops looping and labels the build. In the run method, we make use of the services of the LabelManager (a dependency), which is responsible for tagging of a build. Dependencies in plugins are automatically handled by Bamboo Spring container. As long as the plugin has the correct "setter" method, the dependency will be automatically injected. You may notice that the other method defined by the CustomBuildCompleteAction interface: validate currently doesn't do anything. We will return to this in the next tutorial. Step 3 - Registering the Build Complete Labeller ModuleOnce you have written your labeller module, we must now register the plugin module into our plugin descriptor (atlassian-plugin.xml). <buildCompleteAction key="labeller" name="Build Labeller" class="com.atlassian.bamboo.plugins.labeller.BuildLabeller"> <description>An automatic labelling plugin.</description> </buildCompleteAction> Step 4 - Build and TestThat's it. We now need to test our code. To do this, we can build our plugin by returning to the command line in the root directory of your source directory, and run the command: mvn package. This created a bamboo-labeller-plugin-1-1.0.jar. We can now drop this into Bamboo (/webapp/WEB-INF/lib), and see it in action. Here is what our plugin produced after we ran a build with a OutOfMemoryError: Next StepsSo we have made our first basic plugin. Right now, it's not very configurable, and runs for every build. In the next tutorial, we will introduce configurability to our Labeller. RELATED TOPICS
Bamboo Documentation Home |
![]() |
Document generated by Confluence on Mar 02, 2009 18:52 |