Can we convert a freestyle job in jenkins into a template - templates

I have configured a freestyle Jenkins job to perform build, run some tests, perform code analysis and upload artifacts to Nexus. I will need to create more of such jobs in future and so I want to create a template so that its easy for anyone else to create those jobs in future. Is there a way to convert the freestyle job into template?

You can create generic mechanisms using Ant(In my case NAnt), and use them using a set of imported variables.
My config for web app looks like:
<?xml version="1.0" encoding="utf-8"?>
<webapps>
<add basedir="deploys\JobLocation" deploydir="C:\CIReference\Sites" appname="JobLocation" appid="5" appuri="http/*:8081" apppool.runtineversion="v4.0" />
</webapps>
The add tag has the basic configuration of the webapp
And the generic mechanism to deploy web application is like this
<?xml version="1.0" encoding="utf-8"?>
<project name="webdeploy" xmlns="http://nant.sf.net/release/0.92/nant.xsd">
<include buildfile="baseconfigs.xml" />
<include buildfile="external_tools.xml" />
<call target="baseConfigs" />
<property name="current.environment.path" value="${path::combine(nant.environmentsdir,environment)}"/>
<foreachxml file="${path::combine(current.environment.path,'webapps.xml')}" xpath="/webapps/add" property="basedir,deploydir,appname,appid,appuri,apppool.runtineversion">
<exec program="${appcmd.path}" failonerror="false">
<arg value="delete" />
<arg value="site" />
<arg value="/site.name: ${appname}" />
</exec>
<exec program="${appcmd.path}" failonerror="false">
<arg value="delete" />
<arg value="apppool" />
<arg value="/apppool.name: ${appname}" />
</exec>
<echo message="Deleting directory: ${path::combine(deploydir, appname)}" />
<delete dir="${path::combine(deploydir, appname)}" failonerror="false" />
<echo message="Coping directory to: ${path::combine(deploydir, appname)}" />
<copy todir="${path::combine(deploydir, appname)}" includeemptydirs="true" overwrite="true" verbose="true">
<fileset basedir="${path::combine(directory::get-current-directory(), basedir)}">
<include name="**\*.*" />
</fileset>
</copy>
<exec program="${appcmd.path}">
<arg value="add" />
<arg value="apppool" />
<arg value="/name: ${appname}" />
</exec>
<exec program="${appcmd.path}">
<arg value="set" />
<arg value="apppool" />
<arg value="/name: ${appname}" />
<arg value="/managedRuntimeVersion:${apppool.runtineversion}" />
</exec>
<exec program="${appcmd.path}">
<arg value="add" />
<arg value="site" />
<arg value="/name: ${appname}" />
<arg value="/id: ${appid}" />
<arg value="/bindings: ${appuri}" />
<arg value="/physicalPath: ${path::combine(deploydir, appname)}" />
</exec>
<exec program="${appcmd.path}" failonerror="false">
<arg value="set" />
<arg value="site" />
<arg value="${appname}" />
<arg value="/applicationPool:${appname}" />
</exec>
</foreachxml>
</project>
You can use this tool to create whatever mechanism do you need for your CI solution.
Order files...
baseconfigs.xml
<?xml version="1.0" encoding="utf-8"?>
<project>
<property name="nant.dir" value="" />
<property name="nant.environmentsdir" value="" />
<target name="baseConfigs">
<cd dir="..\..\" />
<property name="nant.dir" value="${path::combine(directory::get-current-directory(),'jenkins\NAnt')}" />
<property name="nant.environmentsdir" value="${path::combine(nant.dir,'environments')}" />
</target>
</project>
external_tools.xml
<?xml version="1.0" encoding="utf-8"?>
<project>
<property name="appcmd.path" value="c:\windows\System32\InetSrv\appcmd.exe" />
<property name="java_path" value="C:\Program Files (x86)\Jenkins\jre\bin"/>
<property name="liquibase_exe_path" value="C:\Program Files (x86)\Jenkins\tools\liquibase\"/>
<loadtasks assembly="C:\Program Files (x86)\Jenkins\tools\nant\nantcontrib\NAnt.Contrib.Tasks.dll" />
<loadtasks assembly="C:\Program Files (x86)\Jenkins\tools\nant\NAnt.GF.Custom.Tasks.dll" />
</project>

Related

How to pass arg for exec task on if-condition? [duplicate]

I've got an ant build script which I should modify. Specifically I should make a subversion checkout conditional: currently only the trunk gets checked out, the new version should checkout a given branch if needed.
<target name="do-svn-checkout" depends="init"
<property name="branch" value=""/>
<exec executable="svn">
<arg value="checkout"/>
<arg value="-r"/>
<arg value="HEAD"/>
<arg value="http://t01/java/trunk"/>
<arg value="zzz"/>
<arg value="--password"/>
<arg value="xxx"/>
<arg value="--username"/>
<arg value="yyy"/>
</exec>
</target>
The property branch will be set via the command line like for instance -Dbranch=mybranch.
If the property branch is empty, the trunk should be checked out, but if the property has any other value, the respective branch should be checked out, like http://t01/svn/hlfg/HLFG/java/branch/the-value-of-the-property. So depending on the property the respective arg-value of the svn call should be modified.
Is it possible to solve this with basic Ant or would I need to use an inline script?
When using Ant >= 1.9.3 it's a piece of cake with the new if/unless feature introduced with Ant 1.9.1
(but you should at least use Ant 1.9.3 because of bugs in Ant 1.9.1, see this answer for details)
Don't forget the namespaces to activate that feature, f.e. :
<project
xmlns:if="ant:if"
xmlns:unless="ant:unless"
>
<property name="foobar" value=" "/>
<echo if:blank="${foobar}">foobar blank !</echo>
<echo unless:blank="${foobar}">foobar not blank !</echo>
</project>
in your case something like :
<target name="do-svn-checkout" depends="init"
<property name="branch" value=""/>
<exec executable="svn">
<arg value="checkout"/>
<arg value="-r"/>
<arg value="HEAD"/>
<arg value="http://t01/java/trunk" if:blank="${branch}">
<arg value=".." unless:blank="${branch}">
<arg value="zzz"/>
<arg value="--password"/>
<arg value="xxx"/>
<arg value="--username"/>
<arg value="yyy"/>
</exec>
</target>
You could define a wrapper target which depends on two other targets - one of which does trunk checkout, the other of which does branch checkout - and each of which is conditional on existence of your optional branch property.
You could further abstract the exec call into a macrodef to which you pass the trunk or branch url.
For example:
<project name="test" default="do-svn-checkout">
<target name="do-svn-checkout" depends="do-svn-trunk-checkout, do-svn-branch-checkout"/>
<target name="do-svn-trunk-checkout" unless="branch">
<svn-checkout svn-url="http://t01/svn/java/trunk"/>
</target>
<target name="do-svn-branch-checkout" if="branch">
<svn-checkout svn-url="http://t01/svn/hlfg/HLFG/java/branch/${branch}"/>
</target>
<macrodef name="svn-checkout">
<attribute name="svn-url"/>
<sequential>
<echo message="svn-url=#{svn-url}"/>
</sequential>
</macrodef>
</project>
Output with no branch property defined:
do-svn-trunk-checkout:
[echo] svn-url=http://t01/svn/java/trunk
do-svn-branch-checkout:
do-svn-checkout:
Output with branch property defined:
do-svn-trunk-checkout:
do-svn-branch-checkout:
[echo] svn-url=http://t01/svn/hlfg/HLFG/java/branch/mybranch
do-svn-checkout:

MsBuild Cpp BeforeBuild not working

I want to execute a target before project builds.
I have define the following in the .vcxproj file.
<Target Name="BeforeBuild">
<Message Text="BeforeBuilds" />
<CallTarget Targets="myTarget" />
</Target>
<Import Project="..\GetCat.targets" />
<Target Name="DefaultBeforeTarget" BeforeTargets="Default">
<CallTarget Targets="myTarget" />
</Target>
<Target Name="myTarget" Inputs="$(TargetPath)" Outputs="$(TargetDir)$(TargetName).tlb" DependsOnTargets="GetCat">
<Message Text="Calling myTarget" />
<Exec Command="mkdri HelloW" />
</Target>
The myTarget <Message> is not getting called.
How to fix this?

Maximum number of arguments for qdbusxml2cpp

I am extending a DBus interface on a linux build machine using Qt. The existing interface works fine and I need to add another parameter
The XML generation method generation is:
<method name="get_card_info">
<arg type="b" name="success" direction="out" />
<arg type="s" name="version" direction="out" />
<arg type="s" name="serial" direction="out" />
<arg type="s" name="BeginDate" direction="out" />
<arg type="s" name="ExpirationDate" direction="out" />
<arg type="s" name="RenewalDate" direction="out" />
<arg type="s" name="ZipCode" direction="out" />
<arg type="s" name="ZipCodeExtension" direction="out" />
<!-- <arg type="u" name="cardStatus" direction="out" /> -->
</method>
The code works fine until I uncomment the commented out line, at which point qdbusxml2cpp reports:
interface_dbus_p.h:39:103: error: wrong number of template arguments (9, should be 8)
This is even if I comment out all calls to this function; indeed this is before the linking code even gets compiled; this is all from the qdbusxml2cpp call.
The XML will compile if I change this to six, seven or eight items, but if I increase it to nine it crashes.
I've changed no other configuration files except the XML code.
What's wrong? Is there a limit of eight parameters?
Found it; yes there is a limit, thanks to QDBusPendingReply
"The QDBusPendingReply is a template class with up to 8 template parameters. Those parameters are the types that will be used to extract the contents of the reply's data."
So no more than 8 parameters for me :(

I want to use NAnt's foreach to iterate files in a folder, how to force alphabetic iteration?

I have an NAnt task "ship" to package my current .sql scripts into a build, then name the build with an incrementing int {######} and copy it to a build folder.
I have another NAnt task which executes those build scripts.
They must execute in order, but in my last attempt, they were not. Can I "force" NAnt to work alphabetically?
FAIL:
<fileset basedir="source\tsql\builds\" id="buildfiles">
<include name="*.sql.template.sql" />
<exclude name="*.sql" />
<exclude name="*asSentTo*" />
</fileset>
<foreach item="File" property"filename">
<in refid="buildfiles">
<echo message="${filename}" />
</in>
</foreach>
PASS:
<foreach item="File" property="filename" in="source\tsql\builds">
<do>
<if test="${string::ends-with(filename,'.sql.template.sql')}">
<echo message="${filename}" />
</if>
</do>
</foreach>
To satisfy my curiosity I tried to reproduce the problem with this script:
<?xml version="1.0"?>
<project name="foreach.test" default="foreach.alpha">
<target name="foreach.alpha">
<foreach item="File" in="C:\foo" property="filename">
<do>
<echo message="${filename}" />
</do>
</foreach>
</target>
</project>
The filenames are printed out in alphabetical order. So conventional use of foreach already seems to be the solution to the problem.
Here is how you do it with a fileset
<fileset id="mySet">
<include name="*.sql" />
</fileset>
<copy>
<fileset refid="mySet" />
</copy>
<foreach item="File" property="filename">
<in>
<items refid="mySet" />
</in>
<do>
<echo message="Copied files: ${filename} to directory: ${Folder}." />
</do>
</foreach>

Publish WebApplication using NAnt

Is it possible to accomplish publish (as in Visual Studio publish on Web Application project) on solution using NAnt? I just can't find the solution.
They key is to use the built-in "_CopyWebApplication" target.
Here is what i do
<target name="compile" description="Compiles the project.">
<exec basedir="." program="${DotNetPath}msbuild.exe" commandline=" src/MyProject.Web/MyProject.Web.csproj /nologo
/t:Rebuild
/t:ResolveReferences;_CopyWebApplication
/p:OutDir=../../output/build/bin/
/p:WebProjectOutputDir=../../output/build/
/p:Debug=${debug}
/p:Configuration=${configuration}
/v:m"
workingdir="." failonerror="true" />
</target>
with the dir structure of:
/project.build
/src/myprojct.sln
/src/myporject.web/myproject.web.csproj
/output
Edit: i also use this to use the YUI compression to compress my css and js
<target name="compress-js">
<foreach item="File" property="filename">
<in>
<items basedir="output/build/assets/javascript/">
<include name="/**/*.js" />
<exclude name="/**/*.min.js" />
<exclude name="/**/*.pack.js" />
</items>
</in>
<do>
<exec basedir="." program="${JavaPath}java" commandline=" -jar S:\yuicompressor-2.4.1\build\yuicompressor-2.4.1.jar --type js --charset utf-8 -o "${filename}" "${filename}"" failonerror="true" />
</do>
</foreach>
</target>
<target name="compress-css" depends="combine-css">
<foreach item="File" property="filename">
<in>
<items basedir="output/build/assets/css/">
<include name="/**/*.css" />
<exclude name="/**/*.min.css" />
<exclude name="/**/*.pack.css" />
</items>
</in>
<do>
<exec basedir="." program="S:\Java\jdk1.6.0_11\bin\java" commandline=" -jar S:\yuicompressor-2.4.1\build\yuicompressor-2.4.1.jar --type css --charset utf-8 -o "${filename}" "${filename}"" failonerror="true" />
</do>
</foreach>
</target>
Using MSBuild beats the purpose of using NAnt, NAnt is something that replaces MSBuild, use the below to do a clean NAnt compilation of Web Application Projects as in VS 2003/2005/2008.
It works for me!
<?xml version="1.0"?>
<project name="MyTest" default="run">
<property name="basename" value="MyTest1x"/>
<property name="debug" value="false"/>
<property name="copytarget" value="c:\temp"/>
<target name="clean">
<delete>
<fileset basedir="${copytarget}">
<include name="bin/${basename}.dll"/>
<include name="**/*.???x"/>
<include name="Web.config"/>
</fileset>
</delete>
</target>
<target name="build">
<mkdir dir="${copytarget}/bin" />
<csc target="library" output="${copytarget}/bin/${basename}.dll" >
<sources>
<include name="*.cs"/>
</sources>
</csc>
</target>
<target name="run" depends="clean,build">
<copy todir="${copytarget}" overwrite="true">
<fileset basedir=".">
<include name="**/*.???x" />
<include name="Web.config" />
</fileset>
</copy>
</target>
</project>
I know this is an old question, but I just learned something, so I decided I'd share: While it is 100% true that the "_CopyWebApplication" target exists and works, as of .NET 4.0 it has been superseded by the "_WPPCopyWebApplication" target in Microsoft.Web.Publishing.targets, which supports new features like web.config transformation syntax, etc.
(reposting to all the similarly worded questions, so don't vote for this one :) )
Here is how you can do it using the MSBUILD task for nant:
<property name="debug" value="AutomatedDebug" />
<property name="configuration" value="Debug;TargetFrameworkVersion=v3.5" />
<msbuild project="src\WebApplication1\WebApplication1.csproj" failonerror="true">
<arg value="/nologo" />
<arg value="/t:Rebuild" />
<arg value="/t:ResolveReferences;_CopyWebApplication" />
<arg value="/p:OutDir=../../build/Debug/WebApplication/" />
<arg value="/p:WebProjectOutputDir=../../build/Debug/WebApplication-Deploy/" />
<arg value="/p:Debug=${debug}" />
<arg value="/p:Configuration=${configuration}" />
<arg value="/v:m" />
</msbuild>
This will compile the WebApplication into a deployable folder, just like using the "Publish" feature from within Visual Studio.