liveBook · Manning

Shell script

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

A shell script is a text file containing commands interpreted and executed by a command-line shell.

A shell script puts commands you would otherwise type in a terminal into a file you can run again. It is useful for repeated development commands and for deployment steps that must work both locally and in an automated workflow. section 4.3.5 section 8.9.3

Listing 8.1 The “Hello World” shell script
echo "Hello world!"     #1

A minimal script contains one command: echo "Hello world!". Run the file with ./index.sh to check that it works. section 8.7.5

Before running a new script as ./index.sh, mark it executable with chmod +x ./index.sh. If it will run from a Git repository, commit and push the executable-file change. On Windows, run these shell scripts from a WSL2 Linux terminal rather than the regular Windows terminal. section 8.7.5

Wrap related commands as the task grows:

  • For daily development, up.sh can run docker compose up --build; down.sh can run docker compose down --volumes. A reboot script can chain the down and up commands. section 4.3.5
  • For deployment, separate scripts can build a Docker image, log in to a container registry and push the image, then apply Kubernetes configuration. section 8.9.3
  • Use environment variables for values such as the registry, image version and registry credentials. Set the required values before testing the scripts locally. section 8.9.3
Listing 8.6 A shell script for deployment to Kubernetes
envsubst < ./scripts/kubernetes/deploy.yaml  #1
   | kubectl apply -f -                    #1
#1 Invokes envsubst to fill parameters in the configuration file from environment variables, and then pipes the expanded configuration to kubectl to deploy the microservice to the Kubernetes cluster

The deployment script uses envsubst to fill environment-variable values into a Kubernetes YAML file, then pipes the result to kubectl apply. section 8.9.3

Get the manual deployment working locally before putting its scripts into GitHub Actions. Local runs make command failures easier to diagnose and let you retest deployment changes. An automated workflow can then invoke the same scripts; it does not need to hold every command inline. section 8.9.3 section 8.5

A script is not mandatory for a tiny workflow: GitHub Actions can run one command or several commands directly in its YAML file. Keeping substantial deployment commands in scripts is more valuable when they need separate local testing. section 8.7.7 section 8.9.3

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Works from a one-command script through executable permissions, environment variables and separate deployment scripts; does not teach shell syntax in depth.
Examples
Uses a Hello World script, Compose up/down wrappers, and scripts to build, publish and deploy a microservice.
What it assumes
Assumes familiarity with deployment commands and tools, while starting script execution with a one-command example.
What you can do after
Practice/perform local deployment steps in scripts before invoking those scripts in an automated workflow.
How it connects
Connects Docker Compose, Docker images, container registries, Kubernetes, environment variables and GitHub Actions.

Also explained in

Compare how 6 books explain shell script →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.