Can GitHub actions directly edit files in a repository? #25234
-
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 |
Beta Was this translation helpful? Give feedback.
Replies: 10 comments 9 replies
-
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.
Let me know if you have more questions I can help with! |
Beta Was this translation helpful? Give feedback.
-
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! |
Beta Was this translation helpful? Give feedback.
-
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 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 |
Beta Was this translation helpful? Give feedback.
-
Looking at the logs it looks like the code fialed becuase there was noting to commit from the
Meaning that the folder |
Beta Was this translation helpful? Give feedback.
-
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 |
Beta Was this translation helpful? Give feedback.
-
Thank you all the Action is now working :slight_smile: Joe Just for reference here is the working file:
|
Beta Was this translation helpful? Give feedback.
-
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.
|
Beta Was this translation helpful? Give feedback.
-
@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 |
Beta Was this translation helpful? Give feedback.
-
Just wanted to say thank you, this worked great for me! |
Beta Was this translation helpful? Give feedback.
{{title}}
{{editor}}'s edit
{{editor}}'s edit
-
Hi @jpadfield I am trying to append file through python and run the workflow through self hosted runner. I get the following error with the below code. Could you please help me with it? Run git config user.name github-actions shell: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.EXE -command ". '{0}'"
git : The term 'git' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
Error: Process completed with exit code 1. |
Beta Was this translation helpful? Give feedback.
Looking at the logs it looks like the code fialed becuase there was noting to commit from the
Commit files
section.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 thephp build.php
creates the docs (I assume) inbuild/docs
?What happens if you run that command locally? Or add
ls -la ./build
or something like that to the workflow to debug.