如何通过Ansible使用Githooks

发布于 2025-01-31 22:25:37 字数 1823 浏览 2 评论 0原文

所有 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 技术交流群。

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

发布评论

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

评论(1

倚栏听风 2025-02-07 22:25:37

如注释中所述,没有拉动合并发生。
如果您希望该脚本在每次Ansible克隆/检查存储库时运行邮政检查 挂钩。

但是,由于您已经在自动化它,我建议使用Ansible触发脚本而不是钩子。
您可以在git模块上注册变量(例如git_result),然后根据git_result.changed来决定该操作。

As mentioned in the comment, there is no pull or merge 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 your git module and then decide what to do based on git_result.changed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文