Port of fix project build status caching to ee
This is a port of https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/10313 to EE.
I had to cherry-pick some more commits this MR was based on that weren't ported to EE yet, they applied cleanly.
cc @ayufan