Ansible/git代码部署,如何获得代码?
(已解决)我使用的Ansible部署所做的一切都可以正常运行,直到我将Git Bare Repo拿到远程服务器的地步 - 这有效,但随后我被卡住了。
我知道您可以使用后接收器的挂钩发出将站点代码复制到服务器上正确位置的命令,因为我之前已经完成了该命令,但这并没有与Ansible自动化。
git --work-tree={{ target_dir }} --git-dir={{ git_bare_dir }} checkout -f
问题在于,使用Ansible创建裸露的存储库意味着没有钩子,因此不会复制原始代码。
这意味着特定于网站的作曲家的东西无法运行,因为它没有到位。
我尝试在git克隆之后添加一项明智的任务:
- name: Checkout the source code from the bare repo.
command:
cmd: "git --work-tree={{ target_path }} --git-dir={{ git_bare_dir }} checkout -f"
chdir: "{{ git_bare_dir }}"
但是它不起作用 - 有一个错误消息说这不是git repo。我尝试了不同的方法。从服务器上的命令行(在右目录中)运行它会产生相同的错误。
如果是相关的(可能不是),我将部署到数字海洋液滴中。
解决方案 :(我在这里添加了解决方案,因为我还不能“自我求助”。)
不确定这是最好的选择
- name: Copy a bare repo from Git to the remote server.
git:
repo: "{{ git_repo_url }}" # Use ssh: url, so we're not asked for name/password
version: "{{ git_repo_version }}"
dest: "{{ target_path }}"
separate_git_dir: "{{ git_bare_dir }}"
force: yes
single_branch: yes
depth: 1
accept_hostkey: yes
key_file: "{{ github_deploy_key }}"
我 同样,这不是裸露的回购。当报告本身处于git_bare_repo
时,该代码被复制到dest
(它在服务器的可访问部分之外)。
(SOLVED) Everything I'm doing with my ansible deployment is functional up to the point where I fetch the Git bare repo to the remote server - this works but then I'm stuck.
I know you can use a post-receive hook to issue a command that copies the site code to the right place on the server, because I've done it before, but that wasn't automated with ansible.
git --work-tree={{ target_dir }} --git-dir={{ git_bare_dir }} checkout -f
The problem is that creating the bare repo using ansible means there's no hook so the raw code doesn't get copied.
Which means the site-specific composer stuff can't run because it's not in place.
I have tried adding an ansible task after the git clone:
- name: Checkout the source code from the bare repo.
command:
cmd: "git --work-tree={{ target_path }} --git-dir={{ git_bare_dir }} checkout -f"
chdir: "{{ git_bare_dir }}"
But it doesn't work - there's an error message that says it's not a git repo. I've tried different ways of doing this. Running it from the command line (in the right directory) on the server gives the same error.
If it's relevant (probably not) I'm deploying to a Digital Ocean droplet.
SOLUTION: (I'm adding the solution here because I can't "self-answer" yet.)
I'm not sure this is the best option but I used this:
- name: Copy a bare repo from Git to the remote server.
git:
repo: "{{ git_repo_url }}" # Use ssh: url, so we're not asked for name/password
version: "{{ git_repo_version }}"
dest: "{{ target_path }}"
separate_git_dir: "{{ git_bare_dir }}"
force: yes
single_branch: yes
depth: 1
accept_hostkey: yes
key_file: "{{ github_deploy_key }}"
Although the ansible names are the same, it's not a bare repo. The code gets copied to dest
while the report itself is at git_bare_repo
(which is way outside the accessible part of the server).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
另一种方法是:
复制
模块) ahook> hook> hooks
从您的本地ansible服务器到目标裸露的repo文件夹,其中包括相关钩子预先填充。An alternative approach is to:
copy
module) ahooks
folder from your local Ansible server to the target bare repo folder, which would include the relevant hooks pre-populated.