增加环境变量支持

This commit is contained in:
yashin 2018-08-16 15:26:37 +08:00
parent 2fdaa6af40
commit 138434afeb
4 changed files with 103 additions and 4 deletions

View File

@ -17,6 +17,7 @@
- [新建码云项目WebHook](#新建码云项目WebHook)
- [测试推送触发构建](#测试推送触发构建)
- [测试PR触发构建](#测试PR触发构建)
- [环境变量](#环境变量)
- [用户支持](#用户支持)
- [参与贡献](#参与贡献)
- [打包或运行测试](#打包或运行测试)
@ -163,6 +164,55 @@ Gitee Jenkins Plugin 是码云基于 [GitLab Plugin](https://github.com/jenkinsc
1. 码云的 WebHook 管理中选择勾选了 Pull Request 的 WebHook 点击测试,观察 Jenkins 任务的构建状态
2. 在码云项目中新建一个Pull Request观察 Jenkins 任务的构建状态
# 环境变量
目前支持环境变量见以下函数,其中不同的 WebHook 触发可能导致有些变量为空,具体请安装插件 [EnvInject Plugin](https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin),于构建中查看 Environment Variables
```java
public Map<String, String> getBuildVariables() {
MapWrapper<String, String> variables = new MapWrapper<>(new HashMap<String, String>());
variables.put("giteeBranch", branch);
variables.put("giteeSourceBranch", sourceBranch);
variables.put("giteeActionType", actionType.name());
variables.put("giteeUserName", userName);
variables.put("giteeUserEmail", userEmail);
variables.put("giteeSourceRepoHomepage", sourceRepoHomepage);
variables.put("giteeSourceRepoName", sourceRepoName);
variables.put("giteeSourceNamespace", sourceNamespace);
variables.put("giteeSourceRepoURL", sourceRepoUrl);
variables.put("giteeSourceRepoSshUrl", sourceRepoSshUrl);
variables.put("giteeSourceRepoHttpUrl", sourceRepoHttpUrl);
variables.put("giteePullRequestTitle", pullRequestTitle);
variables.put("giteePullRequestDescription", pullRequestDescription);
variables.put("giteePullRequestId", pullRequestId == null ? "" : pullRequestId.toString());
variables.put("giteePullRequestIid", pullRequestIid == null ? "" : pullRequestIid.toString());
variables.put("giteePullRequestTargetProjectId", pullRequestTargetProjectId == null ? "" : pullRequestTargetProjectId.toString());
variables.put("giteePullRequestLastCommit", lastCommit);
variables.put("giteePushCreated", created ? "true" : "false");
variables.put("giteePushDeleted", deleted ? "true" : "false");
variables.putIfNotNull("giteePullRequestState", pullRequestState);
variables.putIfNotNull("giteeMergedByUser", mergedByUser);
variables.putIfNotNull("giteePullRequestAssignee", pullRequestAssignee);
variables.put("giteeTargetBranch", targetBranch);
variables.put("giteeTargetRepoName", targetRepoName);
variables.put("giteeTargetNamespace", targetNamespace);
variables.put("giteeTargetRepoSshUrl", targetRepoSshUrl);
variables.put("giteeTargetRepoHttpUrl", targetRepoHttpUrl);
variables.put("giteeBefore", before);
variables.put("giteeAfter", after);
variables.put("ref", ref);
variables.put("beforeSha", beforeSha);
variables.put("isTag", isTag);
variables.put("sha", sha);
variables.put("status", status);
variables.put("stages", stages);
variables.put("createdAt", createdAt);
variables.put("finishedAt", finishedAt);
variables.put("duration", buildDuration);
variables.putIfNotNull("giteeTriggerPhrase", triggerPhrase);
return variables;
}
```
# 用户支持
如在使用过程中有任何疑问,欢迎在 [Gitee Jenkins Issue](https://gitee.com/oschina/Gitee-Jenkins-Plugin/issues) 中反馈。

View File

@ -59,15 +59,18 @@ public final class CauseData {
private final String createdAt;
private final String finishedAt;
private final String buildDuration;
private final boolean created;
private final boolean deleted;
@GeneratePojoBuilder(withFactoryMethod = "*")
CauseData(ActionType actionType, Integer sourceProjectId, Integer targetProjectId, String branch, String sourceBranch, String userName,
String userEmail, String sourceRepoHomepage, String sourceRepoName, String sourceNamespace, String sourceRepoUrl,
String sourceRepoSshUrl, String sourceRepoHttpUrl, String pullRequestTitle, String pullRequestDescription, Integer pullRequestId,
Integer pullRequestIid, Integer pullRequestTargetProjectId, String targetBranch, String targetRepoName, String targetNamespace, String targetRepoSshUrl,
String targetRepoHttpUrl, String triggeredByUser, String before, String after, String lastCommit, String targetProjectUrl,
String triggerPhrase, String pullRequestState, String mergedByUser, String pullRequestAssignee, String ref, String isTag,
String sha, String beforeSha, String status, String stages, String createdAt, String finishedAt, String buildDuration, String pathWithNamespace) {
Integer pullRequestIid, Integer pullRequestTargetProjectId, String targetBranch, String targetRepoName, String targetNamespace,
String targetRepoSshUrl, String targetRepoHttpUrl, String triggeredByUser, String before, String after, String lastCommit,
String targetProjectUrl, String triggerPhrase, String pullRequestState, String mergedByUser, String pullRequestAssignee,
String ref, String isTag, String sha, String beforeSha, String status, String stages, String createdAt, String finishedAt,
String buildDuration, String pathWithNamespace, boolean created, boolean deleted) {
this.actionType = checkNotNull(actionType, "actionType must not be null.");
this.sourceProjectId = checkNotNull(sourceProjectId, "sourceProjectId must not be null.");
this.targetProjectId = checkNotNull(targetProjectId, "targetProjectId must not be null.");
@ -112,6 +115,8 @@ public final class CauseData {
this.finishedAt = finishedAt;
this.buildDuration = buildDuration;
this.pathWithNamespace = pathWithNamespace;
this.created = created;
this.deleted = deleted;
}
public Map<String, String> getBuildVariables() {
@ -133,6 +138,8 @@ public final class CauseData {
variables.put("giteePullRequestIid", pullRequestIid == null ? "" : pullRequestIid.toString());
variables.put("giteePullRequestTargetProjectId", pullRequestTargetProjectId == null ? "" : pullRequestTargetProjectId.toString());
variables.put("giteePullRequestLastCommit", lastCommit);
variables.put("giteePushCreated", created ? "true" : "false");
variables.put("giteePushDeleted", deleted ? "true" : "false");
variables.putIfNotNull("giteePullRequestState", pullRequestState);
variables.putIfNotNull("giteeMergedByUser", mergedByUser);
variables.putIfNotNull("giteePullRequestAssignee", pullRequestAssignee);
@ -305,6 +312,15 @@ public final class CauseData {
return pullRequestAssignee;
}
public boolean getCreated() {
return created;
}
public boolean getDeleted() {
return deleted;
}
public PullRequest getPullRequest() {
if (pullRequestId == null) {
return null;
@ -365,6 +381,8 @@ public final class CauseData {
.append(finishedAt, causeData.getFinishedAt())
.append(buildDuration, causeData.getBuildDuration())
.append(pathWithNamespace, causeData.getPathWithNamespace())
.append(created, causeData.getCreated())
.append(deleted, causeData.getDeleted())
.isEquals();
}
@ -412,6 +430,8 @@ public final class CauseData {
.append(finishedAt)
.append(buildDuration)
.append(pathWithNamespace)
.append(created)
.append(deleted)
.toHashCode();
}
@ -459,6 +479,8 @@ public final class CauseData {
.append("finishedAt", finishedAt)
.append("duration", buildDuration)
.append("pathWithNamespace", pathWithNamespace)
.append("created", created)
.append("deleted", deleted)
.toString();
}

View File

@ -15,6 +15,8 @@ public class PushHook extends WebHook {
private String before;
private String after;
private boolean created;
private boolean deleted;
private String ref;
private Integer userId;
private String userName;
@ -32,6 +34,23 @@ public class PushHook extends WebHook {
this.before = before;
}
public boolean getCreated() {
return created;
}
public void setCreated(boolean created) {
this.created = created;
}
public boolean getDeleted() {
return deleted;
}
public void setDeleted(boolean deleted) {
this.deleted = deleted;
}
public String getAfter() {
return after;
}
@ -127,6 +146,8 @@ public class PushHook extends WebHook {
PushHook pushHook = (PushHook) o;
return new EqualsBuilder()
.append(before, pushHook.before)
.append(created, pushHook.created)
.append(deleted, pushHook.deleted)
.append(after, pushHook.after)
.append(ref, pushHook.ref)
.append(userId, pushHook.userId)
@ -145,6 +166,8 @@ public class PushHook extends WebHook {
.append(before)
.append(after)
.append(ref)
.append(created)
.append(deleted)
.append(userId)
.append(userName)
.append(userEmail)
@ -161,6 +184,8 @@ public class PushHook extends WebHook {
.append("before", before)
.append("after", after)
.append("ref", ref)
.append("created", created)
.append("deleted", deleted)
.append("userId", userId)
.append("userName", userName)
.append("userEmail", userEmail)

View File

@ -96,6 +96,8 @@ class PushHookTriggerHandlerImpl extends AbstractWebHookTriggerHandler<PushHook>
.withAfter(hook.getAfter())
.withRef(hook.getRef())
.withLastCommit(hook.getAfter())
.withCreated(hook.getCreated())
.withDeleted(hook.getDeleted())
.withTargetProjectUrl(hook.getProject().getUrl())
.build();
}