build.sh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/bin/sh
  2. # Build unbound-sec and (optionally) push + sign with cosign in keyless mode.
  3. #
  4. # COSIGN_SIGN=1 requires a REGISTRY_IMAGE (a registry reference, e.g.
  5. # ghcr.io/ric/unbound-sec:latest). The image is pushed first, then the exact
  6. # digest that was pushed is signed — so the signature is bound to the built
  7. # image, not to a (possibly pre-existing) registry tag.
  8. #
  9. # Verification (identity conditions live on the VERIFY side, not sign):
  10. # cosign verify --certificate-identity-regexp 'ric@.*' \
  11. # --certificate-oidc-issuer-regexp 'accounts\.google\.com' \
  12. # "$REGISTRY_IMAGE@sha256:<digest>"
  13. set -eu
  14. IMAGE="${IMAGE:-unbound-sec:latest}"
  15. # Version from the latest git tag (vX.Y.Z) if available, else "dev".
  16. VERSION="$(git describe --tags --always 2>/dev/null | sed 's/^v//' || echo dev)"
  17. # A unique cache-buster per run is NOT used here on purpose: reproducible
  18. # builds rely on layer caching. The test suite passes --build-arg CACHEBUST
  19. # when it needs a forced rebuild.
  20. docker build \
  21. --pull \
  22. -t "$IMAGE" \
  23. -t "unbound-sec:v${VERSION}" \
  24. .
  25. if [ "${COSIGN_SIGN:-0}" = "1" ]; then
  26. REG="${REGISTRY_IMAGE:-}"
  27. if [ -z "$REG" ]; then
  28. echo "COSIGN_SIGN=1 requires REGISTRY_IMAGE (registry reference)" >&2
  29. exit 1
  30. fi
  31. case "$REG" in
  32. */*) : ;; # has a registry/host component
  33. *) echo "REGISTRY_IMAGE must be a registry reference (host/repo:tag)" >&2; exit 1 ;;
  34. esac
  35. echo "==> pushing $REG"
  36. docker tag "$IMAGE" "$REG"
  37. PUSH_OUT="$(docker push "$REG" 2>&1)"
  38. printf '%s\n' "$PUSH_OUT"
  39. # Take the digest from the PUSH result itself ("digest: sha256:..."), so the
  40. # signature is bound to the registry reference that was just published. This
  41. # is more explicit than .RepoDigests[0], which with multiple entries need not
  42. # be the one just pushed.
  43. DG="$(printf '%s\n' "$PUSH_OUT" | grep -oE 'digest: sha256:[0-9a-f]+' | head -1 | awk '{print $2}')"
  44. if [ -z "$DG" ]; then
  45. echo "could not determine pushed digest from push output" >&2
  46. exit 1
  47. fi
  48. echo "==> cosign keyless sign $REG@${DG}"
  49. cosign sign --yes "$REG@${DG}"
  50. echo "==> done. Verify with:"
  51. echo " cosign verify --certificate-identity-regexp '<you>@.*' \\"
  52. echo " --certificate-oidc-issuer-regexp 'accounts\\.google\\.com' \\"
  53. echo " $REG@${DG}"
  54. fi