コード例 #1
0
ファイル: Jenkins.cs プロジェクト: Bassman2/JenkinsWebApi
        /// <summary>
        /// Initializes a new instance of the Jenkins class.
        /// </summary>
        /// <param name="host">Host URL of the Jenkins server.</param>
        /// <param name="login">Login for the Jenkins server.</param>
        /// <param name="passwordOrToken">Password or API token for the Jenkins server.</param>
        public Jenkins(Uri host, string login, string passwordOrToken)
        {
            Connect(host, login, passwordOrToken);

            // init variables
            this.RunConfig = new JenkinsRunConfig();
        }
コード例 #2
0
ファイル: Jenkins.Job.cs プロジェクト: Bassman2/JenkinsWebApi
        /// <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);
        }
コード例 #3
0
ファイル: Jenkins.Job.cs プロジェクト: Bassman2/JenkinsWebApi
 /// <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));
 }