You’ve probably been depending on automated pull requests from Dependabot, but how about making your own pull requests from GitHub actions? This capability can be used for automation that looks for changes, then updates files in your own repository with little to no additional setup needed.
- In dblock/lost-robbies the workflow checks for new sales and raises a PR after updating the JSON data and the
- In opensearch-project/project-meta the workflow enumerates public repositories in the opensearch-project organization and adds new repos to a
Below are some implementation details from opensearch-project/project-meta.
GitHub Action Setup
The job is executed on all changes to
main and daily at midnight.
Permissions and Tokens
The job checks out code, and needs a
env. to make pull requests.
Generating a PR Title and Body
At first I was hard-coding PR titles and commit messages. That’s not ideal. Compare the following PRs. The second version is much more specific!
This can be achieved by setting an environment variable during the workflow execution by piping it into
$GITHUB_ENV, and reusing it in the PR.
- The workflow modifies
.gitignoreby adding lines to it, such as
- Find all additions that start with
| grep '+/'.
- Extract the name of each addition with
| cut -f2 -d'/', e.g.
- Combine all additions into a comma-separated list with
| paste -sd ',' -.
- Add a space after each comma with
| sed "s/,/, /g" |.
- Replace the last comma by an
| sed 's/\(.*\),/\1 and/'.
- Pipe everything into
echo REPOS_ADDED=$(...) >> $GITHUB_ENV.
Make a Pull Request
See opensearch-project/project-meta#7 for an example.