【问题标题】:How to create jobs on Jenkins with credentials enabled?如何在启用凭据的情况下在 Jenkins 上创建作业?
【发布时间】:2013-12-24 07:03:24
【问题描述】:

我有代码可以在没有启用凭据的情况下在 Jenkins 上创建/更新作业。

const string FORMAT = "http://{0}:{1}/createItem?name={2}";
var path =
    string.Format(
        FORMAT,
        this.config.JenkinsServer,
        this.config.JenkinsPort,
        buildName);

this.http.UploadString(
    path,
    definition,
    new Header(HttpRequestHeader.ContentType, "text/xml"));

现在,我想通过启用凭据并在创建新作业时指定用户名/密码来向前迈出一步。我找到了an article about Authenticating scripted clients on Jenkins,但没有 C# 中的代码示例,也不知道该怎么做。

如何在启用凭据的情况下在 Jenkins 上创建/更新作业?

[更新]this link 中的 url 上指定用户名和密码也不起作用。

【问题讨论】:

标签: c# build jenkins


【解决方案1】:

您可以使用基于用户的 AUTHTOKEN 对 Jenkins 进行身份验证。 通过转到您的用户配置页面获取令牌。

http://{JENKINS.HOST}/user/{YOUR.USERNAME}/configure

然后单击“显示 API 令牌”按钮,它会将 GUID 显示为 AuthToken,然后您可以将其与以下内容一起使用。

string password = "5agbe1d6f774634169e9ba4625559bc83f";  // AKA the Jenkins AuthToken
string userName = "YOUR.USERNAME";                       // AKA your Jenkins userID

var webClient = new System.Net.WebClient();

// Create a basic auth token from a username and password seperated by a colon then base 64encoded
string basicAuthToken = Convert.ToBase64String(Encoding.Default.GetBytes(userName + ":" + password));
webClient.Headers["Authorization"] = "Basic " + basicAuthToken;

return client.UploadString(path, definition, headers);

【讨论】:

  • System.Net.WebClient 对我来说似乎没有一个名为 SetBasicAuthHeader 的函数
  • 你运行的是什么版本的 .Net
  • 4.5. Stack overthrow 有一个我正在填写的最小评论长度。
  • 啊,我的错,我一定指的是我写的扩展方法。我已经更新了答案以反映这一点
猜你喜欢
  • 1970-01-01
  • 2013-07-08
  • 2023-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-05
相关资源
最近更新 更多