[Python] Creating and Pushing a file from Github Action

  1. My python script is running twice per day on Github Action.
  2. I would like to create new text file that would only contain “var=x” where x is calculated from the python scripts.
  3. I am trying to overwrite var=x of the text file every run pushing on the current repository.
  4. So my concerns are
    4.1. I have no idea how to create (or where store) new text file from my python scripts.
    4.2. How to push the text file from Github action.

I would appreciate any help !!!

@meenmo,

When a workflow is running, the default workspace of each job is GITHUB_WORKSPACE (github.workspace) on the runner machine. The default working directory of all the steps in the job also is this path.

To create and push a new file to the GitHub repository via a job in the workflow, generally you need to add the following steps mainly:

  1. Use the checkout action to check out the repository to the workspace on the runner machine.

    - name: Checkout
      uses: actions/checkout@v2
    
  2. Create a new text file with the specified text.

       - name: Create/update text file
         run: echo "var=x" > test.txt
        # If 'test.txt' is existing, overwrite its text with "var=x"
        # If 'test.txt' doesn't exist, automatically create it and write the text "var=x"
    
  3. Use git commands to commit and push the new file to the remote GitHub repository.

    - name: Commit and Push
      run: |
        git config --global user.name "github-actions[bot]"
        git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
       # use account 'github-actions[bot]' to set the git config
    
        git add -A
        git commit -m "commit message"
        git push
    

If you want to create/update the text file, and call the git commands in your Python scripts,

  • In Python, there is no need for importing external library to read and write files. Python provides an inbuilt function for creating, writing, and reading files.

  • To call git commands, you can use the subprocess module in your scripts.

1 Like

Thanks for your reply!!

I got one question. From the following line,

run: echo "var=x" > test.txt

How to put x in test.txt from my python scripts?!

Will this work on Github Action?

f = open("test.txt", "a")
f.write("blah blah")
f.close()

It would, though from your description it didn’t sound like you want “append” mode for open().

My preferred option would be to use the pathlib module:

p = pathlib.Path('test.txt')
p.write_text('var=x')

@airtower-luna,
Using the ‘Write’ mode (“w”) for the open() function can let new text overwrite the old text in the file. This should be what @meenmo is looking for.

@meenmo,
Here is a simple Python script example shows how to create a new text file, then write or update the text in the file.

Run the script in the workflow:

1 Like

@meenmo,

How are things going?
Have you tried as the example I shared in my previous reply? Is it helpful to you?
Please try it, and any update, feel free to tell me.

If my above suggestion can help you solve the problem you are facing, maybe you can mark it as the solution of this topic, so that other users who have the similar questions can notice the suggestion when they are looking for an answer. Thanks.

Thanks for your help!!

I tried that already, but still don’t know where that “test.txt” is saved(or created), and

how to push “test.txt” to the current repository.

I would greatly appreciate your help once again.

@meenmo,

By default, the created text file will be created and saved into the default working directory of all the steps in current job (see github.workspace or GITHUB_WORKSPACE).

If you want to save the created text file into a sub-directory under the GITHUB_WORKSPACE, you can create the sub-directory if it does not exist, then create the text file.

# Create directory if not exists
import os
new_dir = "my-test"
if os.path.isdir(new_dir) == False:
  try:
      os.makedirs(new_dir)
  except OSError:
      print ("Creation of the directory %s failed" % new_dir)
  else:
      print ("Successfully created the directory %s" % new_dir)

Here is a Python script example includes the complete steps to create the new text file and push it to remote GitHub repository.

And this is the workflow to execute the Python script.

@meenmo,

What is the progress on your side?
Did you try the latest Python script and the workflow I shared in my previous reply? Is it can solve your questions?
Please try it, and any progress, feel free to tell me.

This is my original yml file that runs four times every day.

name: XXX

on:
  schedule:
    - cron: "0 2,5,9 * * *"

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2
    - name: Set up Python 3.7
      uses: actions/setup-python@v2
      with:
        python-version: 3.7
    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
    - name: XXX

      run: |
        python main.py

And the following is new yml code I merged with yours.

name: XXX

on:
  schedule:
    - cron: "0 2,5,9 * * *"

  push:
    paths-ignore: 
      - '.github/**'

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Set up Python 3.7
      uses: actions/setup-python@v2
      with:
        python-version: 3.7

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

    - name: XXX Notification
      run: |
        python main.py


      - name: List files
        run: |
          ls -R

Will this allow to push the text file?

I would appreciate if you could provide a bit more explanation on the last block of code.

      - name: List files
        run: |
          ls -R

@meenmo ,

Will this allow to push the text file?

Sure, it is allowed.
I had add the lines in my Python script “create-update-file.py” to execute the git commands to commit and push the text file (see here).

print("\n======================= Push changes to remote =======================")
import subprocess
print("Set git config...")
subprocess.Popen(["git", "config", "--global", "user.name", "github-actions[bot]"])
subprocess.Popen(["git", "config", "--global", "user.email", "41898282+github-actions[bot]@users.noreply.github.com"])

print("Push to remote...")
subprocess.Popen(["git", "add", "-A"])
subprocess.Popen(["git", "commit", "-m", "comment message"])
subprocess.Popen(["git", "push"])

Have you copied and added the related code from my Python script “create-update-file.py” into your “main.py”?
The Python script in my “create-update-file.py” has contained all the steps you require, include creating/updating the text file with the new value of “var=x”, and using git commands to push the modified text file to the remote GitHub repository.
You just need to copy and add the related code into your “main.py”, then run your “main.py” in the workflow.

- name: XXX
  run: python main.py


I would appreciate if you could provide a bit more explanation on the last block of code.

- name: List files
  run: |
    ls -R
    cat my-test/test.txt

The command “ls -R” will list all the files in current working directory include the files in subdirectories. The command “cat my-test/test.txt” will print the content of the text file (my-test/test.txt) in the logs.
I just use this step to check and confirm that the text file has been created/updated correctly in the specified directory under the workspace (GITHUB_WORKSPACE). You can remove it, if you do not need it.

@meenmo ,

How are things going? Is everything OK now in your project?
Please try with my suggestions, and if you have any progress, feel free to tell me.

If my above suggestion can help you solve the problem you are facing, you can mark it as the solution of this topic, so that other users who have the similar questions can notice the suggestion when they are looking for an answer.