Time to read: 3 minutes

I write code almost daily. Little apps. Fun projects. Things for actual business purposes. I recently learned how to automate things via bash scripts. They’re simple. I just had never explored them. Below you’ll find two I am using daily.

I work on two types of projects. 1 is actual code. The second is updating and writing on my website. So naturally, being lazy with typing I wrote code to automate the series of deployment steps from the time I save => live in app/website.

The scripts are exposed via source in my .zshrc file. In my particular case I titled my file .my_custom_commands.sh and enabled executable privileges. This enables a named function in that file to be called via command line.

The first function is used to push changes to github. Its titled deploy (most things I have deploy after going to github-at least to staging).

Here is the code.

#!/bin/bash

function deploy(){
  git add .

  echo 'Enter the commit message:'
  read commitMessage

  git commit -m "$commitMessage"

  echo 'Enter the name of the branch:'
  read branch

  git push origin $branch

}

A few gotchas to look out for. Once you save the file, enable it to be executable via chmod +x .my_custom_commands.sh add it to your bash/zshrc profile script via source ~/.my_custom_commands.sh and restart your terminal. From there you should be able to type deploy and trigger the code.

The second function is used to deploy my website. It is set up on github pages and gets pushed to master then a series of commands ran on it to enable the changes on the github pages branch I have.

#!/bin/bash

function website(){

  git add .

  git commit -m "updating website"

  git push origin master

  jgd -n
}

One gotcha to look out for here => I included the #!/bin/bash again so the code would format nicely on the website. You should not paste that inside of your script anywhere except the very first line. So, if you have more than 1 function inside the same file you would have #!/bin/bash on line 1 and thats it!

I also added this function to build my websites locally and serve on localhost before deploying. I only use it if I am making massive changes (css usually) but it is helpful to test.

#!/bin/bash

function buildweb(){
  bundle exec jekyll serve
}

I have since also added the function webme to make any instances of code running locally available to outside machines. This helps a ton when developing services that will be deployed later on. The code is below and asks which port you’d like your tunnel to be available on:

#!/bin/bash

function webme() {
  echo 'what port?'
  read port

  ~/ngrok http $port
}

It is that simple. Once you add that one (if you need too) make sure to restart your terminal and once again, you’ve saved a few seconds per deployment. Add that up over the course of a year and you’ve got some time back!

This post was last updated: Friday, October 04, 2019