Jenkinsfile declarative syntax for conditional post-build step
like you've noted when only works inside a stage. And only valid steps can be used inside the post conditions.You can still use scripted syntax inside of a script block, and script blocks are a valid step. So you should be able to use if inside a script block to get the desired behavior.
... post { failure { script { if (env.BRANCH_NAME == 'master') { ... # your code here } } } }}
see JENKINS-52689