首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过ansible使用githooks

如何通过ansible使用githooks
EN

Stack Overflow用户
提问于 2022-05-23 15:30:03
回答 1查看 203关注 0票数 0

所有吉特钩都是Git在存储库中发生某些事件时执行的普通脚本。

当我用基于ansible的内置git模块更新我的存储库时,我可以使用这些客户端钩子吗?

下面的演示-Playbook在/tmp文件夹中创建一个存储库和一个克隆。post-merge-script将一行添加到文件中。ansible-模块忽略了这个脚本,但是shell命令git pull创建了新行。

代码语言:javascript
复制
$ 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模块调用钩子脚本:

代码语言:javascript
复制
$ 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。

EN

回答 1

Stack Overflow用户

发布于 2022-05-24 06:19:11

正如注释中提到的,没有发生pullmerge

如果希望该脚本每次在ansible克隆/签出存储库时运行,请尝试post-checkout钩子。

但是,由于您已经在自动化它,我建议使用ansible来触发脚本而不是钩子。

您可以在您的git_result模块上注册一个变量(例如git ),然后根据git_result.changed决定该做什么。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72351152

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档