Build, test and package workflows
Continuous integration (CI) runs checks on changes; continuous delivery prepares verified releases, while continuous deployment actually deploys them. Bash can coordinate build tools, test runners and packaging utilities; it does not replace those tools. Define a contract for each stage: inputs, output files, exit status and required dependencies. A stage returning zero must mean its promised work was completed, not simply that its final print succeeded.
Gate dependent stages
Use explicit status checks or an AND list when packaging depends on a successful test. Capture the original status in the else branch before logging. Separate informational stdout from diagnostic stderr. A function invoked in a conditional context cannot depend on errexit to stop every failing operation. Keep working files in a private directory, publish only a verified artifact and avoid reusing stale output from a previous run.
Git hooks are local executable scripts triggered at particular Git events. A pre-commit hook may reject a commit, but it can be bypassed and is not a central enforcement boundary. CI must independently check the committed source. The hook lab creates its own disposable repository, uses a synthetic identity and never pushes. Build and deployment are separate responsibilities; this chapter never deploys. Compare the build artifact against expected bytes using cmp, not command substitution which strips trailing newlines. In the hook drill, record a local marker from the hook to show it ran, rather than assuming any failed commit proves hook execution.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. Git, GNU tar, gzip and coreutils are external dependencies for the relevant examples. Do not run these in an existing repository or CI workspace.
Gate the package
build() { printf 'app v1\n' > build.txt; }test_build() { printf 'app v1\n' > expected.txt || return 1 cmp -s build.txt expected.txt}package() { cp -- build.txt package.txt; }if build && test_build && package; then printf 'package ready\n'else exit "$?"ficat package.txtExpected stdout:
package readyapp v1All three stages must succeed.
Stop after a failed test
build() { printf 'build ok\n'; }check() { return 7; }package() { printf 'package\n' > package.txt; }if build && check && package; then printf 'ready\n'else rc=$? printf 'blocked=%s\n' "$rc"fi[[ ! -e package.txt ]]Expected stdout:
build okblocked=7The package stage is skipped; this demonstration handles the expected failure. A real CI entrypoint must exit nonzero.
Local pre-commit gate
export GIT_CONFIG_NOSYSTEM=1 GIT_CONFIG_GLOBAL=/dev/null git init -q --template= repo || exit 1cd repo || exit 1mkdir -p .git/hooks || exit 1printf '#!/bin/sh\nprintf ran > hook.marker || exit 1\nexit 3\n' > .git/hooks/pre-commit || exit 1chmod +x .git/hooks/pre-commit || exit 1printf 'fixture\n' > note.txt || exit 1git add note.txt || exit 1if git -c user.name=Lab -c user.email=lab@example.invalid commit -qm fixture 2>diagnostic.txt; then exit 1fiprintf ran > expected.marker || exit 1cmp -s expected.marker hook.marker || exit 1if git rev-parse --verify HEAD >/dev/null 2>&1; then exit 1; fiprintf 'commit blocked\n' Expected stdout:
commit blockedGit refuses the commit because the executable hook failed. No commit or remote operation occurs.
Common mistakes
Do not print success after a failed stage or treat local hooks as a security gate.
Practice
Change the test stage to fail; show that package.txt does not exist. Restore success and verify the package bytes. Explain why CI must rerun checks even if a hook passed.
Next: CI runner environments and matrices