Automating (kinda) Git Commands And Push Notifications With I F T T T And Bash

I am currently making “headway” (such a good word, so ambiguous, yet so professional and serious), on a workflow application integrated with Github - starting soon, I will have updates about how exactly it works and why it is life-altering, or on why exactly it does not work and why it is not life-altering(have not reached that critical juncture, yet). While working on that though, I got interested in getting push notifications for commits to the repo and automating git commands(a bit beyond just writting aliases). What follows is a recap of (or for some an introduction to) git aliases, tutorial on how to set up IFTTT applets, and my primordial amalgamation of both in one bash script.

Git Aliases

So, assuming git is set up correctly, just enter the command

git config --global --edit

to edit your git config file. You can read more about git config files and their power here.

And voila… On my config file, for example, I wrote aliases for the commands I use most. So instead of typing out “git add .” I can just to “git a”, instead of “git commit -m “commit message”, I can do “git c “commit message”, and so forth.

How much time do I really save? Probably not much. But it makes me feel like the mysterious kid on the block or something like that. Now, I do not think anybody will ever say, “Wow, he pushed a git commit so quickly, Who is he? How does he do it?” as I walk into a dark alley all cloak and dagger, end scene, roll credits…but stranger things have happened.

Now, notifications for your git repos are pretty straightforward too. There are simpler ways to get notified by GitHub every time something happens on one of your repos(I believe).However, if you, like me, want to make your life even more difficult, then you can use IFTTT

Getting IFTTT Setup (Two Routes):

Route One(easy): Use their integration, relinquish some control, give IFTTT access to your Github

  1. Goto to IFTTT. Sign up for an account. I cannot help anymore with this part.
  2. Select the Create tab
  3. You should now be here
  4. Press the add button and then search for Github, then select the service works best for your mileage.
  5. Give IFTTT authorization to your GitHub account. Are they trustworthy? Who knows! No, really, who? Is trying to protect our data useless in the 21st century? Probably.
  6. On this page, put in the repo you want tracking added to. I, again, cannot help anymore with this part.
  7. You have now completed the IFT or “if this” part of the project. You can stop here, if you want. Alright you did not stop, let’s push forward. Say I chose any new commit as my trigger. If this happens, a “then that” will be triggered. Nice.
  8. Ok, now add then that trigger. After selecting add, search the services for notifications and choose between rich and basic push notifications. I am going to follow the rich notification route.
  9. Edit the fields that you want in the notification here Press create action, continue, and then finish.
  10. Now you have an applet or whatever they call it these days.
  11. Download the application on your phone or computer, depending on where you want to receive it, and sign in with the same account you just made.
  12. And finally, test it by triggering the “if this” portion(ie, make a commit). If your cpu starts getting super hot and weird noises, like how you imagine a cacodemon would sound, I assume no responsibility, but you should probably get that checked out
  13. Success? If not, let me know what is going on with a comment below or an email.

Route Two(not as easy): Use their integration, maintain some control, do not give them access to your Github

  1. Follow the signup and create steps from above, but this time, search for webhooks, not github.
  2. Select receive a web request and enter a name for the event. You should be here
  3. Add a notification action for the “then that” portion.
  4. Here, I used the basic notification with two values and the “occurred at” ingredient. Press create action, then continue, then finish.
  5. Download the app on your phone(or computer) and sign in with the same account.
  6. Test it by pressing on your profile picture, going to my services, selecting webhooks, and clicking on documentation.
  7. If you followed that, you are here:
    As I have, enter “notify” in the first link and “test” in the JSON body. The black rectangles you should not see, I am protecting my private key which you should also do. If done correctly, a notification should pop on your phone as it does here:

(Be patient. Sometimes there is a bit of a delay. If it still does not work, leave a comment! We will work it out together! Same rules apply from previous route)

You can change the title of your applet if you want it to say something other than ‘The event named “notify” occurred on the Maker Webhooks Service’ Ok, doneso. The next step is to get actual notifications for the changes to the repository.

Triggering from a script

Disclaimer: I am a newbie with bash scripts and recommend you find ways to make this better, and I will do the same and update if need be.

The script:

  1. Open your command line and create a file called gitAlert: You can do this on any Unix/Linux OS with the command
    		
         touch gitAlert
    
  2. Open that file in a text editor and copy this code. The argument(“arg=” will be the text sent in the notification)

    	
         #!/bin/bash
         arg=$(echo $1 $2 $3 $4 $5 $6 $7 $8 $9 | sed 's/ /%20/g') curl -X POST -H "Content-Type: application/json" -d 
         '{"value2":"<-- was pushed to one of your repos on"}' https://maker.ifttt.com/trigger/notify/with/key/yourprivatekey?value1=$arg			
    
  3. Save that file and make it executable by typing “chmod +x gitAlert”, in the same folder that you just made the file in.
  4. Now, run the script with this command: “./gitAlert this is a test commit”

If all is well, your command line should show this message:

And a notification should have come through:

Great, but this does not actually tell me when a commit happens. Mission not accomplished.

Merging git commands and IFTTT

The easiest way to run our script(gitAlert) with git operations is by chaining operators, but the whole point of this project was to save time, and adding a command to the git workflow does not do that.

I finally settled on combining the git commands and notifications into one script then just running that. This way, one command will do a ton of operations for me.

Vim back into the bash script(gitAlert) and copy this code above the notification trigger, but below the bash header:

  [[ -z "$1" ]] && echo "Please enter a commit message:";
    typeset msg="$( [[ -n "$1" ]] && echo "$*" || echo $(head -1) )";
    git pull;
    git add .;
    git commit -m "$msg";
    git push;
    date

Go ahead and save that bash file and move it to one of the local GitHub Repos.

Once you run it, a handful of things will happen.

Running ./gitAlert updated tests for database connection

Will do a git pull from the remote repo, git add . any changes to the local, and stage a new commit with the message “updated tests for database connection”, finally git push will push that to the remote repo. Finally, you will get a notification with the text “updated tests for database connection”. If you forget to enter a commit message and just do “./gitAlert”, that is ok! The bash script will wait for user input before continuing( hence the “Please enter a commit message” in the code).

But does it work? Well, we shall see.

Sure does!

So at this point, we have one file that automates some of the “heavy lifting” and sends out a notification when there is a commit made to your repo.

If you are working on a project with a team and get notifications every time somebody commits(if you tell them to run the file). However, if they also want notifications, they would have to add their credentials to the bash script, but that should be easy now!

The work here is preliminary, and there are downsides. For example, you would have to tell people explicitly to run the executable to get notifications for team members’ commits. And if this is a public repo, you would have to hide the file because you do not want anyone to see your key. I am still working on solutions for these drawbacks and will update you all soon!

You could also use this bash script and IFTTT(minus git commands) to notify you when an ML model has finished training, a large file has been downloaded, a print job is done, etc.

Bash code snippet on my github - just add your private key.

Sources/Further reading:

Sending Push Notifications - Jamie Bullock

Git in Bash - Paul Hodges

Written on February 23, 2021