Headers Build fails on context.branch-deploy

We are trying to password protect a branch deploy. In our toml file, we have the following:

    [context.branch-deploy]
      command = "cp netlify_headers public/_headers && gatsby build"

However, whenever we try to build the branch, we get the following error:
cp: cannot create regular file ‘public/_headers’: No such file or directory
“build.command” failed

Interestingly, the exact same setup works on another site, and we have no problem password protecting the deploy branches on that site. We have looked thru everything and there are no differences between the file builds between these 2 sites. So why is that the build fails and says: “cp: cannot create regular file ‘public/_headers’: No such file or directory” on one site, but another site the build works perfectly fine with the same exact toml? Why would our branch deploy fail to create a public folder?

I think the command should be reversed. That is, gatsby build && cp.... It is because it’s the gatsby build command that will generate the public folder and it doesn’t exist currently.

On a side note, Netlify detects the presence of those files in the root of the repo and processes them. There’s no need to copy those to public.

Thanks. We will try that reversal. But, as noted above the command works perfectly fine on other Gatsby installations. We also just tested a new gatsby installation and command worked fine. It is just throwing an error for this one installation.

As for the files, the idea is that Netlify doesn’t know what the file netlify_headers is, so you most certainly need to copy it and rename it to headers, otherwise it won’t know what to do with the file. And the idea is not to call it _headers originally so it is only processed for the branch-deploy. We simply followed the instructions in this post: Password protection for subdomains and specific pages

It might work otherwise because you might already have a public folder in those repos. That’s just my guess.

Reversing the order worked. Thanks so much! Always something simple :0) Our command is below for anyone else who runs into this, and wants to protect a branch-deploy with Gatsby.

[context.branch-deploy]
  command = "gatsby build && cp netlify_headers public/_headers"