FTL Access Artifacts

There doesn't seem to be a lot of documentation on how to use the FTL's for customizing notifications. I've poured over google and this forum for any references but haven't had much luck.

Does anyone know how to reference the artifacts in a build? One of the artifacts generated is an HTML file that I would like to include in the email as a link. Any help is much appreciated.

Thanks

3 comments
Comment actions Permalink

I've actually came here looking for the exact same functionality. I found the getArtifacts function which should provide the required data but the problem is this function requires a complex argument (BuildArtifactsViewMode) which I don't really understand how to pass.
It should look something like -
<#list build.getArtifacts(VIEW_DEFAULT) as artifact>
do something with artifact
</#list>

Any help will be appreciated.

0
Comment actions Permalink

I ended up writing a plug-in to extend the notification template model

//
// MyCompanyNotifications.java
//
// This class is a TeamCity plug-in for extending the Freemarker root model so
// that we can incorporate the mycompanybuild.html file as part of the email we
// send out.
//
// We tried using the Freemarker <#include> item, but as of TeamCity 8 it is
// restricted to including files in the notifications template directory and
// cannot access artifact files.
//
// The TeamCity Freemarker files for email are located on the server in the
// directory:
//
//    D:\ProgramData\JetBrains\TeamCity\config\_notifications\email
//
// To use this, modify the .ftl file(s) to contain this line:
//
//    ${mycompanynotifications.getFileContents( build.artifactsDirectory + "\\mycompanybuild.html" )}
//

package mycompany.teamcity.plugins;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.FileInputStream;
import java.lang.Exception;
import java.lang.StringBuilder;
import java.util.HashMap;
import java.util.Map;
import jetbrains.buildServer.notification.NotificationContext;
import jetbrains.buildServer.notification.TemplateProcessor;
//import jetbrains.buildServer.serverSide.SBuildServer;
import org.jetbrains.annotations.NotNull;
//import freemarker.ext.beans.BeansWrapper;

public class MyCompanyNotifications implements TemplateProcessor
{
    public static class MyCompanyReadFile
    {
        public String getFileContents( String fileName )
        {
            try
            {
                BufferedReader reader = new BufferedReader( new InputStreamReader( new FileInputStream( fileName ), "UTF-16" ) );
                String line = null;
                StringBuilder  stringBuilder = new StringBuilder();
                String ls = System.getProperty( "line.separator" );
                
                while( ( line = reader.readLine() ) != null )
                {
                    stringBuilder.append( line );
                    stringBuilder.append( ls );
                }
                
                return stringBuilder.toString();
            }
            catch( Exception e )
            {
                return "Can't read " + fileName;
            }
        }
    }
    
    @NotNull
    public Map< String, Object > fillModel( @NotNull NotificationContext context )
    {
        Map< String, Object > model = new HashMap< String, Object >();
        MyCompanyReadFile fileReader = new MyCompanyReadFile();
        model.put( "mycompanynotifications", fileReader );
        return model;
    }
}

And here is a copy of the Windows Batch file to build it:

@echo on
if not exist out mkdir out

REM Command uses Windows batch file continuation characters.  Note that there
REM cannot be any leading spaces on the lines with the .jar files.

javac ^
    -d ./out ^
    -g ^
    -J-Xmx128m ^
    -cp ^
D:\TeamCity\webapps\ROOT\WEB-INF\lib\annotations.jar;^
D:\TeamCity\webapps\ROOT\WEB-INF\lib\jdom.jar;^
D:\TeamCity\webapps\ROOT\WEB-INF\lib\openapi.jar;^
D:\TeamCity\webapps\ROOT\WEB-INF\lib\spring-webmvc.jar;^
D:\TeamCity\webapps\ROOT\WEB-INF\lib\spring.jar;^
D:\TeamCity\webapps\ROOT\WEB-INF\lib\util.jar;^
D:\TeamCity\DevPackage\server-api.jar;^
D:\TeamCity\devPackage\common-api.jar;^
D:\TeamCity\devPackage\runtime\runtime-util.jar;^
D:\TeamCity\lib\servlet-api.jar;^
D:\TeamCity\webapps\ROOT\WEB-INF\lib\freemarker.jar ^
    mycompany\TeamCity\Plugins\MyCompanyNotifications.java
if %errorlevel% neq 0 exit /b %errorlevel%

@echo off

@echo ^<?xml version="1.0" encoding="UTF-8"?^>> .\out\teamcity-plugin.xml
@echo ^<teamCity-plugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:shemas-jetbrains-com:teamcity-plugin-v1-xml"^>>> .\out\teamcity-plugin.xml
@echo   ^<info^>>> .\out\teamcity-plugin.xml
@echo     ^<name^>MyCompanyNotificationsPlugin^</name^>>> .\out\teamcity-plugin.xml
@echo     ^<display-name^>MyCompany Notifications Plugin^</display-name^>>> .\out\teamcity-plugin.xml
@echo     ^<description^>Allows Freemarker email notifier to reference contents of the mycompanybuild.html artifact^</description^>>> .\out\teamcity-plugin.xml
@echo     ^<version^>1.0^</version^>>> .\out\teamcity-plugin.xml
@echo     ^<vendor^>>> .\out\teamcity-plugin.xml
@echo       ^<name^>My Company^</name^>>> .\out\teamcity-plugin.xml
@echo       ^<url^>http://www.mycompany.com^</url^>>> .\out\teamcity-plugin.xml
@echo       ^<vendor^>>> .\out\teamcity-plugin.xml
@echo   ^</info^>>> .\out\teamcity-plugin.xml
@echo   ^<deployment use-separate-classloader="true" /^>>> .\out\teamcity-plugin.xml
@echo ^</teamcity-plugin^>>> .\out\teamcity-plugin.xml


if not exist .\out\META-INF mkdir .\out\META-INF

@echo ^<?xml version="1.0" encoding="UTF-8"?^>> .\out\META-INF\build-server-plugin-myCompanyNotifications.xml
@echo ^<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"^>>> .\out\META-INF\build-server-plugin-myCompanyNotifications.xml
@echo. >> .\out\META-INF\build-server-plugin-myCompanyNotifications.xml
@echo ^<beans default-autowire="constructor"^>>> .\out\META-INF\build-server-plugin-myCompanyNotifications.xml
@echo   ^<bean id="myCompanyNotifications" /^>>> .\out\META-INF\build-server-plugin-myCompanyNotifications.xml
@echo ^</beans^>>> .\out\META-INF\build-server-plugin-myCompanyNotifications.xml


@echo on
jar -cf .\out\MyCompanyNotifications.jar ^
    -C out .\META-INF\build-server-plugin-myCompanyNotifications.xml^
    -C out mycompany\teamcity\plugins\MyCompanyNotifications.class^
    -C out mycompany\teamcity\plugins\MyCompanyNotifications$MyCompanyReadFile.class
if %errorlevel% neq 0 exit /b %errorlevel%

if not exist D:\ProgramData\JetBrains\TeamCity\plugins\MyCompanyNotifications mkdir D:\ProgramData\JetBrains\TeamCity\plugins\MyCompanyNotifications
if not exist D:\ProgramData\JetBrains\TeamCity\plugins\MyCompanyNotifications\server mkdir D:\ProgramData\JetBrains\TeamCity\plugins\MyCompanyNotifications\server

copy .\out\teamcity-plugin.xml D:\ProgramData\JetBrains\TeamCity\plugins\MyCompanyNotifications
copy .\out\MyCompanyNotifications.jar D:\ProgramData\JetBrains\TeamCity\plugins\MyCompanyNotifications\server

net stop TeamCity
net start TeamCity



Attachment(s):
MyCompanyNotifications.java.zip
MyCompanyNotifications_Build.bat.zip
0
Comment actions Permalink

Hi Brett,

Thank you for sharing your plugin! We've added a link to this thread to the related feature request: https://youtrack.jetbrains.com/issue/TW-7318.

0

Please sign in to leave a comment.