public string GetJenkinsCrumb() { SavoryWebClient client = new SavoryWebClient(); client.Host = $"{GlobalVariables.JenkinsHost}/crumbIssuer/api/json"; client.HttpMethod = Utility.WebClient.HttpMethod.Get; client.SerializeMode = SerializeMode.Json; client.Authorization = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(GlobalVariables.JenkinsUserId + ":" + GlobalVariables.JenkinsApiToken)); var response = client.Request <CrumbResponse>(); return(response.Crumb); }
public void Build(string jobName, BuildRequest request) { SavoryWebClient client = new SavoryWebClient(); client.Host = $"{GlobalVariables.JenkinsHost}/job/{jobName}/build"; client.HttpMethod = Utility.WebClient.HttpMethod.Post; client.Authorization = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(GlobalVariables.JenkinsUserId + ":" + GlobalVariables.JenkinsApiToken)); client.ContentType = "application/x-www-form-urlencoded"; request.JenkinsCrumb = GetJenkinsCrumb(); client.Content = "json=" + JsonConvert.SerializeObject(request); var content = client.RequestForString(); }
private static bool IsJobExists(string name) { try { SavoryWebClient client = new SavoryWebClient(); client.Authorization = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(GlobalVariables.JenkinsUserId + ":" + GlobalVariables.JenkinsApiToken)); client.HttpMethod = Utility.WebClient.HttpMethod.Get; client.Host = $"{GlobalVariables.JenkinsHost}/job/{name}/config.xml"; client.RequestForString(); return(true); } catch (WebException ex) { var httpWebResponse = (HttpWebResponse)ex.Response; if (httpWebResponse.StatusCode == HttpStatusCode.NotFound) { return(false); } throw; } }
public BuildSetupResponse Setup(BuildSetupRequest request) { BuildSetupResponse response = new BuildSetupResponse(); ProjectVo appVo = null; using (var sqliteConn = ConnectionProvider.GetSqliteConn()) { appVo = sqliteConn.QueryFirstOrDefault <ProjectVo>("select * from project where Name = @Name", new { Name = request.Name }); if (appVo == null) { response.Message = "app not found"; return(response); } } ProcessEntity processEntity = null; using (var sqliteConn = ConnectionProvider.GetSqliteConn()) { processEntity = sqliteConn.QueryFirstOrDefault <ProcessEntity>("select * from process where Name = @Name", new { Name = appVo.TemplateName }); if (processEntity == null) { response.Message = "template not found"; return(response); } } var process = new CSharpLibraryTemplate(); var steps = JsonConvert.DeserializeObject <List <StepVo> >(processEntity.Steps); process.Cmds = steps.Select(v => v.Command).ToList(); List <Param> paramList = new List <Param>(); paramList.Add(new Param { Key = "Repository", Description = "git repository" }); paramList.Add(new Param { Key = "Branch", Description = "git branch", DefaultValue = "master" }); paramList.Add(new Param { Key = "BuildVersion", Description = "build version" }); paramList.Add(new Param { Key = "OutputFolder", Description = "Output Folder" }); if (!string.IsNullOrEmpty(processEntity.Params)) { var items = JsonConvert.DeserializeObject <List <ParamVo> >(processEntity.Params); foreach (var item in items) { paramList.Add(new Param { Key = item.Key, DefaultValue = item.DefaultValue, Description = item.Description }); } } process.Params = paramList; string configXml = process.TransformText(); SavoryWebClient client = new SavoryWebClient(); client.Authorization = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(GlobalVariables.JenkinsUserId + ":" + GlobalVariables.JenkinsApiToken)); client.HttpMethod = Utility.WebClient.HttpMethod.Post; client.Content = configXml; client.ContentType = "application/xml"; if (IsJobExists(appVo.Name)) { client.Host = $"{GlobalVariables.JenkinsHost}/job/{appVo.Name}/config.xml"; } else { client.Host = $"{GlobalVariables.JenkinsHost}/createItem?name={appVo.Name}"; } response.Message = client.RequestForString(); response.Status = 1; return(response); }