Hermetic Build Environment Check
Assert that reproducibility-critical environment variables are pinned before a build, failing fast when they are not.
Code
#!/usr/bin/env bash
set -euo pipefail
# 可复现构建依赖这些变量被显式固定,而不是继承自当前 shell。
required=(SOURCE_DATE_EPOCH TZ LC_ALL LANG)
missing=()
for name in "${required[@]}"; do
if [[ -z "${!name:-}" ]]; then
missing+=("$name")
fi
done
if (( ${#missing[@]} > 0 )); then
echo "hermetic check failed; unset: ${missing[*]}" >&2
exit 1
fi
# UMASK 影响嵌入产物的文件权限,必须是固定值。
if [[ "$(umask)" != "0022" ]]; then
echo "hermetic check failed; umask is $(umask), expected 0022" >&2
exit 1
fi
echo "environment pinned for reproducible build"How it works
set -euo pipefail removes the shell’s usual optimism — errors abort, empty variables abort, hidden pipeline failures abort.
The script leans on parameter expansion 4 times, using forms like ${var:-default} so defaults, trimming, and casing happen inside the shell with no external tool.
Key statements worth quoting
for name in "${required[@]}"; do
if [[ -z "${!name:-}" ]]; then
missing+=("$name")
if (( ${#missing[@]} > 0 )); then
echo "hermetic check failed; unset: ${missing[*]}" >&2
if [[ "$(umask)" != "0022" ]]; then
echo "hermetic check failed; umask is $(umask), expected 0022" >&2
Interactive Tools for This Topic
Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.JSON to TOML ConverterTurn a JSON object into TOML, emitting nested objects as tables and arrays of objects as array-of-tables blocks.JSON to YAML ConverterConvert JSON to readable YAML with nested block mappings, sequences, and quoting only where it is required.
FAQ
- Does the script stop on the first error?
- Yes — `set -euo pipefail` aborts on the first failing command, treats unset variables as errors, and fails the whole pipeline if any stage fails. Nothing continues half-broken.
Report a problem with this exampleOpens a pre-filled issue on GitHub.