Home Share
Home Share

Reputation: 49

Why Github Actions run successfully but Python script didn't execute as expected

I am trying to use Github Action to test a daily running python script. Here below have the very simple file directory: enter image description here

DailyScrapingData.py: (the code below can be run successfully in local machine)

from yahoo_fin import stock_info as si
from datetime import datetime
content = datetime.now().strftime("%Y%m%d") + ", " + str(si.get_live_price("^DJI")) + ", " + str(si.get_live_price("^DWCF"))
print(content, file = open('DailyScrapingData.csv', 'a+'))

.github/workflows/scheduler.yml:

name: DailyScrapingData

on:
  schedule:
- cron: '0 1 * * 1-5' 

jobs:
  pull_data:
runs-on: ubuntu-latest
steps:

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

  - name: setup python
    uses: actions/setup-python@v2
    with:
      python-version: '3.8' # install the python version needed
      
  - name: install python packages
    run: |
      python -m pip install --upgrade pip
      pip install -r requirements.txt
      
  - name: execute py script 
    run: python3 DailyScrapingData.py

enter image description here
There is nothing when I check DailyScrapingData.csv after running all steps of Github actions. Supposingly after running the python script should have to write some data into the csv. But nothing happen.

Any thoughts?

Upvotes: 2

Views: 2064

Answers (2)

Paul Wong
Paul Wong

Reputation: 1

The workflow file is missing a step to commit and push your changes; this is the reason why the edited csv file is not saved to your repository.

Briefly elaborating on what you have done so far:

  • runs-on: ubuntu-latest : Your GitHub runner, a temporary VM; all that happens in here stays in here unless there are explicit commands to export it. It is isolated unlike your local machine
  • actions/checkout@v2 : git checkout loads your repository's files (main branch by default) into the GitHub runner
  • defined steps to install python, some libraries, and run your py script

In layman's terms, this is akin to opening your csv file in Microsoft Excel, writing your Yahoo Finance data, and then closing Excel without saving.

You'll need to add this step to your workflow:

- name: Commit and push
  run: |
    git config user.name "${GITHUB_ACTOR}"
    git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
    git add DailyScrapingData.csv  # or git add -A to add all files
    git commit -m "Updated DailyScrapingData.csv"  # change this message if you want
    git push "https://${GITHUB_ACTOR}:${TOKEN}@github.com/${GITHUB_REPOSITORY}.git" HEAD:main || exit 0

Note that this will throw an error and cause the job to fail if there is nothing to commit, aka no file changes detected.

Upvotes: 0

John Hanley
John Hanley

Reputation: 81386

You are using a single line run step with multiple lines.

Change this step:

  - name: install python packages
    run: 
      python -m pip install --upgrade pip
      pip install -r requirements.txt

To:

  - name: install python packages
    run: |
      python -m pip install --upgrade pip
      pip install -r requirements.txt

Upvotes: 1

Related Questions