如何通过Ansible使用Githooks
所有 git挂钩是普通的脚本,是git执行时在存储库中发生某些事件时执行的。
当我使用 Ansible的内置git模块?
以下演示书在/tmp
文件夹中创建一个存储库和克隆。 post-Merge
-script将行添加到文件中。 Ansible模块忽略了此脚本,但是Shell-Command git Pull
创建了新行。
$ cat git-demo.yml
---
- name: git pull demo
hosts: localhost
tasks:
- name: create directory
file:
dest: /tmp/demo
state: directory
- name: init repo
shell: git -C /tmp/demo init
- name: create first commit
shell: date >> /tmp/demo/test.txt;git -C /tmp/demo add .;git -C /tmp/demo commit -m1
- name: clone demo
ansible.builtin.git:
repo: /tmp/demo
dest: /tmp/test
- name: create hook-script
copy:
dest: /tmp/test/.git/hooks/post-merge
content: |
#!/bin/sh
date +"%F %T Demonstrating git-hook" >> /tmp/demo/test.txt
mode: 0755
- name: create second commit
shell: date >> /tmp/demo/test.txt;git -C /tmp/demo commit -am2
- name: git pull without hook
ansible.builtin.git:
repo: /tmp/demo
dest: /tmp/test
- name: create third commit
shell: date >> /tmp/demo/test.txt;git -C /tmp/demo commit -am3
- name: git pull with hook-script
shell: git -C /tmp/test pull
只有Shell模块调用钩子标记:
$ ansible-playbook git-demo.yml
$ cat /tmp/demo/test.txt
Thu 24 Mar 2022 02:13:06 AM CET
Thu 24 Mar 2022 02:13:08 AM CET
Thu 24 Mar 2022 02:13:08 AM CET
2022-03-24 02:13:09 Demonstrating git-hook
我使用了Git版本2.25.1和Ansible版本2.10.17。
All Git hooks are ordinary scripts that Git executes when certain events occur in the repository.
Can I use these client-side hooks, when i update my repository with the builtin git-module from ansible?
The following Demo-Playbook creates a repository and a clone in the /tmp
folder.
The post-merge
-script adds a line to a file.
The ansible-module ignores this script, but the shell-command git pull
creates the new line.
$ cat git-demo.yml
---
- name: git pull demo
hosts: localhost
tasks:
- name: create directory
file:
dest: /tmp/demo
state: directory
- name: init repo
shell: git -C /tmp/demo init
- name: create first commit
shell: date >> /tmp/demo/test.txt;git -C /tmp/demo add .;git -C /tmp/demo commit -m1
- name: clone demo
ansible.builtin.git:
repo: /tmp/demo
dest: /tmp/test
- name: create hook-script
copy:
dest: /tmp/test/.git/hooks/post-merge
content: |
#!/bin/sh
date +"%F %T Demonstrating git-hook" >> /tmp/demo/test.txt
mode: 0755
- name: create second commit
shell: date >> /tmp/demo/test.txt;git -C /tmp/demo commit -am2
- name: git pull without hook
ansible.builtin.git:
repo: /tmp/demo
dest: /tmp/test
- name: create third commit
shell: date >> /tmp/demo/test.txt;git -C /tmp/demo commit -am3
- name: git pull with hook-script
shell: git -C /tmp/test pull
Only the shell-module invoked the hook-script:
$ ansible-playbook git-demo.yml
$ cat /tmp/demo/test.txt
Thu 24 Mar 2022 02:13:06 AM CET
Thu 24 Mar 2022 02:13:08 AM CET
Thu 24 Mar 2022 02:13:08 AM CET
2022-03-24 02:13:09 Demonstrating git-hook
I have used git version 2.25.1 and ansible version 2.10.17.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如注释中所述,没有
拉动
或合并
发生。如果您希望该脚本在每次Ansible克隆/检查存储库时运行邮政检查 挂钩。
但是,由于您已经在自动化它,我建议使用Ansible触发脚本而不是钩子。
您可以在
git
模块上注册变量(例如git_result
),然后根据git_result.changed
来决定该操作。As mentioned in the comment, there is no
pull
ormerge
happening.If you want that script to run every time ansible clones/checks out the repository, try the
post-checkout
hook.But as you are already automating it, I would suggest to use ansible to trigger the script instead of a hook.
You could register a variable (e.g.
git_result
) on yourgit
module and then decide what to do based ongit_result.changed
.