0

how can I make a job in github action run randomly between 1 and 50 times a day?

Here is my cron job.

cron: '0 0 * * *'

This will run once a day.

But what I want is to run randomly 1-50 times a day.

How do I make it work randomly from 1 to 50?

below is my git action's yml setting file as workflows

#1. Repository Fork
# 2. Modify the files A and B according to the procedure
# 3. After committing the modifications, push & Enjoy!

name: planting-grass

# A. Comment lines 8-11
# on:
# push:
# branches:
# - unknown

# B. Uncomment lines 14-16
on:
   schedule:
     - cron: '0 0 * * *'

jobs:
  task:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set current date
        id: date
        run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
      - name: Execute commands
        run: bash ./task.sh ${{ steps.date.outputs.date }}
      - name: Commit files
        run: |
          git config --global user.name "$(git --no-pager log --format=format:'%an' -n 1)"
          git config --global user.email "$(git --no-pager log --format=format:'%ae' -n 1)"
          git add date.txt
          git commit -m ${{ steps.date.outputs.date }}
      - name: Push changes
        uses: ad-m/github-push-action@master
        with:
          github_token: ${{ secrets.GITHUB_TOKEN }}
          branch: ${{ github.ref }}

Cron jobs and random times, within given hours The method through this post does not work.

enter image description here

Best Regards!

torek
  • 448,244
  • 59
  • 642
  • 775

1 Answers1

1

You can do this by modifying your Bash script to loop a random number of times. For example, this Bash script loops between 1 and 50 times.

#!/usr/bin/env bash
loops=$(( ( RANDOM % 50 )  + 1 ))
echo "$loops"
for i in $(seq 1 $loops); do
    echo foo
done

The one downside of this approach is that you need to take the steps being done in the 'Commit files' step and merge them into the script in the 'Execute commands' step, or they won't be repeated.

Nick ODell
  • 15,465
  • 3
  • 32
  • 66
  • In the git action environment, cannot run the yml environment inside the for statement. – emarwa.mouma.35t Nov 20 '21 at 06:14
  • 1
    True, but all of the things you're doing inside of the "Commit files" step are things you can do in Bash. Configuring Git? You can do that in Bash. Committing files? You can do that in Bash. Getting the date? You can do that in Bash. – Nick ODell Nov 20 '21 at 06:18
  • result of this solution : `./task.sh: 4: i: not found` – emarwa.mouma.35t Nov 20 '21 at 07:11
  • @emarwa.mouma.35t If you truly don't care about the intervals, this approach is best IMO. Once per day, the script/code runs between 1 and 50 times in a row. You can implement it as a wrapper script that calls `./task.sh` (instead of `echo foo`), or, add the loop to `task.sh`. I would use `for ((i=1; i<=loops; i++))` instead of `seq`, but it doesn't matter. – dan Nov 20 '21 at 07:49
  • @emarwa.mouma.35t like the error in your [previous question](https://stackoverflow.com/questions/69973005/how-can-i-randomly-run-for-loop-in-sh-of-git-action-normally), and my answer, you need to specify bash. Running `while (( i <= loops ))` (or similar) in `sh` will produce that error. – dan Nov 20 '21 at 08:00