Skip to content

Commit b45f397

Browse files
committed
Merge pull request jenkinsci#68 from mrwanny/enable_tags
build when tags are pushed
2 parents 3d066f1 + 4719ecb commit b45f397

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,15 @@ Triggers from push events may be filtered based on the branch name, i.e. the bui
6161

6262
This functionality requires accessing the Gitlab server (see [above](#configuring-access-to-gitlab)) and for the time being also a git repository url already saved in the project configuration. In other words, when creating a new project, the configuration needs to be saved *once* before being able to select the allowed branches. For existing projects, all branches are allowed to push by default.
6363

64+
Build Tags
65+
================
66+
67+
In order to build when a new tag is pushed:
68+
* In the ``GitLab server`` add ``Tag push events`` to the ``Web Hook``
69+
* In the ``Jenkins`` under the ``Source Code Management`` section:
70+
* select ``Advance...`` and add ``+refs/tags/*:refs/remotes/origin/tags/*`` as ``Refspec``
71+
* you can also use ``Branch Specifier`` to specify which tag need to be built (exampple ``refs/tags/${TAGNAME}``)
72+
6473
Parameterized builds
6574
====================
6675

src/main/java/com/dabsquared/gitlabjenkins/GitLabPushRequest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public GitLabPushRequest() {
2626

2727
private String before;
2828
private String after;
29+
private String checkout_sha;
2930
private String ref;
3031
private Integer user_id;
3132
private String user_name;
@@ -117,6 +118,14 @@ public String toString() {
117118
return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE);
118119
}
119120

121+
public String getCheckout_sha() {
122+
return checkout_sha;
123+
}
124+
125+
public void setCheckout_sha(String checkout_sha) {
126+
this.checkout_sha = checkout_sha;
127+
}
128+
120129
public static class Repository {
121130

122131
private String name;

src/main/java/com/dabsquared/gitlabjenkins/GitLabPushTrigger.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,12 @@ public void run() {
119119

120120
private GitLabPushCause createGitLabPushCause(GitLabPushRequest req) {
121121
GitLabPushCause cause;
122-
String triggeredByUser = req.getCommits().get(0).getAuthor().getName();
122+
String triggeredByUser;
123+
if (req.getCommits().size() > 0){
124+
triggeredByUser = req.getCommits().get(0).getAuthor().getName();
125+
} else {
126+
triggeredByUser = req.getUser_name();
127+
}
123128
try {
124129
cause = new GitLabPushCause(triggeredByUser, getLogFile());
125130
} catch (IOException ex) {
@@ -139,17 +144,28 @@ private Action[] createActions(GitLabPushRequest req) {
139144
values.put("gitlabSourceBranch", new StringParameterValue("gitlabSourceBranch", branch));
140145
values.put("gitlabTargetBranch", new StringParameterValue("gitlabTargetBranch", branch));
141146
values.put("gitlabBranch", new StringParameterValue("gitlabBranch", branch));
142-
143-
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0} using project {1} (push)", new String[]{job.getName(), getDesc().project.getName()});
147+
148+
LOGGER.log(Level.INFO, "Trying to get name and URL for job: {0} using project {1} (push)", new String[]{job.getName(), job.getRootProject().getName()});
144149
values.put("gitlabSourceRepoName", new StringParameterValue("gitlabSourceRepoName", getDesc().getSourceRepoNameDefault(job)));
145150
values.put("gitlabSourceRepoURL", new StringParameterValue("gitlabSourceRepoURL", getDesc().getSourceRepoURLDefault(job).toString()));
146151

147152
List<ParameterValue> listValues = new ArrayList<ParameterValue>(values.values());
148153

149154
ParametersAction parametersAction = new ParametersAction(listValues);
150155
actions.add(parametersAction);
156+
RevisionParameterAction revision;
157+
158+
if (req.getLastCommit() !=null) {
159+
revision = new RevisionParameterAction(req.getLastCommit().getId());
160+
}else{
161+
if (req.getCheckout_sha().contains("0000000000000000000000000000000000000000") ){
162+
// no commit and no checkout sha, a Tag was deleted, so no build need to be triggered
163+
LOGGER.log(Level.INFO, "GitLab Push {0} has been deleted, skip build .", req.getRef());
164+
return null;
165+
}
166+
revision = new RevisionParameterAction(req.getCheckout_sha());
167+
}
151168

152-
RevisionParameterAction revision = new RevisionParameterAction(req.getLastCommit().getId());
153169
actions.add(revision);
154170
Action[] actionsArray = actions.toArray(new Action[0]);
155171

0 commit comments

Comments
 (0)