2

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?

5
  • It's says the file isn't these. Try listing the directory contents in a shell task to see what is there. Commented Jan 24, 2022 at 16:58
  • Hello jessehouwing, I am new to Github. I don't know how to list the directory by shell, but I tried to re-upload the image in question(see above). I think it is more clear that how the files are stored. Any thoughts? Commented Jan 24, 2022 at 17:18
  • Could it be the last row of Scheduler.yml? : run: python HomeShareMa/DailyScrapingData/DailyScrapingData.py Commented Jan 24, 2022 at 17:27
  • Try this command in a run step: stackoverflow.com/a/40825320/736079 replace .svn with .git. Commented Jan 24, 2022 at 17:58
  • I tried again and rephased .yml file as above. every steps run successfully but not triggering python script to write data into csv file. Any thoughts? Commented Jan 25, 2022 at 2:58

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Hello John Hanley, great point that I missed. however it still cannot trigger python script to run as expected.
@HomeShare - Edit your question with the first problem fixed. That way you are not debugging multiple problems at the same time.
Actually that is the same question. The setting didn't work properly in Github actions. I try to fix errors with suggestions from all of you. Will mark as answer if problem fixed. thanks
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.