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. The instructions for how to do this are available here.
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:
Now we are ready to move onto writing some code to make our plugin do something.
Step 2 - Adding the first Build Complete Labeller Module
In 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.
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 Module
Once you have written your labeller module, we must now register the plugin module into our plugin descriptor (atlassian-plugin.xml).
Step 4 - Build and Test
That'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 Steps
So 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






