如何在启用凭据的 Jenkins 上创建作业?

Ano*_*ous 2 c# build jenkins

我有代码可以在没有启用凭据的情况下在 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"));
Run Code Online (Sandbox Code Playgroud)

现在,我想通过启用凭据并在创建新作业时指定用户名/密码来向前迈进一步。我在 Jenkins 上找到了一篇关于 Authenticating scripted clients 的文章,但没有 C# 中的代码示例,无法弄清楚如何去做。

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

[更新]这个链接一样在 url 上指定用户名和密码也不起作用。

Cod*_*Tim 5

您可以使用基于用户的 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);
Run Code Online (Sandbox Code Playgroud)