Background
Other than writing code, I have been using github for storing and organizing my daily notes. Github allows simplicity and flexibility to work with markdown, further it has the ability to version the files and view them across multiple devices. But as the total number of the file grows navigation becomes a bottleneck. To tackle this issue I’ve setup CI to generate a TOC(table of content) in the README so that I can quickly access a file from the README itself. I am going to setup a github actions workflow and make it generate the table of contents automatically for your project.
Workflow
Github Actions enables us to create workflows (like CI/CD capabilities) that can be integrated directly into our github code repository and runs on github hosted environments. These workflows are triggered automatically on occurrence of some event.
I have all the code and files committed in this demo github repository toc-demo. This is how the file structure for the example project looks like.
The github actions workflow is defined in a yaml file so we will create a file .github/workflows/action.yml
.
We will write a script that can generate the table of contents and then commit the changes and push! We will automate this workflow using the github actions.
Workflow file
|
|
Summarizing the action file:
- the job is triggered when changes are pushed on to the
main
branch. - the steps explained:
- the
actions/checkout@v2
checks-out the repositorytoc-workflow-demo
. - the
actions/setup-python@v2
setups python v3.8 in our actions environment. - the job runs a script
script.py
to generate TOC and write to a file - the end step is to commit the changes script made and push them to github.
- the
Script
To generate TOC we require the name and path of all files. This python script reads through all files in the directory and generates a table of content in a tree hierarchy format. You can modify this script to create what fits you best.
|
|
Conclusion
Once all the code changes are committed and code is pushed to github. Github actions will run the jobs and auto-commit the table of content to project README. The following table of content has been generated by the github actions in the demo project. We can click on the file name to navigate to the file.
Next time any new file is added, removed or modified and committed to github, github actions will take care of updating the TOC.