A GitHub Action to read a file and export its contents
Table of Contents
Use this action in a workflow step
- 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.
${{ 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.
- name: read-file
id: read-file
uses: Shresht7/read-file-action@v1
with:
path: ./package.json
parse: json
You can also set
parse
totrue
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
in an expression or JSON.parse
in an action.
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 |
Output | Description |
---|---|
contents |
The contents of the file |
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 action.
Click to show
# ================
# 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