Let me write a demo for you (The below is a DevOps YAML pipeline, based on Azure Git Repository).
trigger:
branches:
include:
- main
paths:
include:
- monitor/monitored_file.txt
pool:
vmImage: ubuntu-latest
steps:
- checkout: self
persistCredentials: true #This will generate auth information based on auth identity of your pipeline.
- task: PythonScript@0
inputs:
scriptSource: 'inline'
script: |
import os
import random
import string
#add something to Txt_Files/random_name.txt, this is just a demo, it will random a string(length 10, from a to z) and add it to the target file, you can design your own code
def add_something_to_txt(txt_file_path, something_to_add):
txt_file = open(txt_file_path, 'a')
txt_file.write('\n'+something_to_add)
txt_file_path = "Txt_Files/random_name.txt"
something_to_add = ''.join(random.choice(string.ascii_lowercase) for i in range(10))
add_something_to_txt(txt_file_path, something_to_add)
- task: PowerShell@2
inputs:
targetType: 'inline'
script: |
git config --global user.email "xxx@xxx.com"
git config --global user.name "xxx"
git add .
git commit -m 1
git show-ref
git push origin HEAD:main
And this is my repository's structure:

You need to make sure these settings:


The above steps can achieve one goal that when something changed in 'monitor/monitored_file.txt' in the main branch of your repository, it will trigger python script to add a random string(length 10, from a to z) to the file 'Txt_Files/random_name.txt' in your repository.