build.sh 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. docker push "$REG"
  38. # Sign the exact digest that was just pushed (not a mutable tag).
  39. FULL="$(docker inspect --format='{{index .RepoDigests 0}}' "$REG")"
  40. DG="${FULL##*@}" # sha256:...
  41. echo "==> cosign keyless sign $REG@${DG}"
  42. cosign sign --yes "$REG@${DG}"
  43. echo "==> done. Verify with:"
  44. echo " cosign verify --certificate-identity-regexp '<you>@.*' \\"
  45. echo " --certificate-oidc-issuer-regexp 'accounts\\.google\\.com' \\"
  46. echo " $REG@${DG}"
  47. fi