This question must have come up on installer-dev a number of times and probably got answered by Stéphane :)
But for at least one working example, build a Distribution package with Packages.app and set an OS requirement - check out the distribution file that gets generated. You could likely grab some of that code and then feed productbuild your custom Distribution file with that.
The package-building script in the Munki GitHub repo also has an example of dynamically building a Distribution file with productbuild, but it doesn't contain an OS X version check script.
Tim