Support watch with the git state
I would like to only run a build command if there were new changes in a git repo as in the example
my_repo:
git.latest:
- name: https://github.com/some/repo.git
- rev: develop
- target: /opt/foo
cmd.run:
- cwd: /opt/foo
- name: make install
- watch:
- git: my_repo
Right now, the git state does not support mod_watch, so the "watch" is converted into a "require" so make is run every time.
What would be required to add a mod_watch to git? I've been trying to look at examples of mod watch from the service state and cmd state but it's a little confusing what would be required.
How does it determine if a command has changes ? Is it related to the "changes" field or something else. The documentation for watcher function is not clear.
Reading into the code some more, I wonder if this is a correct interpretation of how things work. If I use "-watch: -git: my_repo" instead of calling git.latest directly, it calls git.mod_watch (if it exists). Within git.mod_watch I would then call git.latest and capture that result. I could then look at the result there, and if result['changes'] is empty, then set the result to False for the mod_watch, since nothing changed. Otherwise, set result to True to indicate that the git repo changed and therefor the watching state should also be run.
You are looking at this backwards, you want the mod_watch function in cmd, and the state you have would not call mod_watch in git anyway. Try this:
my_repo:
git.latest:
- name: https://github.com/some/repo.git
- rev: develop
- target: /opt/foo
cmd.wait:
- cwd: /opt/foo
- name: make install
- watch:
- git: my_repo
With cmd.wait the command will only be run if the git returns changes, so with that said, I don't think and more code is required here
Looks like cmd.wait / cmd.wait_script did the trick. Perhaps the "fix" could be an addition to the documentation with a better example of how to use the wait/wait_script states. Would an example on the cmd module or git module make more sense ?
On the cmd state there is this example
Run myscript:
cmd.run:
- name: /path/to/myscript
- cwd: /
- stateful: true
Run only if myscript changed something:
cmd.wait:
- name: echo hello
- cwd: /
- watch:
- cmd: Run myscript
Perhaps a better example might be showing watching the git state for a change ?
Another alternative might be some kind of simple patterns that appear in salt, all collected on a single page after the basic tutorials.
Yes, those are good ideas, we can always use more documentation!
I have a linux service that runs from code deployed via git. When the git repo updates on the server, I want the service to restart. This is done via an upstart script. It would be nice to have the watch in place...
manager-code:
git.latest:
- name: git.github.com/manager.git
- rev: develop
- target: /opt/manager
- identity: /root/.ssh/deploy_production.id_rsa
- require:
- pkg: git
manager:
service.running:
- enable: True
- full_restart: True
- watch:
- git: manager-code
Hi,
I have a similar need for the feature that @clintberry is asking. Any suggestions?