Shell Scripts : The founding stones for development architecture

Key Pointers

  1. What is a shell script?
  2. How can we find and determine if the shell scripts make our daily dev lifes better!
  3. Advantages of scripting in laying down a strong foundation for development architecture.

We all mostly talk about code architecture, scalability, maintainability and all those terms, but we always forget, it is us the developers who achieve them, lets try working in the direction of making the developer’s life a bit easier ( haha 😆 😄 ) which some development architecture and shell scripts

What is shell script

By definition: “A shell script is a computer program designed to be run by the Unix/Linux shell”.

Let’s talk in developer’s language now, shell scripts are small sets of commands that are designed in such a way that they not only ease up your day to day command activities but also helps you to not remember so many commands which we use heavily. It can be as simple as deploying a frontend build with “scp” or creating your react native builds for distribution.

How can we find and determine if the shell scripts make our daily dev lifes better!
Wherever you feel the job can be automated and can be made to run without your requirement to interact with the terminal, there is the place where you can use shell scripts, remember the days of executing “npm i” and then coming back post drinking your hot tea/coffee and seeking the command still processing? Yes, those like instances are where you can use the scripts!!

Some day to day examples may include:

  1. Codepush builds, codepush release ( refer appcenter, codepush to learn more about it )
  2. Frontend build, zipping the builds and sending them over to your virtual machines, extracting them there and moving them to the respective folder and yes, restarting your pm2
  3. Creating builds for QA and releases. etc.

Advantages of scripting in laying down a strong architecture for your repositories

Yes there are several advantages of creating shell scripts, yes they make our lives easy, but how do they actually make developer’s lives easy and lay down a strong foundation for development architecture is what we are here to find.

We all have found ourselves in the situations where we have to create builds, deploy the frontend servers on our virtual machines and we all are well versed with those commands which we all know we have saved them in a sequential order in our terminals and we all end up pressing the up arrow to find them ( yes IK alias too )

But, all these up arrow presses can be cut short with some easy steps to convert our commands into shell scripts

Let’s take a look at the following shell script, this is being used to create an android build for react native

green=`tput setaf 2`# feature name would be the 1st argumentBUILD_NAME=${1:-'build-android-demo'}PACKAGE_VERSION=$(./scripts/version-num.sh ./package.json)yarn installnpx jetify#BUILDcd android && ./gradlew clean && ./gradlew assembleRelease && cd ..#MOVE FILEORIG_DIR="android/app/build/outputs/apk/release"mkdir -p buildmv "${ORIG_DIR}/app-release.apk" "build/${BUILD_NAME}_${PACKAGE_VERSION}.apk"echo "\n${green}${BUILD_NAME}_${PACKAGE_VERSION}.apk"open build  #This only works on macOS

If you save this script in your code base and then for every build you would have to created you don’t have to

  1. Ensure all the packages are installed
  2. Jetify
  3. Navigate into android, clean the gradle and make the release
  4. Rename the build to an appropriate name and copy it from. `/android/app/build/outputs/apk/release` folder to send it where ever needed
  5. And most importantly, you can leave this command, go have your coffee and by the time you come back, you will ideally have your build ready with you.

Crucial Benefits

  1. You can at any time onboard a new team mate into the the deployment and build process without being concerned about sharing the write set of command with the person in said chronological order.
  2. You personally won’t have to remember all such commands for rainy days.
  3. Can be very well reused in almost all the projects that you have of the same domain
  4. You won’t have to think about ci-cd jobs and workflows, just run a script like this and you will be golden

More examples
1. Creating iOS builds on react native: Save this as “iosBuild.sh”

yarn env:devyarn installcd iospod installcd .. react-native run-ios

execute: sh ./iOSBuild.sh
PS: You can always add the flags on the top of react-native run-ios

2. Creating/Pushing builds to virtual machine and deploying them from your local machine at an ease of 1 command: Save this as “sourceCode:deployment.sh”

pink=`tput setaf 5`echo "\n${pink}starting: frontend and prod build\n"yarn front:build && yarn back:buildecho "\n${pink}starting: frontend and prod ziping\n"tar -czvf build-frontend.tar.gz ./build && tar -czvf build-backend.tar.gz ./prodecho "\n${pink}starting: frontend and prod zip send to staging server\n"scp -r ./build-backend.tar.gz virtaul-machine-folder:~/staging-builds/build-backend.tar.gz && \scp -r ./build-frontend.tar.gz virtaul-machine-folder:~/staging-builds/build-frontend.tar.gzecho "\n${pink}ssh into virtual-machine\n"ssh ssh-config-name 'cd staging-builds && sh ./deployment.script.sh'

execute: sh ./sourCodeDeployment.sh

PS: Ensure ssh-config-name exists in you .ssh configs and inside your machine you have a folder like “staging-builds” ( it can be name anything ). Inside that we need to have a deployment.script.sh which will look something like

sudo rm -rf /home/ubuntu/projects/deployment-project/prod && \sudo rm -rf /home/ubuntu/projects/deployment-project/build && \tar -xvzf build-frontend.tar.gz ./build && \tar -xvzf build-backend.tar.gz ./prod && \sudo mv ./build /home/ubuntu/projects/deployment-project && \sudo mv ./prod /home/ubuntu/projects/deployment-project && \cd /home/ubuntu/projects/deployment-project && \yarn install --ignore-engines && \yarn db:migrate && \cd /home/ubuntu/projects && \pm2 reload deployment-project

!~Happy coding~!

--

--

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