Advertisement
joost1234

Migrate Hudson 2.2.1 to Jenkins 1.509

Apr 9th, 2013
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 14.41 KB | None | 0 0
  1. <project name="Migrate jobs from Hudson to Jenkins" default="all" basedir=".">
  2. <description>
  3.     - This Ant script was created to migrate jobs defined in Hudson 2.2.1 to Jenkins 1.509
  4.     - It needs environment variables HUDSON_HOME set to the hudson home, and JENKINS_HOME to the jenkins home.
  5.     - This script was developed as follows:
  6.         1. all jobs were copied to Jenkins as-is, and Jenkins was started.
  7.         2. Jenkins logging output was checked for any errors (there were many).
  8.         3. Logic was added to this script to convert the configuration items of the Hudson jobs that Jenkins stored differently
  9.         4. This process was repeated until Jenkins loaded all jobs without error and the job configuration in Jenkins was correct upon visual inspection.
  10.     - The consequence of the above approach is that this script ONLY migrates configuration items that we were using. For any additional configuration items to be migrated the script needs to be extended. Just create a new job in Jenkins, configure the missing configuration item, and check in the job's config.xml how Jenkins stores it. Then extend the script to cover the migration of that configuration item.
  11.     - Tested with Hudson 2.2.1 and Jenkins 1.509
  12.     - This Ant script needs ant-contrib and xmltask libraries
  13.     - The script in its current form will only work on Linux
  14.     - This was created as a 'throw away' Ant script. No effort was put in making it efficient, complete, reliable, etc.
  15.     - Always backup HUDSON_HOME and JENKINS_HOME beforehand
  16.     - Use at your own risk
  17. </description>
  18.  
  19. <taskdef resource="net/sf/antcontrib/antlib.xml"/>
  20. <taskdef name="xmltask" classname="com.oopsconsultancy.xmltask.ant.XmlTask"/>
  21.  
  22. <property environment="env"/>
  23.  
  24. <target name="all">
  25.     <for param="dir">
  26.         <path>
  27.             <path>
  28.                 <dirset dir="${env.HUDSON_HOME}/jobs" includes="*"/>
  29.             </path>
  30.         </path>
  31.         <sequential>
  32.             <local name="dir.base"/>
  33.             <basename property="dir.base" file="@{dir}"/>
  34.             <echo message="Handling ${dir.base}"/>
  35.  
  36.             <antcall target="migrateProject">
  37.                 <param name="hudson.job" value="${dir.base}"/>
  38.             </antcall>
  39.         </sequential>
  40.     </for>
  41. </target>
  42.  
  43. <target name="migrateProject">
  44.  
  45.   <delete dir="${env.JENKINS_HOME}/jobs/${hudson.job}"/>
  46.   <mkdir dir="${env.JENKINS_HOME}/jobs/${hudson.job}"/>
  47.  
  48.   <!-- Need to keep symbolic links working -->
  49.   <exec executable="bash">
  50.     <arg value="-c"/>
  51.     <arg value="cd '${env.HUDSON_HOME}/jobs/${hudson.job}' ; tar -c --exclude workspace . | tar -x -C '${env.JENKINS_HOME}/jobs/${hudson.job}'"/>
  52.   </exec>
  53.  
  54.   <copy file="${env.JENKINS_HOME}/jobs/${hudson.job}/config.xml" tofile="${env.JENKINS_HOME}/jobs/${hudson.job}/config.xml.hudson.org"/>
  55.  
  56.     <for param="config.xml.file">
  57.     <path>
  58.         <fileset dir="${env.JENKINS_HOME}/jobs/${hudson.job}">
  59.             <include name="config.xml"/>
  60.             <include name="modules/**/config.xml"/>
  61.         </fileset>
  62.     </path>
  63.     <sequential>
  64.             <local name="publishers.count"/>
  65.             <local name="builders.count"/>
  66.             <!--
  67.                params of editXml task:
  68.                xpath.source - The source xml to copy to another location in the document
  69.                xpath.insert - The position to insert "xml.to.insert"
  70.                xpath.dest - The position to insert "xpath.source"
  71.                xml.to.insert - The xml to insert at "xpath.insert"
  72.            -->
  73.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  74.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  75.                 <param name="xpath.source" value="//entry[string='logRotator']/log-rotator-property/originalValue/*"/>
  76.                 <param name="xpath.insert" value="/*"/>
  77.                 <param name="xpath.dest" value="/*/logRotator"/>
  78.                 <param name="xml.to.insert" value="&lt;logRotator/&gt;"/>
  79.             </antcall>
  80.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  81.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  82.                 <param name="xpath.source" value="//entry[string='scmCheckoutRetryCount']/integer-property/originalValue/text()"/>
  83.                 <param name="xpath.insert" value="/*"/>
  84.                 <param name="xpath.dest" value="/*/scmCheckoutRetryCount"/>
  85.                 <param name="xml.to.insert" value="&lt;scmCheckoutRetryCount/&gt;"/>
  86.             </antcall>
  87.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  88.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  89.                 <param name="xpath.source" value="//entry[string='org-jenkinsci-plugins-envinject-EnvInjectBuildWrapper']/external-property/originalValue/*"/>
  90.                 <param name="xpath.insert" value="/*"/>
  91.                 <param name="xpath.dest" value="/*/buildWrappers/EnvInjectBuildWrapper"/>
  92.                 <param name="xml.to.insert" value="&lt;buildWrappers&gt;&lt;EnvInjectBuildWrapper/&gt;&lt;/buildWrappers&gt;"/>
  93.             </antcall>
  94.  
  95.             <xmltask source="@{config.xml.file}">
  96.                 <copy path="count(/project/publishers)" property="publishers.count"/>
  97.             </xmltask>
  98.             <if>
  99.                 <equals arg1="${publishers.count}" arg2="0"/>
  100.                 <then>
  101.                     <xmltask source="@{config.xml.file}" dest="@{config.xml.file}">
  102.                         <insert path="/project" xml="&lt;publishers/&gt;"/>
  103.                     </xmltask>
  104.                 </then>
  105.             </if>
  106.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  107.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  108.                 <param name="xpath.source" value="//entry[string='hudson-tasks-Mailer']/external-property/originalValue/*"/>
  109.                 <param name="xpath.insert" value="/*/publishers"/>
  110.                 <param name="xpath.dest" value="/*/publishers/hudson.tasks.Mailer"/>
  111.                 <param name="xml.to.insert" value="&lt;hudson.tasks.Mailer/&gt;"/>
  112.             </antcall>
  113.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  114.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  115.                 <param name="xpath.source" value="//entry[string='parametersDefinitionProperties']/copy-write-list-property/originalValue/*"/>
  116.                 <param name="xpath.insert" value="/*"/>
  117.                 <param name="xpath.dest" value="/*/properties"/>
  118.                 <param name="xml.to.insert" value=""/>
  119.             </antcall>
  120.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  121.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  122.                 <param name="xpath.source" value="//entry[string='quietPeriod']/integer-property/originalValue/text()"/>
  123.                 <param name="xpath.insert" value="*"/>
  124.                 <param name="xpath.dest" value="/*/quietPeriod"/>
  125.                 <param name="xml.to.insert" value="&lt;quietPeriod/&gt;"/>
  126.             </antcall>
  127.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  128.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  129.                 <param name="xpath.source" value="//entry[string='hudson-tasks-BuildTrigger']/external-property/originalValue/*"/>
  130.                 <param name="xpath.insert" value="/*/publishers"/>
  131.                 <param name="xpath.dest" value="/*/publishers/hudson.tasks.BuildTrigger"/>
  132.                 <param name="xml.to.insert" value="&lt;hudson.tasks.BuildTrigger/&gt;"/>
  133.             </antcall>
  134.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  135.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  136.                 <param name="xpath.source" value="//entry[string='hudson-plugins-parameterizedtrigger-BuildTrigger']/external-property/originalValue/*"/>
  137.                 <param name="xpath.insert" value="/*/publishers"/>
  138.                 <param name="xpath.dest" value="/*/publishers/hudson.plugins.parameterizedtrigger.BuildTrigger"/>
  139.                 <param name="xml.to.insert" value="&lt;hudson.plugins.parameterizedtrigger.BuildTrigger/&gt;"/>
  140.             </antcall>
  141.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  142.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  143.                 <param name="xpath.source" value="//entry[string='hudson-triggers-SCMTrigger']/trigger-property/originalValue/*"/>
  144.                 <param name="xpath.insert" value="/*"/>
  145.                 <param name="xpath.dest" value="/*/triggers/hudson.triggers.SCMTrigger"/>
  146.                 <param name="xml.to.insert" value="&lt;triggers class='vector'&gt;&lt;hudson.triggers.SCMTrigger/&gt;&lt;/triggers&gt;"/>
  147.             </antcall>
  148.  
  149.             <xmltask source="@{config.xml.file}">
  150.                 <copy path="count(/project/builders)" property="builders.count"/>
  151.             </xmltask>
  152.             <if>
  153.                 <equals arg1="${builders.count}" arg2="0"/>
  154.                 <then>
  155.                     <xmltask source="@{config.xml.file}" dest="@{config.xml.file}">
  156.                         <insert path="/project" xml="&lt;builders/&gt;"/>
  157.                     </xmltask>
  158.                 </then>
  159.             </if>
  160.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  161.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  162.                 <param name="xpath.source" value="//entry[string='builders']/describable-list-property/originalValue/*"/>
  163.                 <param name="xpath.insert" value="/*"/>
  164.                 <param name="xpath.dest" value="/*/builders"/>
  165.                 <param name="xml.to.insert" value=""/>
  166.             </antcall>
  167.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  168.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  169.                 <param name="xpath.source" value="//entry[string='jdk']/string-property/originalValue/text()"/>
  170.                 <param name="xpath.insert" value="/*"/>
  171.                 <param name="xpath.dest" value="/*/jdk"/>
  172.                 <param name="xml.to.insert" value="&lt;jdk/&gt;"/>
  173.             </antcall>
  174.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  175.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  176.                 <param name="xpath.source" value="//entry[string='hudson-tasks-ArtifactArchiver']/external-property/originalValue/*"/>
  177.                 <param name="xpath.insert" value="/*/publishers"/>
  178.                 <param name="xpath.dest" value="/*/publishers/hudson.tasks.ArtifactArchiver"/>
  179.                 <param name="xml.to.insert" value="&lt;hudson.tasks.ArtifactArchiver/&gt;"/>
  180.             </antcall>
  181.  
  182.  
  183.             <xmltask source="@{config.xml.file}" dest="@{config.xml.file}">
  184.                 <remove path="/*/scm"/>
  185.             </xmltask>
  186.             <antcall target="editXml" inheritAll="true" inheritRefs="true">
  187.                 <param name="config.xml.file" value="@{config.xml.file}"/>
  188.                 <param name="xpath.source" value="//entry[string='scm']/scm-property/originalValue/*"/>
  189.                 <param name="xpath.insert" value="/*"/>
  190.                 <param name="xpath.dest" value="/*/scm"/>
  191.                 <param name="xml.to.insert" value="&lt;scm class='hudson.scm.SubversionSCM'/&gt;"/>
  192.             </antcall>
  193.  
  194.             <xmltask source="@{config.xml.file}" dest="@{config.xml.file}">
  195.                 <remove path="/*/project-properties"/>
  196.                 <remove path="/*/creationTime"/>
  197.                 <remove path="/*/cascading-job-properties"/>
  198.                 <remove path="/*/cascadingChildrenNames"/>
  199.                 <remove path="/*/advancedAffinityChooser"/>
  200.                 <remove path="/*/cleanWorkspaceRequired"/>
  201.                 <remove path="//autoValidateFileMask"/>
  202.                 <remove path="//hudson.tasks.ArtifactArchiver//compressionType"/>
  203.                 <remove path="//hudson.tasks.ArtifactArchiver//autoValidateFileMask"/>
  204.  
  205.                 <remove path="/*/rootModule"/>
  206.                 <remove path="/*/pomInfo"/>
  207.  
  208.                 <replace path="/*/scm/workspaceUpdater[@class='hudson.scm.subversion.CheckoutWithLocationFoldersCleanupUpdater']/@class" withText="hudson.scm.subversion.CheckoutUpdater"/>
  209.  
  210.                 <remove path="/project/createdBy"/>
  211.                
  212.                 <!-- !! Artifact archiving in Maven2 style jobs is not migrated !! -->
  213.                 <remove path="//maven-artifact-archiver"/>
  214.             </xmltask>
  215.     </sequential>
  216.     </for>
  217.  
  218.     <for param="build.xml.file">
  219.     <path>
  220.         <fileset dir="${env.JENKINS_HOME}/jobs/${hudson.job}/builds">
  221.             <include name="**/build.xml"/>
  222.         </fileset>
  223.     </path>
  224.     <sequential>
  225.         <antcall target="editBuildXml" inheritall="true">
  226.             <param name="build.xml.file" value="@{build.xml.file}"/>
  227.         </antcall>
  228.     </sequential>
  229.     </for>
  230. </target>
  231.  
  232. <target name="editXml">
  233.     <local name="occs"/>
  234.  
  235.     <xmltask source="${config.xml.file}">
  236.         <copy path="count(${xpath.source})" property="occs"/>
  237.     </xmltask>
  238.     <if>
  239.         <not><equals arg1="${occs}" arg2="0"/></not>
  240.         <then>
  241.         <echo message="Migrating ${xpath.source}"/>
  242.         <xmltask source="${config.xml.file}" dest="${config.xml.file}">
  243.             <copy path="${xpath.source}" buffer="theValue" append="true"/>
  244.             <insert path="${xpath.insert}" xml="${xml.to.insert}"/>
  245.             <insert path="${xpath.dest}" buffer="theValue"/>
  246.         </xmltask>
  247.         </then>
  248.         <else>
  249.             <echo message="Element not found, so not migrating: ${xpath.source}"/>
  250.         </else>
  251.     </if>
  252. </target>
  253.  
  254. <target name="editBuildXml">
  255.     <local name="occs.no.comp"/>
  256.     <xmltask source="${build.xml.file}">
  257.         <copy path="count(/*/actions/hudson.scm.SubversionTagAction/tags/no-comparator)" property="occs.no.comp"/>
  258.     </xmltask>
  259.     <if>
  260.         <equals arg1="${occs.no.comp}" arg2="0"/>
  261.         <then>
  262.         <echo message="Migrating ${build.xml.file}"/>
  263.         <xmltask source="${build.xml.file}" dest="${build.xml.file}">
  264.             <insert path="/*/actions/hudson.scm.SubversionTagAction/tags/entry[1]" xml="&lt;no-comparator/&gt;" position="before"/>
  265.         </xmltask>
  266.         </then>
  267.      </if>
  268. </target>
  269.  
  270. </project>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement