Git command to extract files with specific date range and path

Viewed 112

I am building a devops pipeline that will extract OpenAPI ymal files from a GIT repo and then fire some RestAPI requests to another system (API management product) to publish the API using the ymal files.

The idea is to create a Jenkins job and scan the Git repo every 10 minutes, searching for particular path (e.g. /myproject/openapi/) and file extension (*.ymal). If it sees recent changes (within 10 minutes), it will extract those files and run a script to fire the APIs.

Could somebody help on Git command that can extract the ymal files?

Note the Git repo may be very large and I wish to only download and extract those files with matching criteria.

Thank you.

1 Answers

To be able to go through the git's history you need to have your clone already available on the machine where Jenkins runs. Knowing that the job is going to be executed every 10 minutes you could use:

git clone --single-branch --shallow-since="10 minutes ago"

to clone only required subset of commits.

Having relevant commit already cloned you could use command similar to that one to get information about files changed in last 10 minutes:

git log --since "10 minutes ago" --format="" --name-only -- myproject/openapi/**/*.yaml

Please bear in mind that this hardcoded 10 minute limit might not be a good idea. With that you're assuming that the job indeed will run every 10 minutes, which might not happen.

Better option would be to use bigger overlapping window or to store somewhere last successful execution time and look for changes that were introduced since that time.

Related