If it runs every commit via github actions… then maybe you can do something like this:
- name: List SQL files to be executed
shell:
cmd: git diff-tree HEAD~1 HEAD -r --name-only | grep DDL | grep \.sql$
register: files_matched
failed_when: false
- name: Connect to SQL instance
ignore_errors: true
loop: “{{ files_matched.stdout_lines }}”
community.postgresql.postgresql_script:
db: “{{ db_name }}”
login_host: “{{ ansible_host }}”
login_password: “{{ db_password }}”
login_user: “{{ db_user }}”
port: “5432”
path: “{{ item }}”
encoding: UTF-8
when: files_matched.stdout_lines | length > 0
The idea is to get only the changed filenames between the two most recent commits, and filter them just like before. You may need to specify the working directory or otherwise adjust the shell command a bit. I don’t have enough experience with GitHub Actions to know if there’s any gotchas to worry about or if there’s any “magic” that could be leveraged from the GitHub runners to make this better.