如何使用 ansible 从我的 git 存储库中检索单个文件?

问题描述 投票:0回答:4

我试图从 bitbucket 分支检索单个文件,而不是使用 git 模块获取整个分支。获取整个分支需要 2 分钟以上,而我真的只需要那个文件。

git ansible bitbucket
4个回答
1
投票

Ansible 似乎不支持稀疏结账。要使用 Ansible 进行稀疏结帐,您可以使用我在here

找到的以下代码
---
- hosts: all
  
  vars:
    repo_url: ssh://[email protected]/user/repo.git
    base_dir: /var/www

    sparse_checkout:
      - path/to/archive.zip
  
  tasks:
    - name: prepare sparse git repository
      shell: |
        git init
        git config core.sparsecheckout true
        git remote add origin {{ repo_url }}
      args:
        chdir: "{{ base_dir }}"
        creates: "{{ base_dir }}/.git"

    - name: configure sparse-checkout
      copy:
        dest: "{{ base_dir }}/.git/info/sparse-checkout"
        content: '{{ sparse_checkout | join("\n") }}'

    - name: clone code
      shell: git pull origin master
      args:
        chdir: "{{ base_dir }}"
        creates: "{{ base_dir }}/{{ sparse_checkout | first }}"

您可以将文件夹调整为单个文件,就像我上面所做的那样。

如果您只想将所有代码放在任务文件中,可以使用 set_fact: "above" 之前的代码。下面的例子:

- name: set needed facts for ansible sparse checkout
  vars:
    git_user: '<git-token-name>'
    git_ansible_token: '<your-token>'
    ansible_repo_user: '<username>' #(this should probably already be configured in your main vars file, e.g. all.yml, or some group_vars, but if not, you can add it here)
  set_fact:
    # using gitlab as an example
    repo_url: 'https://{{ git_user }}:{{ git_ansible_token }}@gitlab.com/project/repo.git'
    base_dir: "~{{ ansible_repo_user }}/<your/path/to/desired/repo/folder>"
    # folders we want to reduce the repo to
    sparse_checkout:
      - path/to/archive.zip

您可能需要也可能不需要的一些额外代码是创建基本存储库目录,因为此签出预计它存在。因此,您可以将其放在准备稀疏结帐以首先创建它的顶部 Git 任务之上

- name: make parent repo dir for sparse checkout
  become: yes
  vars:
    ansible_become_user: '{{ ansible_repo_user }}'
  file:
    path: "{{ ansible_base_dir }}"
    state: directory
    mode: '0750'

因此,将所有这些整合在一起,包括添加成为所有任务,整个事情在您的任务文件中可能如下所示;

- name: set needed facts for ansible sparse checkout
  vars:
    git_user: '<git-token-name>'
    git_ansible_token: '<your-token>'
    ansible_repo_user: '<username>' #(this should probably already be configured in your main vars file, e.g. all.yml, or some group_vars, but if not, you can add it here)
  set_fact:
    repo_url: 'https://{{ git_user }}:{{ git_ansible_token }}@gitlab.com/project/repo.git'
    base_dir: "~{{ ansible_repo_user }}/<your/path/to/desired/repo/folder>"
    # folders we want to reduce the repo to
    sparse_checkout:
      - path/to/archive.zip

- name: make parent repo dir for sparse checkout
  become: yes
  vars:
    ansible_become_user: '{{ ansible_repo_user }}'
  file:
    path: "{{ ansible_base_dir }}"
    state: directory
    mode: '0750'

- name: prepare sparse git repository
  become: yes
  vars:
    ansible_become_user: '{{ ansible_repo_user }}'
  shell: |
    git init
    git config core.sparsecheckout true
    git remote add origin {{ repo_url }}
  args:
    chdir: "{{ base_dir }}"
    creates: "{{ base_dir }}/.git"

- name: configure sparse-checkout
  become: yes
  vars:
    ansible_become_user: '{{ ansible_repo_user }}'
  copy:
  dest: "{{ base_dir }}/.git/info/sparse-checkout"
    content: '{{ sparse_checkout | join("\n") }}'

- name: clone code
  become: yes
  vars:
    ansible_become_user: '{{ ansible_repo_user }}'
  shell: git pull origin master
  args:
    chdir: "{{ base_dir }}"
    creates: "{{ base_dir }}/{{ sparse_checkout | first }}"

如果对您来说仍然不够快,您可以研究另一项附加功能。我看到了一种方法,您可以在进行克隆时检索较短的历史记录,这样您就只引入最后的

n
提交。

注意,对于 Git 项目令牌,如果您使用 GitLab,则创建一个项目访问令牌,对我来说,我至少需要角色

developer
,并访问仓库的 RO。如果您使用 git 或其他 VCS,则需要进行相应调整。


0
投票

您可以尝试

git archive
从远程git存储库获取文件

git archive --remote=git://git.example.com/project.git HEAD:path/in/repo filename | tar -x

详细信息可以参考Vonc的回答 - 了解更多信息


0
投票

您可以使用稀疏结帐选项

git config core.sparseCheckout true
git remote add -f origin git:<<providde url>
echo "path/to/folder/*" > .git/info/sparse-checkout
git checkout <branchname>

0
投票

Ansible 的模块

git
可以做到
git archive
但问题是它 是从本地克隆完成的。如果没有本地克隆,它会首先克隆,这是您想要避免的,因此您无法使用模块
git

因此你必须使用

command
shell

- name: Get a file using git archive
  command: git archive --remote=https://bitbucket.com/user/project.git HEAD:path/in/repo filename -o /path/to/archive.zip
© www.soinside.com 2019 - 2024. All rights reserved.