Bitbucket到ColdFusion POST Hook

时间:2014-07-11 17:19:09

标签: git coldfusion bitbucket coldfusion-9

我的公司开始使用Bitbucket来托管我们的Git存储库,我无法找到任何关于如何正确设置.cfm文件以便POST挂钩调用并执行自动提取和检出的好例子。 .cfm文件能够解析有效负载,但无法执行命令。任何帮助或链接到示例/指南将非常感激。

服务器:安装了Git并运行Adobe CF9和IIS 7的Windows Server 2003 R2

代码示例:

<cfset strRepositoryDirectory = "Z:\web\_repositories\someproject.git"> <!--- The full windows directory path to the GIT repository. --->
<cfset strExecutablePath = '"C:\Program Files (x86)\Git\bin\sh.exe" --login -i'>
<cfset strWebsiteRootDirectory = "Z:\web\someproject"> <!--- The full windows directory path to the website root. --->
<cfset strGitBinPath = "git"> <!--- The full windows directory path to the GIT bin folder. --->
<cfset strBranch = ""> <!--- The branch that was updated in the repository. --->

<cfset jsonPayload = "">

<cfset arrCommits = ArrayNew( 1 )>

<cfset boolUpdate = false> <!--- Flag that determines if script should update the website contents. --->

<!--- Parse the POST data and convert to a structure. --->
<cfif IsDefined( getHTTPRequestData().content )>
  <cfset jsonPayLoad = toString( getHTTPRequestData().content )>
  <cfset jsonPayLoad = ReplaceNoCase( jsonPayLoad, "payload=", "", "ALL" )>
  <cfset jsonPayLoad = URLDecode( jsonPayLoad )>
  <cfset jsonPayLoad = DeserializeJSON( jsonPayLoad )>

  <!--- Grab the commits, if any. --->
  <cfset arrCommits = StructFind( jsonPayLoad, "commits" )>

  <!--- Check the commits. --->
  <cfif ArrayLen( arrCommits ) EQ 0>
    <cfset boolUpdate = true>
  <cfelse>
    <cfloop array = "#arrCommits#" index = "idxCommit">
      <cfset strBranch = StructFind( idxCommit, "branch" )>

      <cfif UCase( strBranch ) EQ "DEVELOPMENT">
        <cfset boolUpdate = true>

        <cfbreak>
      </cfif>
    </cfloop>
  </cfif>
</cfif>

<!--- Check to see if the website content should be updated. --->
<cfif boolUpdate>
  <cfexecute name = "#strExecutablePath#" arguments = 'cd "#strRepositoryDirectory#" && #strGitBinPath# fetch'></cfexecute>
  <cfexecute name = "#strExecutablePath#" arguments = 'cd "#strRepositoryDirectory#" && GIT_WORK_TREE="#strWebsiteRootDirectory#" #strGitBinPath# checkout -f'></cfexecute>

  <cffile action = "append" file = "#ExpandPath('.')#\deploy.log" output = "#DateFormat( NOW(), 'yyyy-mm-dd' )# #TimeFormat( NOW(), "long" )# -- Deployed branch: #strBranch# || Commit: Automatic PUSH from Bitbucket." addnewline = "yes">
</cfif>

1 个答案:

答案 0 :(得分:0)

您最好的办法是设置一个.bat文件,该文件可以与cfexecute一起运行,而不是进行大量的cfexecute次呼叫。然后,您需要确保分配给ColdFusion服务器的用户可以运行该.bat文件。我通常以CF用户身份登录服务器并直接通过cmd.exe运行批处理文件,以确保正确配置所有内容,并且我有权执行需要运行的所有内容。