Case study · Automation
DevOps Bash Script Toolkit
Ten tested Bash scripts for the jobs that come back every week: tagging releases, Terraform applies, cleanup, health checks, secrets and backups.
How it fits together
- Script
- set -euo pipefail
- --help from comments
- --dry-run
- Lint
- ShellCheck
- Test
- Bats tests
- Ubuntu, macOS, Alpine, CentOS
- Release
- autotag.sh
- release workflow
The problem
Every team writes the same small scripts again, without tests, and each one fails in its own way. The goal was a set of scripts with one structure, built-in help, a dry-run mode where it matters, and tests that run on several systems.
What I built
- Scripts in 8 areas: CI/CD (autotag, build and deploy), Terraform apply wrapper, Kubernetes and Docker cleanup, resource and service-health monitoring, .env to Kubernetes secrets, S3 backups and log rotation.
- autotag.sh: reads commit messages since the last tag and bumps the semantic version (BREAKING CHANGE, feat:, fix:), with a dry-run mode.
- A ShellCheck and test matrix in GitHub Actions on Ubuntu and macOS runners, with Alpine and CentOS as containers.
- Bats tests for each script area, a script documentation template, and CONTRIBUTING and SECURITY files.
Decisions
- Each script stands alone, so a team can copy one file without the rest.
- Help text comes from the script's own header comments, so the docs and the code cannot drift apart.
What went wrong: A rollback that destroys
terraform_apply_wrapper.sh has a --rollback option that runs terraform destroy when an apply fails. On a stack that already exists, that removes working resources too, not only the failed change. Safer options are to stop and keep the state, or to re-apply the last known good commit.
What I would improve next
- Replace the destroy-based rollback with re-applying the previous commit, and require a confirmation flag for destroy.
- The README says POSIX-compliant, but the scripts start with #!/usr/bin/env bash and use Bash tests ([[ ]]). Change the claim to Bash.
- Each Bats file has one test (12 in total). Add tests for failure paths.