使用git进行web开发跟踪管理,并自动部署


使用Gitblit对web开发跟踪管理,添加“自动部署”脚本,在push时将网页一并推送到webroot

方法:将以下代码保存为在gitblit/data/groovy/目录,文件名为deploy.groovy(注意此脚本仅适于用Gitblit)

/*
 * Copyright 2012 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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.CheckoutCommand
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.Repository
import org.eclipse.jgit.lib.RepositoryBuilder
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

/**
 * Sample Gitblit Post-Receive Hook: localclone
 *
 * The Post-Receive hook is executed AFTER the pushed commits have been applied
 * to the Git repository.  This is the appropriate point to trigger an
 * integration build or to send a notification.
 * 
 * This script is only executed when pushing to *Gitblit*, not to other Git
 * tooling you may be using.
 * 
 * If this script is specified in *groovy.postReceiveScripts* of gitblit.properties
 * or web.xml then it will be executed by any repository when it receives a
 * push.  If you choose to share your script then you may have to consider
 * tailoring control-flow based on repository access restrictions.
 *
 * Scripts may also be specified per-repository in the repository settings page.
 * Shared scripts will be excluded from this list of available scripts.
 * 
 * This script is dynamically reloaded and it is executed within it's own
 * exception handler so it will not crash another script nor crash Gitblit.
 *
 * If you want this hook script to fail and abort all subsequent scripts in the
 * chain, "return false" at the appropriate failure points.
 * 
 * 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
 *  
 */

// Indicate we have started the script
logger.info("deploy hook triggered by ${user.username} for ${repository.name}")

def rootFolder = '/opt/www'
def repoName = repository.name
def destinationFolder = new File(rootFolder, StringUtils.stripDotGit(repoName))
def sourceFolder = new File(gitblit.getRepositoriesFolder(), repoName)

logger.info("deploy hook checkout ${sourceFolder.absolutePath} to ${destinationFolder.absolutePath}")
if (destinationFolder.exists()) {
        logger.info("deploy hook checkout begin")
        Repository repo = new RepositoryBuilder()
                .setGitDir(sourceFolder) 
                .setWorkTree(destinationFolder)
                .build()

        logger.info("deploy of repo. ${repo.isBare()}")
        logger.info("deploy of repo. ${repo.getWorkTree()}")
        Git git = new Git(repo)
        CheckoutCommand cmd = git.checkout()
        cmd.setForce(true)
        cmd.setAllPaths(true)
        cmd.setName("master")
        cmd.setStartPoint("HEAD")
        cmd.call();
        git.repository.close()
}
else
{
        logger.info("deploy hook checkout ignore")
}

clientLogger.info("${repoName} deploy to ${destinationFolder}")


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值