Better Practices
Published in

Better Practices

Elevate your automation game by using Git Hooks

Automatically consume APIs and perform behind-the-scenes actions with your everyday Git commands

Photo by Efe Kurnaz on Unsplash

Getting started 🛠

  1. Create a file calledpost-checkout inside the .git/hooks directory.
  2. Make it executable, and change the permissions to suit:
    chmod +x post-checkout.
  3. Write some script to give our hook something to run. We can write our hook in any language. I like using Node, so that’s what we’ll go with here:
#!/usr/bin/env nodeconsole.log('Hello world!')
Testing a basic “Hello world!” post-checkout Git Hook

Making things more sophisticated ⚙

Manual workflow

Manual vs. Automated workflow with Git Hooks. Manual interactions in gray.

Automated workflow

module.exports = {
postmanApiKey: 'xxx',
collectionFile: 'postman_collection.json'
  • Determine when a new branch is created
  • Read the current/master postman_collection.json held in the repo
  • Upload this into our Postman workspace via Postman’s API so we can start working with it right away
post-checkout hook syncs repo based collection with Postman workspace
Automatically creating a Postman collection on a git checkout command
  • Identify if we’ve made any changes to the collection in Postman
  • If yes, pull those changes out of the Postman app into our local repo
  • Add another commit to keep things in sync
pre-push hook syncs changed made in Postman with local branch
Automatically commit an updated Postman collection to our branch (see highlight)
Pull request with our changed files as well as the synced collection file

Summary 🛤

Photo by SpaceX on Unsplash



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Matt Ball

🇬🇧 living in 🇺🇸. Solutions @ Postman 🚀. Wannabe racing driver.