Automate The Creation And Merge Of Pull Requests With gh cli & PowerShell

Pavol Kutaj
1 min readSep 4, 2021

--

The aim of this playbook🏁 is to create a snippet for scripting that creates and merges a pull request in the feature branches GitHub workflow. I am using this for things like post-outage configuration amendments where I need to update lots of repositories at once, script-wise. This helps to remove any interactive process, utilizing git & gh cli

I am leaving out the cloning and navigation part for multi-repo manipulations. This is focused primarily on automating the merge of a single-repo.

1. instructions

  • check if you are on a master branch
  • pull possible changes
  • checkout a new branch
  • do the work
  • stage -> commit -> push
  • create and merge the PR
if ("master" -ne ((git branch) -replace "\* ", "")) {git checkout master}
git pull origin
git checkout -b "JIRA-ID/foo_description"

# MAIN CODE CHANGING SOMETHING IN THE REPO

git add "foobar.json"
git commit -m "<ops> config change within foobar.json"
git push -u origin
gh pr create --fill
gh pr merge --merge --delete-branch
  • -u sets origin as a default remote repo
  • --fill helps not to prompt for title/body and just use commit info
  • --merge selects a merge commit, instead of rebase/squash options
  • optional --delete-branch Delete the local and remote branch after merge

2. sources

--

--

No responses yet