在 python/bash 脚本中编写 Git 挂钩

发布于 2024-09-11 04:23:18 字数 1539 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尝蛊 2024-09-18 04:23:18

这里是使用 Python 进行挂钩的示例。一般来说,钩子与语言无关。您可以使用该脚本执行某些工作或以 0/其他返回代码退出来更改 git 进程的流程。

Here is an example of using Python for a hook. In general the hooks are language agnostic. You use the script to do some work or to exit with a 0/other return code to alter the flow of a git process.

2024-09-18 04:23:18

git自带的例子是用shell脚本写的;每个存储库的 .git/hooks 中都有一些基本的,而更高级的则安装到 /usr/share/doc/git-core/contrib/hooks 中。

通过 $ man githooks 还可以获得有关各种挂钩的更多信息。

The examples that come with git are written in shell script; there are some basic ones in .git/hooks of each repo and more advanced ones installed to /usr/share/doc/git-core/contrib/hooks.

There's also more info on the various hooks available via $ man githooks.

迷爱 2024-09-18 04:23:18

我发现在python上写git hook很容易。这是 python 上的 post-receive hook 的一个例子。提供的示例在不同的文件夹中部署主分支和开发分支(主分支中的更改将被推送到生产网站,开发分支中的更改将被推送到质量保证站点)

#!/usr/bin/env python                                                                    
# -*- coding: UTF-8 -*-                                                                  
#post-receive                                                                            

import sys                                                                               
import subprocess                                                                        

# 1. Read STDIN (Format: "from_commit to_commit branch_name")                            
(old, new, branch) = sys.stdin.read().split()                                            

# 2. Only deploy if master branch was pushed                                             
if branch == 'refs/heads/master':                                                        
    subprocess.call('date >> ~/prod-deployment.log', shell=True)                         
    subprocess.call('GIT_WORK_TREE=/home/ft/app.prod git checkout master -f', shell=True)
    subprocess.call('cd ../../app.prod;bower update', shell=True)                        

#3. Only deploy if develop branch was pushed                                             
if branch == 'refs/heads/develop':                                                       
    subprocess.call('date >> ~/dev-deployment.log', shell=True)                          
    subprocess.call('GIT_WORK_TREE=/home/ft/app.dev git checkout develop -f', shell=True)
    subprocess.call('cd ../../app.dev;bower update', shell=True)                         

I found out that it's easy to write git hook on python. It's an example of post-receive hook on python. Provided example deploys master and develop branches in different folders (changes in master will be pushed to production website and changes in develop branch will be pushed to qa site)

#!/usr/bin/env python                                                                    
# -*- coding: UTF-8 -*-                                                                  
#post-receive                                                                            

import sys                                                                               
import subprocess                                                                        

# 1. Read STDIN (Format: "from_commit to_commit branch_name")                            
(old, new, branch) = sys.stdin.read().split()                                            

# 2. Only deploy if master branch was pushed                                             
if branch == 'refs/heads/master':                                                        
    subprocess.call('date >> ~/prod-deployment.log', shell=True)                         
    subprocess.call('GIT_WORK_TREE=/home/ft/app.prod git checkout master -f', shell=True)
    subprocess.call('cd ../../app.prod;bower update', shell=True)                        

#3. Only deploy if develop branch was pushed                                             
if branch == 'refs/heads/develop':                                                       
    subprocess.call('date >> ~/dev-deployment.log', shell=True)                          
    subprocess.call('GIT_WORK_TREE=/home/ft/app.dev git checkout develop -f', shell=True)
    subprocess.call('cd ../../app.dev;bower update', shell=True)                         
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文