How to use TeamCity with the Play Framework

At Q42 we use TeamCity for our Continuous Integration. When working on a project created with the Play Framework we found out that it does not play well with Team City out of the box. Since there seems to be little information about this topic we will walk you through it.

Assumptions

For this tutorial, we assume the following:

  • TeamCity 6.5 is installed and configured on a Windows machine
  • Play 1.2.4

Configure the build agents

Install Play framework

Download and unzip the Play Framework on all your agents. For example: C:Toolsplay-1.2.4

Create one .bat file in C:Toolsplay-1.2.4

  • play-autotest-teamcity.bat
  • This batch file will first compile the play project and auto-test it. If compilation of your project fails or tests fail play will not return different exit codes. This is why we need to validate that the tests have been ran manually by checking for existing test result files.
  • ::this script should be called from your projects working directory

    ::run play to compile our project and let it test
    CALL C:Toolsplay-1.2.4play.bat auto-test

    ::verify that the unit tests have been ran
    @IF EXIST test-resultresult.passed @(
    @ECHO Play tests returned .passed file;
    EXIT 0;
    )
    @IF EXIST test-resultresult.failed @(
    @ECHO Play tests returned .failed file;
    EXIT 1;
    )

    @ECHO Compiling Play Framework project failed! No tests output exists in test-result folder. See build log for details. 1>&2
    EXIT 2;

Create agent requirement property

Open the installation folder of your build agents and modify theconfbuildAgent.properties file. Add the following on a newline:

env.frameworks.java.play=1.2.4

Save the file. TeamCity will catch on in a few minutes.

You are now ready to build Play projects

Create a new build configuration in TeamCity.

Step 1 — general settings

Set the following for your “Artifact paths”:

/test-result/*.html=>/test-results/

This will preserve all the generated test results (html files) as artifacts of the build.

Enable “Fail build if:”

  • “build process exit code is not zero”
  • “at least one test failed”
  • “an error message is logged by build runner”

Version Control

Set the usual Version Control Settings for your project.

NOTE: We assume that the root of the checkout directly contains your Play project (the folder in which your app and conf folder reside).

Build Steps

We will create 3 build steps to resolve the dependencies, compile and test and to publish the test results.

Build step 1: Resolve dependencies

  • Command Line runner
  • Leave Working directory empty.
  • Run Executable with parameters.
  • Command executable: C:Toolsplay-1.2.4play.bat
  • Command parameters: dependencies

This will download all your dependencies.

Build step 2: Compile and test

  • Command Line runner
  • Leave Working directory empty.
  • Run Executable with parameters.
  • Command executable: C:Toolsplay-1.2.4play-autotest-teamcity.bat
  • Command parameters: -

This will run the script we previously created on all the agents.

Add build feature — Xml Report processing

  • (Build steps tab in the wizard)
  • Report type: ANT JUnit
  • Monitoring Rules: +:test-result/TEST-*.xml

This will monitor if test result files are generated and will process them. By default TeamCity will fail the build if it finds tests which have failed.

Build triggering

Add some build triggers to your liking.

Agent Requirements

Add a new requirement and fill in the following:

  • Parameter name=env.frameworks.java.play
  • Condition=exists

Now only build agents on which you have defined this parameter in the properties will be able to build your config. In the list of available agents your agent should still be in the list of ‘available agents’ or TeamCity did not pickup your agent config change (or you forgot it?)

Save, run and be happy. When compilation fails the Compile and Test step will fail, if your unit tests fail the last step will fail.

Add code coverage

Play can integrate with Cobertura to create code coverage reports which TeamCity understands.

Add Cobertura to Play Dependencies

Add cobertura to your Play dependencies.yml file. For example:

require:
- play
- play -> cobertura 2.4

NOTE: if you omit the version number it will not download cobertura (as per specs) but gives no warning about it.

Add the following artifact path to your build configuration in step 1 — general settings

/test-result/code-coverage/**=>/code-coverage-cobertura/

this will copy the cobertura output to the artifacts code-coverage-cobertura folder of your build

New Artifact-Based-Report tab

Configure your TeamCity server to have a conditional-artifact-based Report tab

Go to http://{your-team-city-server.com}/admin/serverConfig.html?init=1&tab=reportTabs

Add a new report tab

  • title: “Code Coverage (Cobertura)”
  • Base Path: code-coverage-cobertura/
  • Start Page: index.html

Now when the folder code-coverage-cobertura is available in the artifacts of your build the tab ‘Code Coverage (Cobertura)’ will be available and if you open it it will show the index.html file.

Credits


Check out our Engineering Blog for more in-depth articles on pragmatic code for happy users!