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
README.md
. - In opensearch-project/project-meta the workflow enumerates public repositories in the opensearch-project organization and adds new repos to a
.meta
file.
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 GITHUB_TOKEN
in 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
.gitignore
by adding lines to it, such as/cross-cluster-replication/
. - Find all additions that start with
+/
using| grep '+/'
. - Extract the name of each addition with
| cut -f2 -d'/'
, e.g.cross-cluster-replication
. - 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
and
with| sed 's/\(.*\),/\1 and/'
. - Pipe everything into
REPOS_ADDED=
withecho REPOS_ADDED=$(...) >> $GITHUB_ENV
.
Make a Pull Request
Profit
See opensearch-project/project-meta#7 for an example.