How to set up a cron job to run app.py on the server via a GitHub workflow?

Viewed 297

I have a simple bot that reads certain emails and tweets them. Now I'm trying to automate it and for that I need to run a cron job on the server. Here's my folder structure:

┌ .github
│  └── workflows
│      └── main.yml
├ .env
├ .gitignore
├ app.py
├ post_tweet.py
└ read_email.py

My main file is pretty simple:

#app.py

from read_email import mail_connect


def main():
     mail_connect()


if __name__ == '__main__':
     main()

The entire project is hosted at Vercel (and integrated with GitHub). I'd like to run this app.py file and from what I've read, I need to create a workflow on GitHub to run it, but I'm having some difficulty (and a little uncertain if it will work).

#main.yml

on:
  schedule:
  - cron: "*/10 11-13 * * 1-5"

jobs:
  build:
    runs-on: ubuntu-latest
      - uses: actions/checkout@v2  #Do I need this?
      - name: Run a one-line script
        run: #What could I enter here to run app.py?

While I was writing this question, the following question also came to me: do I really need a server like Vercel to run this or is it possible to do everything just with GitHub?

1 Answers

To run your app.py python script with Github Actions and CRON, you'll need to use a workflow like this:

name: Python Script Workflow

on:
  schedule:
  - cron: "*/10 11-13 * * 1-5"

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository content
        uses: actions/checkout@v2 # Checkout the repository content to github runner.

      - name: Setup Python Version
        uses: actions/setup-python@v2
        with:
          python-version: 3.8 # Install the python version needed

      - name: Install Python dependencies
        uses: py-actions/py-dependency-install@v2
        with:
          path: "requirements.txt"

      - name: Execute Python script # Run the app.py
        run: python app.py

As you can see, you need to use the checkout action to access the files inside the github repo, then setup the python version, install the dependencies, and finally run the script.

Here is an example of a personnal repo with this workflow

And here is the related workflow run

Related