我在jenkins官方码头中心的码头工具中使用Jenkins(但我甚至尝试过我们在bluemix实例上的jenkins)。
我正在编写程序(当前测试驱动)从java触发作业然后使用获取作业ID jenkins api。
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秒才能在/ api / json中工作。
你知道会出现什么问题吗?
如何调整它?
答案 0 :(得分:1)
请检查两次执行之间是否还需要延迟。
触发工作
public class DigitalSignature : ContentPage
{
SignaturePadView sign = new SignaturePadView();
public DigitalSignature()
{
Button btnOk = new Button
{
Text = "Ok",
BackgroundColor = Color.FromHex("#ff6600"),
HorizontalOptions = LayoutOptions.End,
WidthRequest = 100,
HeightRequest = 35,
FontSize = 15
};
btnOk.Clicked += btnOk_Clicked;
sign = new SignaturePadView()
{
SignatureLineColor = Color.Red,
StrokeColor = Color.Black,
StrokeWidth = 10f,
HeightRequest = 150,
BackgroundColor = Color.White,
ClearText = "Clear Me"
};
sign.CaptionText = "pls sign here";
Content = new StackLayout
{
Children = {
sign,
btnOk
}
};
}
private void btnOk_Clicked(object sender, EventArgs e)
{
}
}
获取职位信息
curl -X POST http://${JENKINS_HOTS}:${JENKINS_PORT}/job/${JOB_NAME}/build \
--user ${USER}:${PASSWORD} \
--data-urlencode json='{"parameter": [{"name":"delay", "value":"0sec"}]}'
如果您仍需要等待大约8秒钟,请检查作业中curl http://${JENKINS_HOTS}:${JENKINS_PORT}/job/${JOB_NAME}/api/json \
--user ${USER}:${PASSWORD}
的设置。如果尚未启用,请启用它并将期间设置为quiet period
。这应该消除执行之间的延迟。
根据Jenkins实例的工作量,即使是零秒的时间段,您也可能需要等待一段时间。