A simple GitHub action for updating a template file with specified values and pushing the changes to a repository.
- Description: The repository to push the changes to. Can be obtained from
the
github.repositorycontext. - Example:
alvelive/update-action - Required: true
- Description: The branch to push the changes to. Can be obtained from the
github.head_refcontext. - Example:
main,user/feature-branch - Required: true
- Description: The commit SHA to be used to construct
base_url. Can be obtained from thegithub.shacontext. - Required: true
- Description: The file to read the input from.
- Default:
action-template.yml - Required: true
- Description: The file to write the output to.
- Default:
action.yml - Required: true
- Description: The string to replace.
- Default:
{{base_url}} - Required: true
- Description: The string to replace with.
- Default:
https://raw.githubusercontent.com/${{ repository }}/${{ sha }}/ - Required: false
This action reads a template file, replaces a specified string with a given value or a generated URL, and writes the result to an output file. It then commits and pushes the changes to the specified repository and branch.
name: Update Action File
on:
push:
branches:
- main
jobs:
update-action-file:
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v2
- name: Update Action File
uses: ./.github/actions/update-action
with:
repository: ${{ github.repository }}
branch: ${{ github.head_ref }}
sha: ${{ github.sha }}
template: 'action-template.yml'
output: 'action.yml'
match: '{{base_url}}'
replace: 'https://example.com/new-url'-
Configure Git
- Sets global Git configuration for user name and email.
-
Pull latest changes
- Pulls the latest changes from the specified branch with rebase.
-
Update action file
- Reads the template file.
- Replaces the specified string (
match) with the given or generated value (replace). - Writes the result to the output file.
- Verifies the replacement.
-
Commit and push action
- Adds the updated output file.
- Commits the changes with a message.
- Pushes the changes to the specified branch.
The action uses the following environment variable to construct the replacement
string if replace is not provided:
replacement: Constructed ashttps://raw.githubusercontent.com/{repository}/{sha}/
- Ensure that the
templatefile exists and is accessible. - The action will fail if the specified
matchstring is not found and replaced in thetemplatefile. - The action uses Bash scripts to perform file operations and Git commands. Ensure that the runner environment supports Bash.