项目作者: varunsridharan

项目描述 :
~ Dynamic ReadME Generator ~
高级语言: PHP
项目地址: git://github.com/varunsridharan/action-dynamic-readme.git
创建时间: 2020-10-17T11:07:36Z
项目社区:https://github.com/varunsridharan/action-dynamic-readme

开源协议:MIT License

下载


Dynamic ReadMe - Github Action

Convert Static Readme into Dynamic Readme

Motivation

As an open-source software developer I use GitHub Repositories extensively to store my projects. I maintain over 100 projects, of which, about 85% of them have standardised content for the README.md file. That being said, I am finding it increasingly tedious to add, update or remove content in the README.md files across all my repositories because of two main challenges:

  1. Templating of files: The information which is common to README.md files across all my repositories such as Sponsor, Contribute, Contact, etc., cannot be templated and inserted into the README.md files of all my projects / repositories.

  2. Project / Repository-specific information: Github does not provide any repository-specific variables which can be used to dynamically insert repository information into the README.md file. As a result, repository-specific information needs to be hard-coded into the README file.

Solution:

To overcome this limitation, and help developers such as myself automate this tedious task, I have created a GitHub action called “Github Action Dynamic ReadMe”. This action pulls repository-specific variables and also allows for templating of files, thereby easily creating dynamic file content for files such as README.md.

⚙️ Configuration

Option Description Default
FILES list of files that should be compiled. false
DELIMITER you can change the default DELIMITER if it causes issue with your data. ${{ }}
GLOBAL_TEMPLATE_REPOSITORY you can set a global repository template where all the files are stored. false
committer_name Specify the committer name Dynamic Readme
committer_email Specify the committer email githubactionbot+dynamicreadme@gmail.com
commit_message set a custom commit message 💬 - File Rebuilt - Github Action Runner : ${GITHUB_RUN_NUMBER}
confirm_and_push Commit the changes and push directly to repository true

:writing_hand: Syntax

  • Variables : ${{ VARIABLE_NAME }}
  • Inline File Includes : <!-- include {filepath} -->
  • Reusable File Includes :
    • Start : <!-- START include {filepath} -->
    • END : <!-- END include {filepath} -->

Variables

All Default vairables exposed by github actions runner can be accessed like $‎{{ GITHUB_ACTIONS }} OR $‎{{ GITHUB_ACTOR }}

Dynamic Readme Github Action Uses Repository Meta - Github Action which
exposes useful metadata as environment variable and those variables can be used as template tags.

any variables exposed by Repository Meta can be accessed like below

  1. Repository Owner : ${{ env.REPOSITORY_OWNER }}
  2. Repository Full Name : ${{ env.REPOSITORY_FULL_NAME }}

:information_source: Note : Any environment variable can be accessed just by using env. as prefix ${{ env.VARIABLE_NAME }}

File Includes

Source Options

  • Relative Path : template/file.md
  • Absolute path : ./template/file.md
  • From Repository : {owner}/{repository}/{filepath} OR {owner}/{repository}@{branch}/{filepath}

Relative Path Syntax

Files are always searched from repository root path

  1. Inline Includes :
  2. <!-- include template/file.md -->
  3. Reusable Includes :
  4. <!-- START template/file.md -->
  5. <!-- END template/file.md -->

Absolute path Syntax

Files are searched from current repository. This can come in handy when writing nested includesType a message

  1. Inline Includes :
  2. <!-- include ./template/file.md -->
  3. Reusable Includes :
  4. <!-- START ./template/file.md -->
  5. <!-- END ./template/file.md -->

From Repository Syntax

You can include any type of file from any repository. If you want to include a file from a Private Repository, you have to provide Github Personal Access Token INSTEAD OF Github Token in the action’s workflow file.

:information_source: If branch is not specified then default branch will be cloned

Without Branch
  1. Inline Includes :
  2. <!-- include octocat/Spoon-Knife/README.md -->
  3. Reusable Includes :
  4. <!-- START octocat/Spoon-Knife/README.md -->
  5. <!-- END octocat/Spoon-Knife/README.md -->
Custom Branch
  1. Inline Includes :
  2. <!-- include octocat/Spoon-Knife/@master/README.md -->
  3. Reusable Includes :
  4. <!-- START octocat/Spoon-Knife/@master/README.md -->
  5. <!-- END octocat/Spoon-Knife/@master/README.md -->

Builtin Markdown Handlers

blow options can be used with inline / resuable includes.

  • code - [code] OR [code:{code_type}] - will print the contents of the file inside a codeblock
  • blockquote - [blockquote] - will print the contents of the file inside a blockquote
  • raw - [raw] OR [escape] - will print the actual contents without rendering it.
Example
  1. Inline Includes With Custom Markdown Below file will be included inside HTML blockquote element
  2. <!-- include [code] template/thankyou.md -->
  3. Inline Includes With Custom Markdown Below file will be included inside HTML blockquote element
  4. <!-- include [code:json] template/contents.json -->
  5. Reusable Includes Custom Markdown :
  6. <!-- START [code] template/file.md -->
  7. <!-- END [code] template/file.md -->
  8. Reusable Includes Custom Markdown :
  9. <!-- START [code:json] template/contents.json -->
  10. <!-- END [code:json] template/contents.json -->

:information_source: Inline includes can come in handy when you want to parse the data once and save it. It can also be used inside a nested include.

:information_source: Even though Reusable includes and Inline Includes do the same work, they can come in handy when you are generating a template and saving it in the same file. It preserves the include comment which will be parsed again when re-generating the template, and the contents of the include will be updated accordingly.

Pull Request Support

This action modifies the files, creates a commit and uploads them directly to the default branch of the repository.

If the default branch of the repository is protected by the rule Require a pull request before merging then you will need to set the option confirm_and_push: false so that the commit is not made and the changes are not uploaded to the default branch.

  1. - name: "💫 Dynamic Template Render"
  2. uses: varunsridharan/action-dynamic-readme@main
  3. with:
  4. GLOBAL_TEMPLATE_REPOSITORY: {repository-owner}/{repository-name}
  5. files: |
  6. FILE.md
  7. FILE2.md=output_filename.md
  8. folder1/file.md=folder2/output.md
  9. confirm_and_push: false # Important if use "Require a pull request before merging" rule
  10. env:
  11. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

In addition to the previous configuration, it is necessary to use other actions to be able to confirm the changes and generate a Pull Request towards the default branch

  1. - name: "📥 Fetching Repository Contents"
  2. uses: actions/checkout@main
  3. - name: "💫 Dynamic Template Render"
  4. uses: varunsridharan/action-dynamic-readme@main
  5. with:
  6. GLOBAL_TEMPLATE_REPOSITORY: {repository-owner}/{repository-name}
  7. files: |
  8. FILE.md
  9. FILE2.md=output_filename.md
  10. folder1/file.md=folder2/output.md
  11. confirm_and_push: false # Important if use "Require a pull request before merging" rule
  12. env:
  13. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  14. - name: Create pull request
  15. uses: peter-evans/create-pull-request@v3
  16. with:
  17. token: ${{ secrets.GITHUB_TOKEN }}

Thanks to this configuration, the necessary files can be dynamically generated by adding them to a different branch that then we can merge with the default branch, complying with the rule Require a pull request before merging.

Check out the example workflow (Pull Request Support) for a more advanced configuration


For live Demo Please Check Demo Repository


🚀 Example Workflow File

  1. name: Dynamic Template
  2. on:
  3. push:
  4. branches:
  5. - main
  6. workflow_dispatch:
  7. jobs:
  8. update_templates:
  9. name: "Update Templates"
  10. runs-on: ubuntu-latest
  11. steps:
  12. - name: "📥 Fetching Repository Contents"
  13. uses: actions/checkout@main
  14. - name: "💾 Github Repository Metadata"
  15. uses: varunsridharan/action-repository-meta@main
  16. env:
  17. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  18. - name: "💫 Dynamic Template Render"
  19. uses: varunsridharan/action-dynamic-readme@main
  20. with:
  21. GLOBAL_TEMPLATE_REPOSITORY: {repository-owner}/{repository-name}
  22. files: |
  23. FILE.md
  24. FILE2.md=output_filename.md
  25. folder1/file.md=folder2/output.md
  26. env:
  27. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

🚀 Example Workflow File (Pull Request Support)

  1. name: Dynamic Template
  2. on:
  3. push:
  4. branches:
  5. - main
  6. workflow_dispatch:
  7. jobs:
  8. update_templates:
  9. name: "Update Templates"
  10. runs-on: ubuntu-latest
  11. steps:
  12. - name: "📥 Fetching Repository Contents"
  13. uses: actions/checkout@main
  14. - name: "💾 Github Repository Metadata"
  15. uses: varunsridharan/action-repository-meta@main
  16. env:
  17. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  18. - name: "💫 Dynamic Template Render"
  19. uses: varunsridharan/action-dynamic-readme@main
  20. with:
  21. GLOBAL_TEMPLATE_REPOSITORY: {repository-owner}/{repository-name}
  22. files: |
  23. FILE.md
  24. FILE2.md=output_filename.md
  25. folder1/file.md=folder2/output.md
  26. committer_name: github-actions[bot]
  27. committer_email: github-actions[bot]@users.noreply.github.com
  28. commit_message: 'docs: update readme file [skip ci]'
  29. confirm_and_push: false
  30. env:
  31. GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
  32. - name: Create pull request
  33. id: cpr
  34. uses: peter-evans/create-pull-request@v3
  35. with:
  36. token: ${{ secrets.GITHUB_TOKEN }}
  37. commit-message: 'docs: documentation files updated [skip ci]'
  38. committer: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  39. author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  40. signoff: false
  41. branch: github-actions/repository-maintenance
  42. delete-branch: true
  43. title: Update documentation files
  44. body: All documentation files has been updated to last recent version
  45. labels: |
  46. skip-changelog
  47. docs
  48. - name: Enable pull request automerge
  49. if: steps.cpr.outputs.pull-request-operation == 'created'
  50. uses: peter-evans/enable-pull-request-automerge@v1
  51. with:
  52. token: ${{ secrets.PAT }}
  53. pull-request-number: ${{ steps.cpr.outputs.pull-request-number }}
  54. - name: Auto approve
  55. if: steps.cpr.outputs.pull-request-operation == 'created'
  56. uses: juliangruber/approve-pull-request-action@v1
  57. with:
  58. github-token: ${{ secrets.PAT }}
  59. number: ${{ steps.cpr.outputs.pull-request-number }}

📝 Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog,
and this project adheres to Semantic Versioning.

Checkout CHANGELOG.md

🤝 Contributing

If you would like to help, please take a look at the list of issues.

📜 License & Conduct

📣 Feedback

  • ⭐ This repository if this project helped you! :wink:
  • Create An 🔧 Issue if you need help / found a bug

💰 Sponsor

I fell in love with open-source in 2013 and there has been no looking back since! You can read more about me here.
If you, or your company, use any of my projects or like what I’m doing, kindly consider backing me. I’m in this for the long run.

  • ☕ How about we get to know each other over coffee? Buy me a cup for just $9.99
  • ☕️☕️ How about buying me just 2 cups of coffee each month? You can do that for as little as $9.99
  • 🔰 We love bettering open-source projects. Support 1-hour of open-source maintenance for $24.99 one-time?
  • 🚀 Love open-source tools? Me too! How about supporting one hour of open-source development for just $49.99 one-time ?

Connect & Say 👋



Built With ♥ By Varun Sridharan