GitHub Repository Scheduled Update Using GitHub Actions

Introduction

GitHub Actions is a continuous integration and continuous delivery (CI/CD) platform that allows you to automate your build, test, and deployment pipeline. There could be some other cool things that can be automated by GitHub Actions, such as closing stale GitHub issues, tagging GitHub merge requests, or even crypto mining (strongly discouraged)!

In this blog post, I would like to quickly show how to use GitHub scheduled actions to update the repository itself routinely.

What’s the Date Today?

In this example, we created a GitHub repository “What’s the date today?” whose README contains the date today that will be automatically updated everyday using GitHub Actions.

More specifically, what we have to do are as follows.

  • Create an empty repository.
  • Turn on the repository write permission for GitHub Actions.
Workflow Write Permission
  • Create the following yaml file and placed it in the .github/workflows directory.
update-date.yml
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
27
28
29
30
31
32
33
34
35
name: GitHub Self-Updating Repository Demo

on:
# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#schedule
schedule:
# The shortest interval you can run scheduled workflows is once every 5 minutes.
# Note: The schedule event can be delayed during periods of high loads of GitHub Actions workflow runs.
# High load times include the start of every hour.
# To decrease the chance of delay, schedule your workflow to run at a different time of the hour.
# Every 5 minutes.
# - cron: '*/5 * * * *'
# At the beginning of every day.
- cron: "0 0 * * *"

# on: [push]

jobs:
report:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
# https://github.com/actions/checkout/tree/v3.0.2
uses: actions/checkout@v3
- name: Modify date and time
run: |
date > README.md
cat README.md
- name: Push to repository
run: |
git config --global user.name "Lei Mao"
git config --global user.email "leimao@users.noreply.github.com"
now=$(date)
git add -A
git commit -m "Auto Push on $now"
git push

References

GitHub Repository Scheduled Update Using GitHub Actions

https://leimao.github.io/blog/GitHub-Repo-Scheduled-Update-GitHub-Actions/

Author

Lei Mao

Posted on

05-11-2022

Updated on

05-11-2022

Licensed under


Comments