如何在进行ansible部署时获取git的提交ID

时间:2015-12-22 11:38:52

标签: git deployment ansible

我正在使用ansible来部署项目,而部署我确实采用git的最后一次提交ID,以便在出现任何问题时我应该能够恢复。
获取提交ID的过程很繁琐,需要登录到远程服务器并获取提交ID。

有什么方法可以从远程服务器获取最后一次提交ID,它应该登录到本地服务器。在ansible将最新代码部署到远程之前。

2 个答案:

答案 0 :(得分:2)

你可以

- name: get git version
  shell: git rev-parse HEAD
  register: git_version

- name: store it
  shell: echo "last_git_version: {{ git_version.stdout }}" > host_vars/{{ ansible_host }}/git_info.yml
  delegate_to: localhost

然后,当您部署代码时,您将拥有一个变量,其中包含您部署的最新last_git_version代码,您可以将其恢复为。

答案 1 :(得分:0)

我想更好的选择是:

  • 注册git module
  • 的输出
  • 并使用注册变量的.before

一个例子可能像这样:

- name: Clone git project
  git:
    repo: https://github.com/nextcloud/docker
    dest: /tmp/
  register: result_git_clone
- name: Print last commit ID
  debug:
    msg:
      - "{{ result_git_clone.before }}"

预期输出应类似于:

TASK [nextcloud : Print last commit ID] ***********************************************************************
ok: [puigreig] => {}

MSG:

['476e60305b5f51aba863f0552cbaf7c2d4e0dbf9']