0
我在詹金斯官方碼頭中心使用Jenkins在我的本地碼頭工人(但我甚至嘗試過jenkins,我們在bluemix實例上)。Jenkins api太慢
我正在寫程序(當前測試驅動)從java觸發作業,然後使用 jenkins api獲取作業ID。
Properties jenkinsProps = new Properties();
InputStream jenkinsPropsIs = Files.newInputStream(jenkinsPropsFilePath);
jenkinsProps.load(jenkinsPropsIs);
// for building url
String jenkinsServerUrl = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_SERVER_URL);
String jobName = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_JOB_NAME);
String jobRemoteAccessToken = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_JOB_ACCESS_TOKEN);
// for headers
String jenkinsUser = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_USERNAME);
String jenkinsUserApiToken = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_API_TOKEN);
String jenkinsCrumb = jenkinsProps.getProperty(JenkinsPropertiesKeys.KEY_JENKINS_CSRF_CRUMB);
// build parameters
Map<String, String> params = new LinkedHashMap<>();
params.put("param1", "test1");
params.put("param2", "test2");
params.put("param3", "test3");
// Jenkins cause - to identify which process had run this job
String procID = UUID.randomUUID().toString();
params.put("cause", procID);
String url = getJenkinsBuildWithParametersUrl(jenkinsServerUrl, jobName, jobRemoteAccessToken, params);
WebRequest request = new WebRequest(); // own HttpConnection based client
// setup Jenkins crumb to avoid CSRF
request.setHeader(HEADER_NAME_JENKINS_CRUMB, jenkinsCrumb);
// user authentification (Basic + base64 encoded user:apiToken)
setupAuthenticationHeader(request, jenkinsUser, jenkinsUserApiToken);
// execute POST request
request = request.post(url);
// asserts
assertNotNull(request);
assertEquals(201, request.getResponseCode());
/* GET JOB ID */
Thread.currentThread().sleep(8000); // !!! if less then 8sec, jenkins returns old job number
request.reset();
setupAuthenticationHeader(request, jenkinsUser, jenkinsUserApiToken);
url = getJenkinsLastBuildUrl(jenkinsServerUrl, jobName);
// execute get request to /api/json
request = request.get(url);
assertTrue(request.isOK());
// get note & compare with proc id, to match job
String jenkinsJobProcId = null;
JsonObject jenkinsLastBuildJson = request.getResponseAsJson();
JsonArray jenkinsActions = jenkinsLastBuildJson.get("actions").getAsJsonArray();
for (JsonElement action : jenkinsActions) {
JsonObject actionJson = action.getAsJsonObject();
if (actionJson.get("_class").getAsString().equals("hudson.model.CauseAction")) {
JsonArray causeActionJsonArray = actionJson.get("causes").getAsJsonArray();
for (JsonElement cause : causeActionJsonArray) {
JsonObject causeJson = cause.getAsJsonObject();
if (causeJson.get("_class").getAsString().equals("hudson.model.Cause$RemoteCause")) {
jenkinsJobProcId = causeJson.get("note").getAsString();
break;
}
}
if (!jenkinsJobProcId.isEmpty()) {
break;
}
}
}
System.out.println("LastBuild prodId : " + jenkinsJobProcId);
assertEquals(procID, jenkinsJobProcId);
// get jenkins build number
int lastBuildNumber = jenkinsLastBuildJson.get("number").getAsInt();
System.out.println("LastBuild buildNumber : " + lastBuildNumber);
assertTrue(lastBuildNumber > 0);
一旦我觸發工作,它需要8秒,以工作apear在/ api/json中。
你知道可能是什麼問題嗎?
如何調整它?
你應該提供一些更多的細節。 1)你使用什麼代碼? - 提供一個片段。 2)你已經試圖縮小問題的範圍了? - 我做了A並得到了B。3)你的硬件規格是什麼? - CPU,RAM,磁盤。 ....類似的東西。 – SubOptimal
我使用標準jenkins apis,用java httpconnection調用。我在我的macbook pro上運行docker上的jenkins。沒有什麼特別的描述。 –
你如何衡量成功的作業觸發和api中的可視性之間的時間?任何有用的Jenkins日誌? – SubOptimal