Creating a K2 Deployment Package from code

本文介绍了一种通过编程方式自动生成K2部署包的方法,使用C#代码实现了从指定文件夹创建部署包的过程,包括加载流程、环境配置及两次编译验证。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

下面这篇文章介绍了如何通过编程的方式生成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);
        }



        /// 
        /// 
        /// 
        /// 
        /// 
 
        ///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));
            }

        }



    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值