• Creating a K2 Deployment Package from code


    下面这篇文章介绍了如何通过编程的方式生成K2的部署包

    http://cyclops.nettrends.nl/blog/2008/05/creating-a-k2-deployment-package-from-code/

    In our current project, we are using Tean Foundation Server to automate our build process. This also means MSI’s are created for every artifact to deploy, eventually using TFS’s functionality to deploy a new msi when the build quality is changed. MSI’s are build in a way so they can be installed from command line without user actions required (unattended installation). So, when we change the build quality of a build it can fully automatically be deployed to a testing environment.

    Most of our software that needs to be deployed are webservices, websites or web parts. K2 however, is somewhat harder to deploy. Since BlackPearl it has become a lot easier because you can use a MSBUILD command to deploy a process. For those of you who don’t know about this. Check out KB000188 at the K2 Knowledge base. In general, it comes down to this command:

    MSBUILD DeploymentPackage.msbuild /p: Environment=Development

    It’s easy to create a MSI or (PowerShell) script that starts the MSBUILD command and installs the process. But how do we automate the creation of the deployment package? Since this is normally done from VS2005.

     

    实际上,主要用到了两个方法

    Sourcecode.Framework.ProjectSystem.Project.CreateDeploymentPackage

    Project.Save

    完整的代码如下

    http://cyclops.nettrends.nl/blog/wp-content/uploads/2008/05/deploypackagecreator.cs

    using System;
    using System.Collections.Generic;
    using System.Text;
    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;
    
    namespace CreateK2DeploymentPackage
    {
        public class DeployPackageCreator : Task
        {
    
            private string folderName;
            private string kprxLocation;
            private string outputFolder;
            private string selectedEnvironment;
            private string  k2ConnectionString;
    
            public string  K2ConnectionString
            {
                get { return k2ConnectionString; }
                set { k2ConnectionString = value; }
            }
    	
    
            public string SelectedEnvironment
            {
                get { return selectedEnvironment; }
                set { selectedEnvironment = value; }
            }
    	
    
    
            public string OutputFolder
            {
                get { return outputFolder; }
                set { outputFolder = value; }
            }
    	
            public string KPRXLocation
            {
                get { return kprxLocation; }
                set { kprxLocation = value; }
            }
    	
            public string FolderName
            {
                get { return folderName; }
                set { folderName = value; }
            }
    	
    
            public override bool Execute()
            {
                try
                {
                    this.SavePackage(this.FolderName, this.KPRXLocation, this.OutputFolder);
                }
                catch (Exception ex)
                {
                    //Log.LogError(ex.Message);
                    throw ex;
                    return false;
                }
                return true;
            }
            
    
    
            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);
            }
    
    
    
            /// <summary>
            /// 
            /// </summary>
            /// <param name="project"></param>
            /// <returns></returns>
            ///TODO: Method needs expanding to output the errors (etc..)
            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));
                }
    
            }
    
    
    
        }
    }
    
  • 相关阅读:
    asp.net文件操作类
    MSMQ是什么?
    Type.GetType()在跨程序集反射时返回null的解决方法
    ASP.NET反射
    VS单元测试入门实践教程
    详解Linq to SQL
    .Net资源文件全球化
    正则表达式使用详解
    C# 中的委托和事件详解
    python基础
  • 原文地址:https://www.cnblogs.com/chenxizhang/p/1577814.html
Copyright © 2020-2023  润新知