Пример #1
0
        /// <summary>
        /// Run a Jenkins job.
        /// </summary>
        /// <param name="jobName">Name of the Jenkins job</param>
        /// <param name="parameters">Parameters for the Jenkins job</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Result and number of the Jenkins build</returns>
        public async Task <object> RunJobAsync(string jobName, JenkinsBuildParameters parameters, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(jobName))
            {
                throw new ArgumentNullException(nameof(jobName));
            }

            Uri location;

            if (parameters == null || parameters.IsEmpty)
            {
                location = await PostRunAsync($"/job/{jobName}/build?delay=0sec", null, cancellationToken);
            }
            else
            {
                location = await PostRunAsync($"/job/{jobName}/build?delay=0sec", parameters, cancellationToken);
            }


            // Key	Value  Location http://localhost:8080/queue/item/9/
            // Key	Value  Location http://localhost:8080/queue/item/10/
            // Key Value  Location http://localhost:8080/job/Freestyle%20Test%20Parameter/

            if (location != null && location.ToString().Contains("/queue/item/"))
            {
                // if no delay item.Executable will be null
                await Task.Delay(100);

#pragma warning disable IDE0059 // Unnecessary assignment of a value
                string schema = await GetStringAsync(new Uri(location, "api/Schema").ToString(), cancellationToken);

#pragma warning restore IDE0059 // Unnecessary assignment of a value
                //object item = await GetAsync<>(new Uri(location, "api/xml/").ToString());
                //return item;
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Run Jenkins job and return after job has finished.
        /// </summary>
        /// <param name="jenkins">Jenkins this parameter</param>
        /// <param name="name">Name of the Jenkins job.</param>
        /// <param name="parameters">Parameter list for the Jenkins job.</param>
        /// <param name="pollingTime">Polling time in milliseconds. The default value is 5 seconds.</param>
        /// <returns>Finished build</returns>
        public static JenkinsModelRun RunJobComplete(this Jenkins jenkins, string name, JenkinsBuildParameters parameters = null, int pollingTime = 5000)
        {
            //    int num = -1;
            //    JenkinsRun build = null;
            //    JenkinsQueueLeftItem item = jenkins.RunJobAsync(name, parameters).Result;
            //    if (item != null)
            //    {
            //        num = item.Executable.Number;  // not item.ID
            //    }
            //    else
            //    {
            //        Thread.Sleep(pollingTime);
            //        build = jenkins.GetLastBuildAsync(name).Result;
            //        num = build.Number;
            //    }
            //    do
            //    {
            //        Thread.Sleep(pollingTime);
            //        build = jenkins.GetBuildAsync(name, num).Result as JenkinsBuild;
            //    } while (build.IsBuilding);

            //    return build;
            return(null);
        }
Пример #3
0
 /// <summary>
 /// Run a Jenkins job.
 /// </summary>
 /// <param name="jobName">Name of the Jenkins job</param>
 /// <param name="parameters">Parameters for the Jenkins job</param>
 /// <returns>Result and number of the Jenkins build</returns>
 public async Task <object> RunJobAsync(string jobName, JenkinsBuildParameters parameters)
 {
     return(await RunJobAsync(jobName, parameters, CancellationToken.None));
 }
Пример #4
0
        /// <summary>
        /// Run a Jenkins job.
        /// </summary>
        /// <param name="jobName">Name of the Jenkins job</param>
        /// <param name="parameters">Parameters for the Jenkins job</param>
        /// <param name="runConfig"></param>
        /// <param name="progress"></param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>Result and number of the Jenkins build</returns>
        public async Task <JenkinsRunProgress> RunJobAsync(string jobName, JenkinsBuildParameters parameters, JenkinsRunConfig runConfig, IProgress <JenkinsRunProgress> progress, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(jobName))
            {
                throw new ArgumentNullException(nameof(jobName));
            }

            runConfig = runConfig ?? this.RunConfig ?? throw new Exception("No JenkinsRunConfig available.");

            string path = $"/job/{jobName}/{(parameters == null ? "build" : "buildWithParameters")}?delay={runConfig.StartDelay}sec";
            var    res  = await PostRunJobAsync(path, null, cancellationToken);

            //Uri location =
            // store last progress info to compare for changes
            string             jobUrl = new Uri(this.BaseAddress, $"/job/{jobName}").ToString();
            JenkinsRunProgress last   = new JenkinsRunProgress(jobName, jobUrl, res);

            // return if
            if (res.Location == null ||                             // Jenkins server is too old and does not return the location
                res.StatusCode == HttpStatusCode.Conflict ||        // Jenkins job is disabled
                runConfig.RunMode == JenkinsRunMode.Immediately)    // RunMode = Immediately
            {
                return(last);
            }

            string buildUrl = null;

            while (!cancellationToken.IsCancellationRequested)
            {
                string str = await GetApiStringAsync(res.Location.ToString(), cancellationToken);

                if (str.StartsWith("<buildableItem"))
                {
                    JenkinsModelQueueBuildableItem item = JenkinsDeserializer.Deserialize <JenkinsModelQueueBuildableItem>(str);
                    Debug.WriteLine($"buildableItem: IsPending={item.IsPending} IsBlocked={item.IsBlocked} IsBuildable={item.IsBuildable} IsStuck={item.IsStuck} Why={item.Why}");
                    UpdateProgress(ref last, progress, jobName, jobUrl, item);
                    if (item.IsStuck && runConfig.ReturnIfBlocked)
                    {
                        return(last);
                    }
                }
                else if (str.StartsWith("<blockedItem"))
                {
                    JenkinsModelQueueBlockedItem item = JenkinsDeserializer.Deserialize <JenkinsModelQueueBlockedItem>(str);
                    Debug.WriteLine($"blockedItem: IsBlocked={item.IsBlocked} IsBuildable={item.IsBuildable} IsStuck={item.IsStuck} Why={item.Why}");
                    UpdateProgress(ref last, progress, jobName, jobUrl, item);
                    if (item.IsStuck && runConfig.ReturnIfBlocked)
                    {
                        return(last);
                    }
                }
                else if (str.StartsWith("<leftItem"))
                {
                    JenkinsModelQueueLeftItem item = JenkinsDeserializer.Deserialize <JenkinsModelQueueLeftItem>(str);
                    Debug.WriteLine($"leftItem: IsCancelled={item.IsCancelled} IsBuildable={item.IsBlocked} IsBuildable={item.IsBuildable} IsStuck={item.IsStuck} Why={item.Why}");
                    UpdateProgress(ref last, progress, jobName, jobUrl, item);
                    if (item.Executable != null)
                    {
                        buildUrl = item.Executable.Url;
                        break;
                    }
                }
                else
                {
                    string schema = await GetApiStringAsync(new Uri(res.Location, "api/schema").ToString(), cancellationToken);

                    throw new Exception($"Unknown XML Schema!!!\r\n{schema}");
                }
                await Task.Delay(runConfig.PollingTime, cancellationToken);
            }

            if (runConfig.RunMode <= JenkinsRunMode.Queued)
            {
                return(last);
            }

            while (!cancellationToken.IsCancellationRequested)
            {
                JenkinsModelRun run = await GetApiBuildAsync <JenkinsModelRun>(buildUrl.ToString(), cancellationToken);

                Debug.WriteLine($"modelRun: IsBuilding={run.IsBuilding} IsKeepLog ={run.IsKeepLog} Result={run.Result}");
                UpdateProgress(ref last, progress, jobName, jobUrl, run);
                Console.WriteLine($"IsBuilding: {run.IsBuilding}");
                if (runConfig.RunMode <= JenkinsRunMode.Started && run.IsBuilding)
                {
                    // build started
                    return(last);
                }
                if (!run.IsBuilding)
                {
                    // build finished
                    return(last);
                }
                await Task.Delay(runConfig.PollingTime, cancellationToken);
            }
            return(last);
        }
Пример #5
0
 /// <summary>
 /// Run a Jenkins job.
 /// </summary>
 /// <param name="jobName">Name of the Jenkins job</param>
 /// <param name="parameters">Parameters for the Jenkins job</param>
 /// <param name="runConfig"></param>
 /// <param name="progress"></param>
 /// <returns>Result and number of the Jenkins build</returns>
 public async Task <JenkinsRunProgress> RunJobAsync(string jobName, JenkinsBuildParameters parameters, JenkinsRunConfig runConfig, IProgress <JenkinsRunProgress> progress)
 {
     return(await RunJobAsync(jobName, parameters, runConfig, progress, CancellationToken.None));
 }
Пример #6
0
 /// <summary>
 /// Run a Jenkins job.
 /// </summary>
 /// <param name="jobName">Name of the Jenkins job</param>
 /// <param name="parameters">Parameters for the Jenkins job</param>
 /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
 /// <returns>Result and number of the Jenkins build</returns>
 public async Task <JenkinsRunProgress> RunJobAsync(string jobName, JenkinsBuildParameters parameters, CancellationToken cancellationToken)
 {
     return(await RunJobAsync(jobName, parameters, null, null, cancellationToken));
 }