Run a Python script on Github
Github provides a way for running scripts that exist in your repository, called Github Actions. It enables you create to create a workflow to build, package, publish, deploy, test and do other stuff with your project. Travis CI offers similiar functionality, but is not for free anymore, while Github still is.
Running Python is posible on Github. There are workflows for testing or publishing a Python package. You can also just run a Python script that generates something like a file and save that file in the repository, automatically.
1 Create a job
For running a job that will be picked up by the Github workflow, you’ll have to set up the following folder structure in the root of your repository.
- Create a folder with the name .github.
- Inside that folder, create a folder with the name workflows.
- Create a yaml file inside workflows with a name like run-python.yml.
<repo>/.github/workflows/run-python.yml
The YAML file is where you put the workflow syntax that instructs Github what to do.
2 Building the actual job
First, you should provide a name.
name: run-python
Second, you create a trigger for the job defined by on. A common trigger is a push to Github.
on:
push:
branches:
- main
Triggering the job on a schedule based on a cron pattern is also useful.
on:
schedule:
- cron: '0 0 * * *'
Then you configure the actual job you want to run. This example will start a build on the lastest version of Ubuntu.
It will checkout your code (actions/checkout@v2), install Python (actions/setup-python@v2), install the relevant Python packages and run your actual Python script. The actions, like actions/checkout@v2 are handy scripts that you can find in the Github marketplace.
Lastly it will commit and push (ad-m/[email protected]) the resulting changes to the main branch.
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout repo content
uses: actions/checkout@v2
- name: setup python
uses: actions/setup-python@v2
with:
python-version: '3.8.2'
- name: install python packages
run: |
python -m pip install --upgrade pip
pip install requests
- name: execute py script
run: python3 <the-name-of-your-python-script>
- name: commit files
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add -A
git commit -m "update data" -a
- name: push changes
uses: ad-m/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: main
Keep in mind that the yaml will not work when the indentation is not correct. The nesting style of a yaml is comparable to Python. The full run-python.yml should look something like this:
name: run-python
on:
schedule:
- cron: '0 0 * * *'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: checkout repo content
uses: actions/checkout@v2
- name: setup python
uses: actions/setup-python@v2
with:
python-version: '3.8.2'
- name: install python packages
run: |
python -m pip install --upgrade pip
pip install requests
- name: execute py script
run: python3 <the-name-of-your-python-script>
- name: commit files
run: |
git config --local user.email "[email protected]"
git config --local user.name "GitHub Action"
git add -A
git commit -m "update data" -a
- name: push changes
uses: ad-m/[email protected]
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
branch: main
3 See it run
Check out the newly created job under the Actions tab on the homepage of your repository. If you are using the on push trigger in the YAML, you’ll see that it starts to run right after you pushed the new workflow job to Github.