You’re right - it’s a scoping issue.
Two things are coming that will help you out here:
- In 7.7 there’s going to be globally scoped scripting area, which should work in runScript calls from the pipelines.
- We’ve been meaning, all along, to add a new kind of pipeline block that’s dedicated to executing a script. This would be the more elegant solution you’re looking for. I don’t know the timeline on this, hopefully soon after the release.