A GitHub Action to build dynamic markdown summaries for GitHub Actions and pull requests using Handlebars templates and custom data.
MarkdownBuilder helps you create custom markdown summaries for GitHub Actions workflows and pull requests using the power of Handlebars templating. With MarkdownBuilder, you can customize your templates with optional JSON data, posting the results directly to GitHub Action summaries or as comments on pull requests.
Inspired by GitHub Test Reporter
If you find this project useful, consider giving it a GitHub star ⭐
It means a lot to us.
Add MarkdownBuilder to your GitHub workflow by specifying the Handlebars template and optional JSON data file. MarkdownBuilder will generate and post the markdown summary as specified.
name: Generate Markdown Summary
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Generate Markdown Summary
uses: Ma11hewThomas/github-markdown-builder@v1
with:
template-file-path: './path/to/template.hbs'
json-file-path: './path/to/data.json' # Optional
summary: true # Set to true to post to GitHub Action summary
pull-request: false # Set to true to comment on PR if available| Name | Description | Required | Default |
|---|---|---|---|
template-file-path |
Path to the Handlebars template file to use. | true |
- |
json-file-path |
Path to the optional JSON file for template data. | false |
- |
summary |
Post markdown to GitHub Action summary | false |
true |
pull-request |
Comment markdown on Pull Request | false |
false |
| Name | Description |
|---|---|
summary |
The generated markdown summary. |
To post the markdown output to the GitHub Actions summary, set summary: true. This will add the generated markdown to the GitHub Actions summary tab, visible within the workflow logs.
To add a comment directly to an open pull request, set pull-request: true. Ensure your workflow is triggered by an event that provides a pull request context (e.g., pull_request or pull_request_target events).
You must set the GITHUB_TOKEN, which is typically available by default in
GitHub Actions, but it needs to have write permissions for pull requests. For
guidance on configuring these permissions, please see GitHub's
documentation.
- name: Comment On Pull Request
uses: Ma11hewThomas/[email protected]
with:
template-file-path: './path/to/template.hbs'
json-file-path: './path/to/data.json' # Optional
pull-request: true # Set to true to comment on PR if available
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}MarkdownBuilder uses Handlebars templates to let you customize the markdown output. You provide a Handlebars template file (with a .hbs extension) and, optionally, a JSON data file to define custom data that will populate your template.
In your template file, you can reference any data from the JSON file using the
{{data.propertyName}} syntax. For example, if your JSON file contains the
following:
{
"example": "Hello, World!",
"title": "Markdown Summary",
"items": ["Item 1", "Item 2", "Item 3"]
}You can access these values in your Handlebars template as follows:
In this example:
{{data.title}}will be replaced by "Markdown Summary".{{data.example}}will output "Hello, World!".- The
{{#each data.details}}block will iterate over each item in the items array, creating a bullet point list.
MarkdownBuilder provides easy access to various GitHub context properties, enabling you to incorporate GitHub-specific data directly into your Handlebars templates. This can be especially useful for generating summaries or pull request comments that reflect repository information, workflow details, or the current pull request status.
These properties are accessible under the github property in your templates.
The following GitHub properties are available for use in your Handlebars templates:
-
Action Context (accessed via
github)action: Name of the GitHub Action being executedactor: The GitHub username of the person or app that triggered the workfloweventName: The event that triggered the workflowsha: The commit SHA that triggered the workflowref: The branch or tag reference of the repositoryworkflow: The name of the workflowjob: The job ID in the workflowrunNumber: The run number of the workflowrunId: The unique ID for the workflow runapiUrl,serverUrl,graphqlUrl: URLs for GitHub API, server, and GraphQL endpointsbuildUrl: A direct URL to the summary view of the workflow run
-
Repository Context (accessed via
github.repository)cloneUrl: URL to clone the repositorycreatedAt: Repository creation timestampdefaultBranch: Default branch of the repositorydescription: Repository descriptionfullName: Full name of the repository (e.g.,owner/repo)htmlUrl: Web URL of the repositorylanguage: Main language used in the repositorylicenseName: Name of the license (if any)name: Repository nameopenIssuesCount: Count of open issues in the repositorysize: Size of the repository in KBstargazersCount: Number of stars the repository has receivedallowForking: Whether the repository allows forkingcompareUrl: URL to compare changes between the default branch and the latest commitcontributorsUrl: URL to view repository contributorsdeploymentsUrl: URL to view repository deploymentsdownloadsUrl: URL to view repository downloadseventsUrl: URL to view repository eventsforksUrl: URL to view repository forkssshUrl: SSH URL for cloning the repositorystargazersUrl: URL to view stargazers of the repositorystatusesUrl: URL to view commit statusessubscriptionUrl: URL to view repository subscription optionstagsUrl: URL to view repository tagsteamsUrl: URL to view repository teams
-
Pull Request Context (only available if the workflow is triggered by a pull request event; accessed via
github.pullRequest)additions: Number of additions in the pull requestassignee: GitHub username of the assigned reviewerassignees: List of all assigned reviewersauthorAssociation: Author’s association with the repositoryautoMerge: Whether auto-merge is enabledpushedAt: Timestamp of the last push to the pull requestbody: Body content of the pull requestchangedFiles: Number of changed files in the pull requestclosedAt: Timestamp of when the pull request was closedcomments: Number of comments on the pull requestcreatedAt: Timestamp of when the pull request was createddeletions: Number of deletions in the pull requestdiffUrl: URL to the diff of the pull requestdraft: Whether the pull request is a drafthtmlUrl: Web URL of the pull requestid: ID of the pull requestlabels: List of labels on the pull requestnumber: Pull request numberpatchUrl: URL to the patch of the pull requestrebaseable: Whether the pull request can be rebasedrequestedReviewers: List of requested reviewersrequestedTeams: List of requested teams for reviewreviewComments: Number of review comments on the pull requeststate: State of the pull request (e.g., open, closed, merged)title: Title of the pull request
-
Sender Context (details about the GitHub user who triggered the workflow; ; accessed via
github.sender)login: GitHub username of the senderid: ID of the sendernodeId: Node ID of the senderavatarUrl: Avatar URL of the sendergravatarId: Gravatar ID of the senderhtmlUrl: Web URL of the sender’s GitHub profiletype: Type of the sender (e.g., User, Bot)siteAdmin: Whether the sender is a GitHub site admin
You can use any of these properties in your Handlebars template by referencing
{{github.propertyName}}. For example:
If certain properties in the JSON file are missing, Handlebars will leave the corresponding placeholders empty. To prevent issues, you may want to use Handlebars helpers or default values in your template where applicable.
By providing flexible templating, MarkdownBuilder allows you to dynamically generate markdown content based on the data you supply, making it easy to adjust outputs to suit different workflows and scenarios.
MarkdownBuilder supports various Handlebars helpers, which enable more advanced functionality in your templates. Helpers allow you to perform operations like conditional checks, iterating over lists, and custom formatting. MarkdownBuilder supports built-in Handlebars helpers as well as a few custom helpers to extend functionality.
Coming Soon!