Last active
January 11, 2020 22:56
-
-
Save MoriTanosuke/5884992 to your computer and use it in GitHub Desktop.
This is a simple post-receive-hook Groovy script for gitblit.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.gitblit.GitBlit | |
import com.gitblit.Keys | |
import com.gitblit.models.RepositoryModel | |
import com.gitblit.models.TeamModel | |
import com.gitblit.models.UserModel | |
import com.gitblit.utils.JGitUtils | |
import com.gitblit.utils.StringUtils | |
import java.text.SimpleDateFormat | |
import org.eclipse.jgit.api.CloneCommand | |
import org.eclipse.jgit.api.Git | |
import org.eclipse.jgit.lib.Repository | |
import org.eclipse.jgit.lib.Config | |
import org.eclipse.jgit.revwalk.RevCommit | |
import org.eclipse.jgit.transport.ReceiveCommand | |
import org.eclipse.jgit.transport.ReceiveCommand.Result | |
import org.eclipse.jgit.util.FileUtils | |
import org.slf4j.Logger | |
/** | |
* Gitblit post-receive-hook to start jekyll on a repository. | |
* | |
* Written by Carsten Ringe "[email protected]". | |
* | |
* Bound Variables: | |
* gitblit Gitblit Server com.gitblit.GitBlit | |
* repository Gitblit Repository com.gitblit.models.RepositoryModel | |
* receivePack JGit Receive Pack org.eclipse.jgit.transport.ReceivePack | |
* user Gitblit User com.gitblit.models.UserModel | |
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand> | |
* url Base url for Gitblit String | |
* logger Logs messages to Gitblit org.slf4j.Logger | |
* clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger | |
* | |
* Accessing Gitblit Custom Fields: | |
* def myCustomField = repository.customFields.myCustomField | |
*/ | |
logger.info("jekyll hook triggered by ${user.username} for ${repository.name}") | |
def rootFolder = '/tmp' | |
def bare = false | |
def cloneAllBranches = false | |
def cloneBranch = 'refs/heads/master' | |
def includeSubmodules = true | |
def repoName = repository.name | |
def sanitizedRepoName = StringUtils.stripDotGit(repoName) | |
def destinationFolder = new File(rootFolder, sanitizedRepoName) | |
def wwwFolder = '/var/lib/tomcat7/webapps/' + sanitizedRepoName | |
def srcUrl = 'file://' + new File(GitBlit.getRepositoriesFolder(), repoName).absolutePath | |
// delete any previous clone | |
if (destinationFolder.exists()) { | |
FileUtils.delete(destinationFolder, FileUtils.RECURSIVE) | |
} | |
// clone the repository | |
logger.info("cloning ${srcUrl} to ${destinationFolder}") | |
CloneCommand cmd = Git.cloneRepository(); | |
cmd.setBare(bare) | |
if (cloneAllBranches) | |
cmd.setCloneAllBranches(true) | |
else | |
cmd.setBranch(cloneBranch) | |
cmd.setCloneSubmodules(includeSubmodules) | |
cmd.setURI(srcUrl) | |
cmd.setDirectory(destinationFolder) | |
Git git = cmd.call(); | |
git.repository.close() | |
// report clone operation success back to pushing Git client | |
logger.info("${repoName} cloned to ${destinationFolder}") | |
"mkdir ${wwwFolder}".execute() | |
def build = "/usr/local/bin/jekyll build -s ${destinationFolder} -d ${wwwFolder}".execute().text | |
logger.info(build) | |
logger.info("${repoName} build with jekyll to ${wwwFolder}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment