Skip to content

Can GitHub actions directly edit files in a repository? #25234

Can GitHub actions directly edit files in a repository? #25234
Mar 18, 2020 · 9 answers

Hi,

“Can GitHub Actions run a PHP file that can read, delete and create files directly within a repository?”

I have a simple repository that builds a series of html pages, based on a few json files. The build process is carried out by a PHP file. The idea is to simplify the process of making a set of consistent GitHub web-pages for a given repository.

I have been running the PHP file on a local machine in a cloned copy of the repository: it removes the old html files in the docs folder, reads in the json files and then creates a new set of html pages into the local docs folder, based on the content of the json files.

The local copy of the repository can then be pushed back to GitHub and the GitHub pages for the project are updated.

What I was wondering is, can this process simply be run as an “action” within GitHub? Basically the plan would be if one of three named json files are updated, the action could run and automatically rebuild the html files.

I can see lots of example actions that run files but I can not see any that actually directly interact with the existing repository files, is it possible? I have seen some talking about automating push processes but I could not see how it could include the files created by the php file that is run within the action.

I would also be very interested if it was possible to call the php file from a different repository but still run it locally within your own repository, but this is less important. :slight_smile: (This would allow a single build process to be used across a small set of different repositories)

I am hoping that this, if possible should be reasonably simple.

Any suggestions most welcome.

Thanks

Joe

Looking at the logs it looks like the code fialed becuase there was noting to commit from the Commit files section.

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
##[error]Process completed with exit code 1.

Meaning that the folder ./docs doesn’t seem to exist at that location. I’m not familiar with PHP at all but could it be that the php build.php creates the docs (I assume) in build/docs?
What happens if you run that command locally? Or add ls -la ./build or something like that to the workflow to debug.

Replies

9 suggested answers

Hey @jpadfield, you can most definitely have an action that edits internal files (except those in the “.github” folder. 

I do the following in one of my repo’s which calls a python script that parses all my source code and updates a todo.txt file and then commits it to the repo. 

jobs:
  build:
runs-on: macos-latest

steps:
- uses: actions/checkout@v1
- name: Run a one-line script
  run: python ./.github/workflows/todo_List.py
- name: switching from HTTPS to SSH
  run: git remote set-url origin ${{ secrets.ssh }}
- name: check for changes
  run: git status
- name: stage changed files
  run: git add .
- name: commit changed files
  run: git commit -m "Auto updating TODO.txt"
- name: fetch from master
  run: git fetch origin master
- name: push code to master
  run: git push origin HEAD:master

Let me know if you have more questions I can help with! 

0 replies

Hi @jpadfield ,

Yes, github actions can achieve this. 

Step1: You need the get the source code with actions/checkout, if the code exists on other repo, you need to add parameters ‘repository:’ and ‘token’, please check the usage accordingly.

Step2: You need to rebuild the html files.

Step3. You can push the code to repo with actions ‘ad-m/github-push-action’, if you want to push to another repo, please add ‘repository’ and ‘github_token’ parameter for the action. 

Here is my answer in a similar ticket which push back the code to repository, hope it helps!

0 replies

Hi @weide-zhou & @logankilpatrick 

THank you for your responses it is great that this process should be possible. Looking at the links/examples you have suplied I have created an Action which I think should do the job locally:

https://github.com/jpadfield/simple-site/blob/master/.github/workflows/build.yml

But it seems to be failing with a very unhelpfull error:

.github#L1
Process completed with exit code 1.

Am I missing something simple?

Another related quesation, is it possible to limit the action to only run after certain files, or perhaps a certain folder is changed? Not a big issue, but it would stop the action running when it is not needed.

Thanks

Joe

0 replies

Looking at the logs it looks like the code fialed becuase there was noting to commit from the Commit files section.

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
##[error]Process completed with exit code 1.

Meaning that the folder ./docs doesn’t seem to exist at that location. I’m not familiar with PHP at all but could it be that the php build.php creates the docs (I assume) in build/docs?
What happens if you run that command locally? Or add ls -la ./build or something like that to the workflow to debug.

0 replies
Answer selected

Thank you :slight_smile:

So basically it was working, but because no updates where required it returned an error.

Ok no to find an if the files are different then commit them.

Joe

0 replies

Thank you all the Action is now working :slight_smile:

Joe

Just for reference here is the working file:

# This is a basic workflow to help you get started with Actions

name: Build GH Pages

Controls when the action will run. Triggers the workflow on push or pull request

events but only for the master branch

on:
push:
branches: ['master']
paths: 'build/*.json'

A workflow run is made up of one or more jobs that can run sequentially or in parallel

jobs:

This workflow contains a single job called "build"

build:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2

# Runs commands using the runners shell
- name: Run the build php script
  run: |
    cd build
    php build.php # This file reads a few json files and then creates a set of html files in the doc folder
    cd ../
- name: Commit files # transfer the new html files back into the repository
  run: |
    git config --local user.name "jpadfield"
    git add ./docs
    git commit -m "Updating the repository GitHub html pages in the docs folder"
- name: Push changes # push the output folder to your repo
  uses: ad-m/github-push-action@master
  with:
    github_token: ${{ secrets.GITHUB_TOKEN }}
    force: true

0 replies

So basically it was working, but because no updates where required it returned an error.
Indeed 

Great, happy to help! 

For future reference: by clicking on the action (in the left menu) you could see more of the logs than e.g. the following.

.github#L1
Process completed with exit code 1.
0 replies

@vidavidorra yes I had missed that one, so was working a bit blind, but once you mentioned it I explored some more and found it, so thanks.

Joe

0 replies

Just wanted to say thank you, this worked great for me!

0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants