James Moger
2015-12-23 2539ceea0d47467d54cedd340afa6ede2909b2bd
commit | author | age
fa54be 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 import com.gitblit.GitBlit
17 import com.gitblit.Keys
18 import com.gitblit.models.RepositoryModel
19 import com.gitblit.models.UserModel
20 import com.gitblit.utils.JGitUtils
21 import org.eclipse.jgit.lib.Repository
22 import org.eclipse.jgit.revwalk.RevCommit
23 import org.eclipse.jgit.transport.ReceiveCommand
24 import org.eclipse.jgit.transport.ReceiveCommand.Result
25 import org.slf4j.Logger
26
27 /**
28  * Sample Gitblit Post-Receive Hook: jenkins
29  *
30  * The Post-Receive hook is executed AFTER the pushed commits have been applied
31  * to the Git repository.  This is the appropriate point to trigger an
32  * integration build or to send a notification.
33  * 
34  * This script is only executed when pushing to *Gitblit*, not to other Git
35  * tooling you may be using.
36  * 
37  * If this script is specified in *groovy.postReceiveScripts* of gitblit.properties
38  * or web.xml then it will be executed by any repository when it receives a
39  * push.  If you choose to share your script then you may have to consider
40  * tailoring control-flow based on repository access restrictions.
41  *
42  * Scripts may also be specified per-repository in the repository settings page.
43  * Shared scripts will be excluded from this list of available scripts.
44  * 
45  * This script is dynamically reloaded and it is executed within it's own
46  * exception handler so it will not crash another script nor crash Gitblit.
47  * 
48  * Bound Variables:
2f5d15 49  *  gitblit            Gitblit Server                 com.gitblit.GitBlit
JM 50  *  repository        Gitblit Repository            com.gitblit.models.RepositoryModel
6bb3b2 51  *  receivePack        JGit Receive Pack            org.eclipse.jgit.transport.ReceivePack
2f5d15 52  *  user            Gitblit User                com.gitblit.models.UserModel
JM 53  *  commands        JGit commands                 Collection<org.eclipse.jgit.transport.ReceiveCommand>
54  *    url                Base url for Gitblit        String
55  *  logger            Logs messages to Gitblit     org.slf4j.Logger
56  *  clientLogger    Logs messages to Git client    com.gitblit.utils.ClientLogger
7c1cdc 57  *
JM 58  * Accessing Gitblit Custom Fields:
59  *   def myCustomField = repository.customFields.myCustomField
fa54be 60  *  
JM 61  */
62 // Indicate we have started the script
63 logger.info("jenkins hook triggered by ${user.username} for ${repository.name}")
64
65 // This script requires Jenkins Git plugin 1.1.14 or later
66 // http://kohsuke.org/2011/12/01/polling-must-die-triggering-jenkins-builds-from-a-git-hook/
67
68 // define your jenkins url here or set groovy.jenkinsServer in 
69 // gitblit.properties or web.xml
198fa1 70 def jenkinsUrl = gitblit.getString('groovy.jenkinsServer', 'http://yourserver/jenkins')
fa54be 71
306116 72 // define the repository base url
R 73 def jenkinsGitbaseurl = gitblit.getString('groovy.jenkinsGitbaseurl', "${url}/r")
74
fa54be 75 // define the trigger url
306116 76 def triggerUrl = jenkinsUrl + "/git/notifyCommit?url=" + jenkinsGitbaseurl + "/${repository.name}"
fa54be 77
JM 78 // trigger the build
79 new URL(triggerUrl).getContent()