https://github.com/shresht7/read-file-action
A GitHub Action to read a file and export its contents 📄
https://github.com/shresht7/read-file-action
github-actions
Last synced: about 1 month ago
JSON representation
A GitHub Action to read a file and export its contents 📄
- Host: GitHub
- URL: https://github.com/shresht7/read-file-action
- Owner: Shresht7
- License: mit
- Created: 2022-03-19T13:40:26.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-12-01T19:19:50.000Z (6 months ago)
- Last Synced: 2025-03-26T01:41:31.118Z (about 2 months ago)
- Topics: github-actions
- Language: TypeScript
- Homepage:
- Size: 223 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Read File Action[](https://github.com/Shresht7/Gist-Mirror/releases)
[](./LICENSE)A GitHub Action to read a file and export its contents
[](https://github.com/Shresht7/read-file-action/actions/workflows/test.yml)
[](https://github.com/Shresht7/read-file-action/actions/workflows/validate.yml)
[](https://github.com/Shresht7/read-file-action/actions/workflows/action-readme.yml)Table of Contents
- [📖 Usage](#-usage)
- [📋 Inputs](#-inputs)
- [📋 Outputs](#-outputs)
- [📃 Workflow Example](#-workflow-example)
- [📑 License](#-license)---
## 📖 Usage
Use this action in a workflow step
```yaml
- name: read-file
id: read-file
uses: Shresht7/read-file-action@v1
with:
path: ./README.md
```You can then access the output contents using [expressions][1].
`${{ steps.read-file.outputs.contents }}`
> Note: this assumes you set the id as `read-file`.
You can tell this action to automatically parse `yaml` or `json` by specifying the `parse` input.
```yaml
- name: read-file
id: read-file
uses: Shresht7/read-file-action@v1
with:
path: ./package.json
parse: json
```> You can also set `parse` to `true` to let the action automatically determine the file-extension.
The parsed contents will be available as a stringified JSON. This output can be used by [`fromJSON`][2] in an [expression][1] or `JSON.parse` in an action.
---
## 📋 Inputs
| Input | Description | Default | Required |
| :------ | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------: | :----------: |
| `path` | Path to the file to read (can be a URL) | `undefined` | **required** |
| `parse` | Parse the file contents as `yaml` or `json`. If `true`, will try to automatically determine the file-extension. You can set it to `yaml` or `json` manually too. If `false`, it will return the raw string | `undefined` | |## 📋 Outputs
| Output | Description |
| :--------- | :----------------------- |
| `contents` | The contents of the file |## 📃 Workflow Example
The following workflow snippet demonstrates how this action can be used to read a file and its contents can be used by other actions. The snippet itself is placed here using this action in conjunction with the [markdown-slots][3] action.
Click to show
```yaml
# ================
# READ FILE ACTION
# ================name: Read File Example
# Activation Events
# =================on:
# When this workflow file changes
push:
branches:
- main
paths:
- .github/workflows/example-workflow.yml# Manual workflow dispatch
workflow_dispatch:# Jobs
# ====jobs:
update-readme:
runs-on: ubuntu-latest
steps:
# Actions Checkout ✅
# ===================- name: checkout
uses: actions/checkout@v3# Read File 📄
# ============- name: read-file
id: read-file
uses: Shresht7/read-file-action@v1
with:
path: .github/workflows/example-workflow.yml# Markdown Slots 📋
# =================- name: markdown-slots
id: markdown-slots
uses: Shresht7/markdown-slots@v1
with:
slots: |
- slot: example
content: ${{ toJSON(steps.read-file.outputs.contents) }}
# steps.read-file.outputs.contents is itself a YAML string (example-workflow.yml)
# which causes markdown-slots action to try and parse it as a part of content and fail.
# the toJSON function forces the results into a one-line string.# Push Changes 🌎
# ===============- name: check for changes
id: git-diff
run: |
if git diff --exit-code; then
echo "::set-output name=changes_exist::false"
else
echo "::set-output name=changes_exist::true"
fi- name: push
if: ${{ steps.git-diff.outputs.changes_exist == 'true' }}
run: |
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
git add .
git commit -m 'Update README.md 📄'
git push```
---
## 📑 License
[MIT](./LICENSE)
[1]: https://docs.github.com/en/actions/learn-github-actions/expressions
[2]: https://docs.github.com/en/actions/learn-github-actions/expressions#fromjson
[3]: https://www.github.com/Shresht7/markdown-slots