我们可以将jenkins的自由式作业转换为模板吗?

时间:2018-03-05 21:48:03

标签: templates jenkins

我已经配置了一个自由式Jenkins作业来执行构建,运行一些测试,执行代码分析并将工件上传到Nexus。我将来需要创建更多这样的工作,因此我想创建一个模板,以便将来任何人都可以轻松创建这些工作。有没有办法将自由式作业转换为模板?

1 个答案:

答案 0 :(得分:0)

您可以使用Ant(在我的案例中为NAnt)创建通用机制,并使用一组导入的变量来使用它们。 我的网络应用配置如下:

    <?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>

  

add标签具有webapp的基本配置

部署Web应用程序的通用机制就像这样

<?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>

您可以使用此工具创建CI解决方案所需的任何机制。

订购文件......

  

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>