Using SaltStack, how can I watch for *one* file change in a git repository?

时间:2016-02-03 03:11:01

标签: salt-stack

For example, I have a git.latest state like this:

foobar:
  git.latest:
    - branch: production
    ...

I want to define a cmd.run or cmd.wait state that runs if and only if one particular file in the foobar git repository has changed. I don't care if other files of the repo have changed.

How can I do that?

There doesn't seem to be anything immediately obvious in the current release of SaltStack (2015.8.4)

2 个答案:

答案 0 :(得分:1)

如果你正在使用Github(或类似的),你可以使用file.managed来观察文件的变化:

/tmp/egg:
  file.managed:
    - source: https://raw.githubusercontent.com/mafrosis/dotfiles/master/README.md
    - source_hash: sha1=bf5b231d1b3dc9bc3217896f9f5d1c903b0779dd
  cmd.wait:
    - name: touch /tmp/bacon
    - watch:
      - file: /tmp/egg

答案 1 :(得分:0)

您可以转储git将要更改为'changed_files'文件的文件,然后检查要检查的文件:

update_git:
  cmd.run:
    - name: git fetch && git --no-pager diff --no-color --name-only HEAD origin/{{ branch }} > /tmp/changed_files
    - cwd: {{ directory }}
  git.latest:
    - name: {{ git_url }}
    - branch: {{ git_branch }}
    - target: {{ directory }}

update_npm_packages:
  cmd.run:
    - name: npm ci --only=production --unsafe-perm
    - cwd: {{ directory }}
    - onlyif: test `/tmp/changed_files | grep package-lock.json | wc -l` != 0
    - require:
        - cmd: update_git