Ejemplo n.º 1
0
 public BuildAgent(BuildAgentOptions agentOptions, IProjectRepository repository, IBuildQueueConsumer queue, ILogger logger)
 {
     this.Repository   = repository;
     this.Queue        = queue;
     this.Logger       = logger;
     this.AgentOptions = agentOptions;
     _localQueue       = new System.Collections.Concurrent.ConcurrentQueue <LocalQueuedJob>();
 }
Ejemplo n.º 2
0
        public void TestStartProcessCreatesEnvironmentVariables()
        {
            var writer = new Mock.MockBuildOutputWriter()
            {
                PrefixSeverity = false
            };

            var agentOptions = new BuildAgentOptions()
            {
                AgentIdentifier  = "AgentID",
                ConcurrentBuilds = 1,
                WorkingPath      = "WorkingPath"
            };

            var codeInfo = new CodeRepositoryInfo()
            {
                Author             = "Author",
                RevisionIdentifier = "RevIdent"
            };

            var buildEntry = new BuildQueueEntry()
            {
                BuildNumber = 999
            };

            var project = new BuildProjectConfiguration()
            {
                ProjectID      = Guid.NewGuid(),
                Name           = "TestProject",
                RepositoryType = "svn",
                RepositoryPath = "repo-path",
                Variables      = new Dictionary <string, string>()
                {
                    { "VarTest", "123" }
                }
            };

            using (var env = new BuildEnvironment("C:\\Build", writer))
            {
                env.AddAgentVariables(agentOptions);
                env.AddCodeInfoVariables(codeInfo);
                env.AddGlobalVariables();
                env.AddQueueEntryVariables(buildEntry);
                env.AddProjectConfigurationVariables(project);

                var resultCode = env.StartProcessAsync("cmd.exe", "/c set").Result;

                Assert.AreEqual(0, resultCode);
            }

            var vars = new Dictionary <string, string>();

            using (var reader = new System.IO.StringReader(writer.StringWriter.ToString()))
            {
                string line;
                while (true)
                {
                    line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    var parts = line.Split(new char[] { '=' }, 2);

                    vars.Add(parts[0], parts[1]);
                }
            }

            Assert.AreEqual("true", vars["CI"]);
            Assert.AreEqual("C:\\Build", vars["CI_WORKINGPATH"]);

            Assert.AreEqual(Environment.MachineName, vars["CI_AGENTHOST"]);
            Assert.AreEqual(agentOptions.AgentIdentifier, vars["CI_AGENTIDENTIFIER"]);

            Assert.AreEqual(codeInfo.RevisionIdentifier, vars["CI_REVISIONIDENTIFIER"]);
            Assert.AreEqual(codeInfo.Author, vars["CI_REVISIONAUTHOR"]);

            Assert.IsNotNull(vars["CI_BUILDDATE"]);
            Assert.IsNotNull(vars["CI_BUILDDATETIME"]);

            Assert.AreEqual(project.NextBuildNumber, int.Parse(vars["CI_BUILDNUMBER"]));

            Assert.AreEqual(project.ProjectID.ToString(), vars["CI_PROJECTID"]);
            Assert.AreEqual(project.Name, vars["CI_PROJECTNAME"]);
            Assert.AreEqual(project.RepositoryType, vars["CI_REPOSITORYTYPE"]);
            Assert.AreEqual(project.RepositoryPath, vars["CI_REPOSITORYPATH"]);
            Assert.AreEqual(project.Variables["VarTest"], vars["CI_VARTEST"]);
        }
Ejemplo n.º 3
0
 public static void AddAgentVariables(this BuildEnvironment env, BuildAgentOptions agentOptions)
 {
     env.Variables.Add("AgentHost", Environment.MachineName);
     env.Variables.Add("AgentIdentifier", agentOptions.AgentIdentifier);
 }
Ejemplo n.º 4
0
        static void Main(string[] args)
        {
            var version = System.Reflection.Assembly.GetEntryAssembly().GetName().Version;

            Console.WriteLine();
            Console.WriteLine($"SteadyBuild Agent {version.Major}.{version.Minor}.{version.Revision}");
            Console.WriteLine();

            var app = new CommandLineApplication(throwOnUnexpectedArg: false);

            var managerPathOption = app.Option("-q | --queue-manager <url>", $"The URL to the build queue manager endpoint. Defaults to {DEFAULT_URL}", CommandOptionType.SingleValue);
            var logFileOption     = app.Option("-l | --log <filename>", "File where log messages will be written.", CommandOptionType.SingleValue);
            var workingPathOption = app.Option("-p | --working-path <path>", "The path where project code will be downloaded during build processes.", CommandOptionType.SingleValue);
            var threadOption      = app.Option("-t | --thread-count <count>", "The number of concurrent builds that can be performed.", CommandOptionType.SingleValue);
            var agentIdentOption  = app.Option("-i | --agent-identifier <value>", "The agent identifier string used when connecting to the build queue manager.", CommandOptionType.SingleValue);

            app.OnExecute(() =>
            {
                int threadCount    = threadOption.HasValue() ? int.Parse(threadOption.Value()) : 1;
                string agentId     = agentIdentOption.HasValue() ? agentIdentOption.Value() : Environment.MachineName;
                string managerPath = managerPathOption.HasValue() ? managerPathOption.Value() : DEFAULT_URL;
                string workingPath = workingPathOption.HasValue() ? workingPathOption.Value() : "%TEMP%\\SteadyBuild";

                TextWriter logWriter;

                if (logFileOption.HasValue())
                {
                    logWriter = new System.IO.StreamWriter(System.IO.File.Open(Environment.ExpandEnvironmentVariables(logFileOption.Value()), System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write), Encoding.UTF8);
                }
                else
                {
                    logWriter = Console.Out;
                }

                using (var logger = new StreamLogger(logWriter, MessageSeverity.Debug))
                {
                    Console.WriteLine($"  Agent ID\t : {agentId}");
                    Console.WriteLine($"  Manager\t : {managerPath}");
                    Console.WriteLine($"  Working Path\t : {workingPath}");
                    Console.WriteLine($"  Threads\t : {threadCount}");
                    Console.WriteLine();

                    var client = new HttpManagerClient(managerPath);

                    var options = new BuildAgentOptions()
                    {
                        AgentIdentifier  = agentId,
                        ConcurrentBuilds = threadCount,
                        WorkingPath      = Environment.ExpandEnvironmentVariables(workingPath)
                    };

                    var agent = new BuildAgent(options, client, client, logger);

                    Console.WriteLine("Starting agent thread.");

                    agent.Run();

                    Console.WriteLine("Agent thread exited.");
                }

                return(0);
            });

            app.Execute(args);
        }
Ejemplo n.º 5
0
 public BuildWorker(BuildAgentOptions options, IProjectRepository repository, int agentInstanceID)
 {
     _options         = options;
     _agentInstanceID = agentInstanceID;
     _repository      = repository;
 }