【问题标题】:Automatic check in to TFS using Powershell/C#使用 Powershell/C# 自动签入 TFS
【发布时间】:2018-02-27 05:32:19
【问题描述】:

我正在尝试编写一个在成功构建时运行的 Powershell 脚本,并从 azure 下载一些工件并签入到 TFS。

我们是否有示例 c# 代码或 powershell 脚本来完成此任务?

提前致谢

【问题讨论】:

  • 您使用的是哪个版本的 TFS?当您说 runs on the successful build and downloads some artifacts from azure and checks in to TFS 时,您将如何运行您的 powershell 脚本?作为构建过程中的构建后事件或构建完成时。天蓝色的神器是什么?只是一些发布到 azure 或其他东西的构建工件?
  • 我正在使用 TFS 构建。因此,我们可以在 TFS 中进行设置,以便在构建完成后调用 ps 脚本。此时,我想从 azure 下载 ARM 模板(我的 ps 脚本已经在做)。我唯一遇到的问题是将文件签入到 tfs 路径
  • 既然构建完成并且你已经下载了arm模板,那就和将本地文件签入TFS一样。您也可以尝试使用tf.exe command 来实现它。

标签: c# powershell tfs


【解决方案1】:

您可以通过编程方式(C#)将一些文件从本地文件夹签入到 TFS。您需要创建一个工作区挂起更改签入这些更改解决冲突,如果你有。相关代码片段:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

namespace BasicSccExample
{
    class Example
    {
        static void Main(string[] args)
        {
            // Verify that we have the arguments we require.
            if (args.Length < 2)
            {
                String appName = Path.GetFileName(Process.GetCurrentProcess().MainModule.FileName);
                Console.Error.WriteLine("Usage: {0} collectionURL teamProjectPath", appName);
                Console.Error.WriteLine("Example: {0} http://tfsserver:8080/tfs/DefaultCollection $/MyProject", appName);
                Environment.Exit(1);
            }

            // Get a reference to our Team Foundation Server.
            TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(args[0]));

            // Get a reference to Version Control.
            VersionControlServer versionControl = tpc.GetService<VersionControlServer>();

            // Listen for the Source Control events.
            versionControl.NonFatalError += Example.OnNonFatalError;
            versionControl.Getting += Example.OnGetting;
            versionControl.BeforeCheckinPendingChange += Example.OnBeforeCheckinPendingChange;
            versionControl.NewPendingChange += Example.OnNewPendingChange;

            // Create a workspace.
            Workspace workspace = versionControl.CreateWorkspace("BasicSccExample", versionControl.AuthorizedUser);

            String topDir = null;

            try
            {
                String localDir = @"c:\temp\BasicSccExample";
                Console.WriteLine("\r\n--- Create a mapping: {0} -> {1}", args[1], localDir);
                workspace.Map(args[1], localDir);

                Console.WriteLine("\r\n--- Get the files from the repository.\r\n");
                workspace.Get();

                Console.WriteLine("\r\n--- Create a file.");
                topDir = Path.Combine(workspace.Folders[0].LocalItem, "sub");
                Directory.CreateDirectory(topDir);
                String fileName = Path.Combine(topDir, "basic.cs");
                using (StreamWriter sw = new StreamWriter(fileName))
                {
                    sw.WriteLine("revision 1 of basic.cs");
                }

                Console.WriteLine("\r\n--- Now add everything.\r\n");
                workspace.PendAdd(topDir, true);

                Console.WriteLine("\r\n--- Show our pending changes.\r\n");
                PendingChange[] pendingChanges = workspace.GetPendingChanges();
                Console.WriteLine("  Your current pending changes:");
                foreach (PendingChange pendingChange in pendingChanges)
                {
                    Console.WriteLine("    path: " + pendingChange.LocalItem +
                                      ", change: " + PendingChange.GetLocalizedStringForChangeType(pendingChange.ChangeType));
                }

                Console.WriteLine("\r\n--- Checkin the items we added.\r\n");
                int changesetNumber = workspace.CheckIn(pendingChanges, "Sample changes");
                Console.WriteLine("  Checked in changeset " + changesetNumber);

                Console.WriteLine("\r\n--- Checkout and modify the file.\r\n");
                workspace.PendEdit(fileName);
                using (StreamWriter sw = new StreamWriter(fileName))
                {
                    sw.WriteLine("revision 2 of basic.cs");
                }

                Console.WriteLine("\r\n--- Get the pending change and check in the new revision.\r\n");
                pendingChanges = workspace.GetPendingChanges();
                changesetNumber = workspace.CheckIn(pendingChanges, "Modified basic.cs");
                Console.WriteLine("  Checked in changeset " + changesetNumber);
            }
            finally
            {
                if (topDir != null)
                {
                    Console.WriteLine("\r\n--- Delete all of the items under the test project.\r\n");
                    workspace.PendDelete(topDir, RecursionType.Full);
                    PendingChange[] pendingChanges = workspace.GetPendingChanges();
                    if (pendingChanges.Length > 0)
                    {
                        workspace.CheckIn(pendingChanges, "Clean up!");
                    }

                    Console.WriteLine("\r\n--- Delete the workspace.");
                    workspace.Delete();
                }
            }
        }

        internal static void OnNonFatalError(Object sender, ExceptionEventArgs e)
        {
            if (e.Exception != null)
            {
                Console.Error.WriteLine("  Non-fatal exception: " + e.Exception.Message);
            }
            else
            {
                Console.Error.WriteLine("  Non-fatal failure: " + e.Failure.Message);
            }
        }

        internal static void OnGetting(Object sender, GettingEventArgs e)
        {
            Console.WriteLine("  Getting: " + e.TargetLocalItem + ", status: " + e.Status);
        }

        internal static void OnBeforeCheckinPendingChange(Object sender, ProcessingChangeEventArgs e)
        {
            Console.WriteLine("  Checking in " + e.PendingChange.LocalItem);
        }

        internal static void OnNewPendingChange(Object sender, PendingChangeEventArgs e)
        {
            Console.WriteLine("  Pending " + PendingChange.GetLocalizedStringForChangeType(e.PendingChange.ChangeType) +
                              " on " + e.PendingChange.LocalItem);
        }
    }
}

更多详情请参考 Buck Hodges 的博客here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-02
    • 2021-03-09
    • 2011-05-27
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-12
    • 2014-11-13
    相关资源
    最近更新 更多