0

is there a way to trigger a python script to run when a specific file in the remote repo is updated? Also, the python script will update another file in the repo. I am using Azure for my project

I am thinking of using pipeline by creating a task to check if the file was updated using powershell then based on that the next task will excute the python script and the one after will push the code made by the python script. However, I am not well_informed in powershell scripiting

layth
  • 1
  • 1

1 Answers1

0

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:

enter image description here

You need to make sure these settings:

enter image description here

enter image description here

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.

Bowman Zhu-MSFT
  • 4,776
  • 1
  • 9
  • 10
  • @layth Hi layth, if my answer help you, could you please [mark it as the answer](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235)? This will help others who meet the similar issue. :) – Bowman Zhu-MSFT Nov 23 '22 at 01:45