所有吉特钩都是Git在存储库中发生某些事件时执行的普通脚本。
当我用基于ansible的内置git模块更新我的存储库时,我可以使用这些客户端钩子吗?
下面的演示-Playbook在/tmp文件夹中创建一个存储库和一个克隆。post-merge-script将一行添加到文件中。ansible-模块忽略了这个脚本,但是shell命令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。
发布于 2022-05-24 06:19:11
正如注释中提到的,没有发生pull或merge。
如果希望该脚本每次在ansible克隆/签出存储库时运行,请尝试post-checkout钩子。
但是,由于您已经在自动化它,我建议使用ansible来触发脚本而不是钩子。
您可以在您的git_result模块上注册一个变量(例如git ),然后根据git_result.changed决定该做什么。
https://stackoverflow.com/questions/72351152
复制相似问题