maxgfr/github-change-json is a GitHub Action which lets you change a value from a JSON file (e.g. package.json, tsconfig.json, or any other JSON file).
Sometimes you need to update a .json file in your project during a workflow. For example, when you want to deploy a package to GitHub Packages and npm packages with a different name for each package (e.g. @maxgfr/package-name for GitHub Packages and package-name for npm packages) or you want to publish a create-react-app to GitHub Pages by modifying the homepage prop such as here. The purpose of this action is to handle this kind of situation by updating your .json file directly during the workflow without having to manually edit it.
name: 'action-test'
on:
pull_request:
push:
jobs:
action:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Get commit sha
run: |
echo "GITHUB_SHA=${GITHUB_SHA}" >> $GITHUB_ENV
- name: Modify name of the package.json
uses: maxgfr/github-change-json@main
with:
key: 'name'
value: '@maxgfr/example-${{ env.GITHUB_SHA }}'
path: example/package.json
commit: true # it will commit the change
- name: Modify name of the package.json locally
uses: maxgfr/github-change-json@main
with:
key: 'name'
value: 'yo'
path: ./example/package.json- name: Update package name for GitHub Packages
uses: maxgfr/github-change-json@main
with:
key: 'name'
value: '@my-org/my-package'
path: package.json
commit: false- name: Bump version
uses: maxgfr/github-change-json@main
with:
key: 'version'
value: '2.0.0'
path: package.json
commit: true- name: Update TypeScript target
uses: maxgfr/github-change-json@main
with:
key: 'compilerOptions.target'
value: 'ES2020'
path: tsconfig.json| Name | Type | Required | Description |
|---|---|---|---|
| path | string | yes | Path to the JSON file you want to update (relative to repository root) |
| key | string | yes | Key that you want to modify in your JSON |
| value | string | yes | Value linked to the key that you want to modify in your JSON |
| commit | boolean | no | Boolean flag to commit and push the changes to the repository (default: false) |
This action does not produce any outputs. It modifies the specified JSON file and optionally commits the changes.
The action will:
- Read the JSON file at the specified path
- Update the specified key with the new value
- Write the modified JSON back to the file with proper formatting (2-space indentation)
The action will fail and provide clear error messages if:
- The specified file does not exist
- The file contains invalid JSON
- The file cannot be read or written
When commit: true:
- The action will configure git with the GitHub Actions bot credentials
- Changes will be committed with message:
chore: update <path> with <key>=<value> - The commit will be pushed to the branch that triggered the workflow
- Pre-commit hooks are bypassed with
--no-verify
pnpm installpnpm run buildpnpm testpnpm run lintpnpm run formatpnpm run allMIT
Contributions are welcome! Please feel free to submit a Pull Request.