Reputation: 555
I did scheduled job but it can work several times even if there is no changes(in source ) no commit after it done no mr. How to check if it worked once end ifthere is no changes don't start it again?
Upvotes: 0
Views: 1015
Reputation: 41051
One way you might do this is to create/check a cache:
file to determine if the scheduled pipeline has already run.
If the file does not exist, it means that the schedule job has not run for this commit SHA. If the file does exist (is restored from cache), then we know the schedule already ran before on this commit. Here we use a key:
of $CI_COMMIT_SHA
so that the cache will be invalidated only when there are changes to the repo.
myjob:
rules:
- if: '$CI_PIPELINE_SOURCE == "schedule"'
cache:
key: $CI_COMMIT_SHA
paths:
- .schedule_cache
script:
- | # check if the job already ran for this commit
if [[ -f .schedule_cache ]]; then
echo "already ran for this commit, nothing to do"
exit 0
fi
- ... # do the job
- echo "done" > .schedule_cache # create the cache file
If you need this job to run both for branches and for schedules, you can change the key to key: $CI_PIPELINE_SOURCE-$CI_COMMIT_SHA
so that the key will additionally be separate for each pipeline source.
Upvotes: 2