【问题标题】:How to get the SHA of the checked out code with ansible git module?如何使用 ansible git 模块获取签出代码的 SHA?
【发布时间】:2016-03-16 09:42:49
【问题描述】:

我想使用 Ansible 存储当前签出的提交 SHA-1 哈希以用于代码版本。

我想set_fact这个版本用于其他角色。

【问题讨论】:

  • 你的意思是剧本所在的repo的git commit ID?
  • 对不起,我没有解释清楚。是的 git commit id 或 SHA-1 哈希,但是剧本使用 git 模块签出的单独 git 存储库

标签: git ansible ansible-playbook


【解决方案1】:

Ansible 中的git 模块会为您返回此信息,您只需将其注册到一个变量中(在下面的示例中变量为gitresult)。

- hosts: web
  tasks:
    - name: Checkout repo
      git:
        repo=https://github.com/michalgasek/www-discogs.git
        dest=/vagrant/checkout
        update=yes
        accept_hostkey=yes
      register: gitresult

    - debug: msg="SHA-1 before git update is {{ gitresult.before }}"
    - debug: msg="SHA-1 after git update is {{ gitresult.after }}"

跑步:

PLAY ***************************************************************************

TASK [setup] *******************************************************************
ok: [192.168.2.201]

TASK [Checkout repo] ***********************************************************
ok: [192.168.2.201]

TASK [debug] *******************************************************************
ok: [192.168.2.201] => {
    "msg": "SHA-1 before git update is 87544e2ea1c8dec30e5fc68302caa262b10affda"
}

TASK [debug] *******************************************************************
ok: [192.168.2.201] => {
    "msg": "SHA-1 after git update is 87544e2ea1c8dec30e5fc68302caa262b10affda"
}

希望它能解决你的问题。

【讨论】:

  • 有什么方法可以使用 git 模块在本地检查 SHA-1,而不依赖于访问远程仓库?这显然是可能的(它显示在一个简单的git log 命令中),但是 git 模块需要repo 参数,我的测试似乎证实如果它无法访问它就会中断。
  • 如果git 模块需要repo 参数,那么它将不起作用。但是您可以在上述用例中使用 command 模块:command: git log --pretty=format:"%H" -n 1command: git rev-parse HEAD 将给您 SHA-1,只需 register: gitresult 并且您的 SHA-1 可以在 {{ gitresult.std_out }} 中访问
  • 是的,这是一个很好的有效选择。通过更详细的测试,我还发现将cloneupdate都设置为false会阻止下载,这是官方文档中建议的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-23
  • 1970-01-01
相关资源
最近更新 更多