如何进行K2自动部署并与持续集成工具(TeamCity)集成?

时间:2011-09-18 15:56:31

标签: deployment msbuild k2

我正在研究K2 blackpearl项目( Visual Studio 2010 + K2 blackpearl + SQL Server 2008 )。 SVN中的完整源代码,我使用TeamCity作为持续集成工具。我完成了Web和数据库自动部署。当我正在进行K2自动部署时,我使用MSBuild将K2包部署到K2服务器上   
     Msbuild“项目工作文件夹\ obj \ Debug \ Deployment \ WorkflowName.msbuild”/ p:TestOnly = True / p:Environment = Development

在运行MSBuild之前,我需要首先创建K2部署包,现在出现问题:
1.我没有发现该命令与Visual Studio的“Create K2 Deployment package”具有相同的功能;
2.我发现我可以使用编码来创建包,因此我尝试创建一个Console应用程序来创建K2部署包。代码需要引用Microsoft.Build DLL,但不支持将Microsoft.Build引用添加到Console项目。所以我尝试创建一个类项目,并将下面的代码放到类中,该类成功复杂化,但是当我尝试将此类项目或DLL添加到控制台项目时,它仍然存在同样的问题。我收到4个关于System.Design,Microsoft.Build,Microsoft.Build.Framework和Microsoft.Build.Utilities的警告。我找不到通过Console运行create package方法的方法。

您是否有更好的想法或解决方案来解决K2自动部署问题?

错误信息:
    无法解析引用的程序集“... \ bin \ Debug \ DeployPackageCreator.dll”,因为它依赖于“System.Design,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a”,它不在当前目标中框架“.NETFramework,Version = v4.0,Profile = Client”。请删除不在目标框架中的程序集的引用,或考虑重新定位项目。

代码:

using Microsoft.Build.Framework;
using Microsoft.Build.Utilities;
using SourceCode.ProjectSystem;
using SourceCode.Workflow.Authoring;
using SourceCode.Framework.Deployment;
using System.IO; 
using SourceCode.EnvironmentSettings.Client;



public string K2ConnectionString { get; set; }
public string SelectedEnvironment { get; set; }
public string OutputFolder { get; set; }
public string KPRXLocation { get; set; }
public string FolderName { get; set; }



private void SavePackage(string folderName, string kprxLocation, string outputFolder)
    {
        //Create a project to use for deployment, the project is the folder/solution
        string tmpPath = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetTempFileName()));
        Project project = new Project(folderName, tmpPath);

        //Add the process as a projectfile.
        Process processToDeploy = Process.Load(kprxLocation);

        // Log load problems.
        foreach (SourceCode.Framework.ObjectLoadException loadExceptions in processToDeploy.LoadErrors)
        {
            //Log.LogWarning("Load exception: {0}", loadExceptions.Message);
        }

        // Add process to the project.
        AddProcess(project, processToDeploy);

        //Do a test-compile
        if (!TestCompile(project))
        {
            throw new Exception("First compile test; Project did not compile.");
        }

        // create a deployment package
        DeploymentPackage package = project.CreateDeploymentPackage();

        // Add environment stuff
        AddEnvironment(package, this.SelectedEnvironment);

        //Set other connections. The K2 deployment package uses a reference. This does the same but does not use the reference!
        package.SmartObjectConnectionString = this.K2ConnectionString;
        package.WorkflowManagementConnectionString = this.K2ConnectionString;

        //Do a test-compile
        if (!TestCompile(project))
        {
            throw new Exception("Second compile test; Project did not compile.");
        }

        //Finaly, save the deployment package
        package.Save(outputFolder, folderName);
    }


   private bool TestCompile(Project project)
    {
       // project.Environment.AuthoringMode = SourceCode.Framework.AuthoringMode.CodeOnly;
        //Log.LogMessage("Project.environment: {0}", project.Environment.Name);
        DeploymentResults compileResult = project.Compile();

        if (!compileResult.Successful)
        {
            foreach (System.CodeDom.Compiler.CompilerError error in compileResult.Errors)
            {
                string errString = string.Format("Error compiling: {0} - {1}", error.ErrorNumber, error.ErrorText);
                //Log.LogWarning(errString);
                Console.WriteLine(errString);
            }
        }

        return compileResult.Successful;
    }



private void AddProcess(Project project, Process process)
    {
        // Create the ProjectFile
        ProjectFile file = (ProjectFile)project.CreateNewFile();

        // Set information on the file.
        file.FileName = process.FileName;
        file.Content = process;

        // Add the file to the project
        project.Files.Add(file);

        // Save the project to the temp location.
        project.SaveAll();
    }



    private void AddEnvironment(DeploymentPackage package, string SelectedEnvironment)
    {
        // Since there's no documentation on connecting to the environment server. This seems to work....
        EnvironmentSettingsManager envManager = new EnvironmentSettingsManager(true);
        envManager.ConnectToServer(this.K2ConnectionString);
        envManager.InitializeSettingsManager(true);
        envManager.Refresh();

        // Add environments + environment properties.
        foreach (EnvironmentTemplate envTemp in envManager.EnvironmentTemplates)
        {
            foreach (EnvironmentInstance envInst in envTemp.Environments)
            {
                //Add an environment to the package.
                DeploymentEnvironment env = package.AddEnvironment(envInst.EnvironmentName);
                foreach (EnvironmentField field in envInst.EnvironmentFields)
                {
                    env.Properties[field.FieldName] = field.Value;
                }

                // Make sure the environment we select actually exists.
                if (envInst.EnvironmentName == SelectedEnvironment)
                {
                    package.SelectedEnvironment = envInst.EnvironmentName;
                }
            }
        }

        //Cleanup
        envManager.Disconnect();
        envManager = null;

        //Final check of the selected environment 
        if (package.SelectedEnvironment == null || package.SelectedEnvironment == string.Empty)
        {
            throw new Exception(string.Format("Could not find the environment you wanted to select ({0})", SelectedEnvironment));
        }
    }

1 个答案:

答案 0 :(得分:2)

此错误似乎是由您使用“.Net 4.0 客户端配置文件”(而不是框架的完整版本)构建部署包(依赖于System.Design)引起的其中不包括框架库 System.Design

是否可以构建完整版的.Net 4而不是 Client Profile

虽然我不完全了解K2运行的平台,但如果它是一个Sharepoint库/包,那么我假设它运行的服务器安装了完整版的.net 4。

有关MSDN上的客户端配置文件限制的信息:

http://msdn.microsoft.com/en-us/library/cc656912.aspx