git - 如何使用 Ansible 将代码从本地 git 存储库部署到远程服务器

标签 git ansible

我正在使用 Ansible 2.5。我需要将代码从本地( Controller )git 存储库部署到远程服务器。

我正在尝试使用带有 git 模块的 Ansible-playbook,它只能将代码从本地存储库部署到另一个本地路径或远程存储库到另一个远程路径。它基于主机配置。

- git:
    repo: /home/pi/Desktop/kk/Vue-Example/
    dest: /home/pi/Desktop/bb

这里repo将是本地( Controller 机器)git存储库路径
dest将是远程机器位置。

最佳答案

这正是我想要的工作流程——从我知道我可以依赖的本地 git 存储库中提取文件。在我的例子中,我使用特定的提交 ID(经过充分测试的版本)而不是分支名称。如果你想要这个,只需将下面的“master”替换为提交 ID。

- tasks:
    - name: Make temp directory
      tempfile:
        state: directory
      register: temp_git_archive
      delegate_to: localhost
      become: no
      changed_when: False
    - name: Extract latest git commit on branch master
      shell: git archive master |tar --extract --directory={{ temp_git_archive.path }}
      args:
        chdir: /home/pi/Desktop/kk/Vue-Example/  # YOUR LOCAL GIT REPO
      delegate_to: localhost
      become: no
      changed_when: False
    - name: Copy to remote
      copy:
        src: "{{ temp_git_archive.path }}"
        dest: /home/pi/Desktop/bb  # YOUR DESTINATION DIRECTORY
    - name: Delete temp directory
      file:
        path: "{{ temp_git_archive.path }}"
        state: absent
      when: temp_git_archive.path is defined
      delegate_to: localhost
      become: no
      changed_when: False
可以使用 Ansible 的“git”和“unarchive”模块代替上面的“shell”模块,但我更喜欢一步完成。

关于git - 如何使用 Ansible 将代码从本地 git 存储库部署到远程服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55472314/

相关文章:

Git:删除单个远程修订

Ansible 使用 'with_items' 创建列表

ansible - Ansible 中的条件通知取决于角色

linux - 为什么 Ansible 没有在变量中看到属性?

git - 将本地 git 存储库与 Ansible Tower 结合使用

Ansible:如何从文件中导入 GPG 私钥?

git - 我可以看看 2 个提交补丁是否相同

git - 您如何与其他开发人员共享您的 git 存储库?

git - `git blame/annotate` : terminate on merge commits

git - 在 git 子模块中提交后触发 Jenkins 测试