You can use “on” in your GitHub workflow file to schedule GitHub workflow actions. The schedule event allows you to trigger a workflow at a scheduled time. You can schedule a workflow to run at specific UTC times using POSIX cron syntax. Scheduled workflows run on the latest commit on the default or base branch. The shortest interval you can run scheduled workflows is once every 5 minutes.
Step 1: Create a personal access token on GitHub.
Step 2: Create a GitHub repository using REST API.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
## Declare some Variables GITHUB_PAT_TOKEN=' GITHUB_REPO_NAME=' GITHUB_USER_NAME=' ## Create a new GitHub repository curl \ --silent \ --header "Authorization: token $GITHUB_PAT_TOKEN" \ --data '{ "name": "'"$GITHUB_REPO_NAME"'", "description": "GitHub repo created through API", "auto_init": true, "private": true, "gitignore_template": "nanoc" }' \ https://api.github.com/user/repos |
Step 3: Create a workflow definition for GitHub actions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
## Create your workflow file cat << EOF > myworkflow.yaml name: GitHub Actions Demo on: schedule: # * is a special character in YAML so you have to quote this string - cron: '30 5,17 * * *' jobs: job1: name: job1 runs-on: ubuntu-latest steps: - name: step1 run: echo "hello world!" EOF ## Cron syntax has five fields separated by a space, and each field represents a unit of time. ## ┌───────────── minute (0 - 59) ## │ ┌───────────── hour (0 - 23) ## │ │ ┌───────────── day of the month (1 - 31) ## │ │ │ ┌───────────── month (1 - 12 or JAN-DEC) ## │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT) ## │ │ │ │ │ ## │ │ │ │ │ ## │ │ │ │ │ ## * * * * * |
Note: We have used “on” with “schedule” to make GitHub automatically trigger GitHub workflow actions at a particular time of the day.
Step 4: Upload the workflow file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## Create base64 version of workflow CONTENT=$(cat myworkflow.yaml | base64 -w 0) && cat << EOF > content.json { "message":"file myworkflow.yaml added", "content":"$CONTENT" } EOF ## Upload the workflow file in GitHub curl \ --silent \ --request PUT \ --header "Authorization: token $GITHUB_PAT_TOKEN" \ --data @content.json \ https://api.github.com/repos/$GITHUB_USER_NAME/$GITHUB_REPO_NAME/contents/.github/workflows/myworkflow.yaml |
Step 5: Clean up.
1 2 3 4 5 6 |
## Delete the GitHub repository curl \ --silent \ --request DELETE \ --header "Authorization: token $GITHUB_PAT_TOKEN" \ https://api.github.com/repos/$GITHUB_USER_NAME/$GITHUB_REPO_NAME |