Пример #1
0
 internal JenkinsBuild(Jenkins jenkins, JenkinsJob job, int buildNum)
 {
     this.jenkins          = jenkins;
     this.job              = job;
     this.modelRun         = JenkinsRun.Run(() => jenkins.GetBuildAsync <JenkinsModelRun>(this.JobName, buildNum).Result);
     this.isCompleteLoaded = true;
 }
Пример #2
0
 internal JenkinsBuild(Jenkins jenkins, JenkinsJob job, JenkinsModelRun modelRun)
 {
     this.jenkins          = jenkins;
     this.job              = job;
     this.modelRun         = modelRun;
     this.isCompleteLoaded = false;
 }
Пример #3
0
        /// <summary>
        /// Get the last build
        /// </summary>
        /// <param name="jobName">Name of 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>Jenkins build data</returns>
        public async Task <JenkinsModelRun> GetLastBuildAsync(string jobName, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(jobName))
            {
                throw new ArgumentException(nameof(jobName));
            }

            JenkinsModelRun build = await GetApiBuildAsync <JenkinsModelRun>($"job/{jobName}/lastBuild", cancellationToken);

            return(build);
        }
Пример #4
0
        /// <summary>
        /// Get the last build
        /// </summary>
        /// <param name="jobName">Name of 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>Jenkins build data</returns>
        public async Task <JenkinsModelRun> GetLastBuildAsync(string jobName, CancellationToken cancellationToken)
        {
            if (string.IsNullOrEmpty(jobName))
            {
                throw new ArgumentException(nameof(jobName));
            }

            string str = await GetStringAsync($"job/{jobName}/lastBuild/api/xml", cancellationToken);

            JenkinsModelRun build = Deserialize <JenkinsModelRun>(str, buildTypes);

            return(build);
        }
Пример #5
0
 /// <summary>
 /// Refresh the properties of the Jenkins build.
 /// </summary>
 public void Update()
 {
     this.modelRun = JenkinsRun.Run(() => jenkins.GetBuildAsync <JenkinsModelRun>(this.JobName, this.modelRun.Number).Result);
 }
Пример #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="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);
        }