2

I have Serverless application that has two stages: develop (on branch develop) and master (on branch master). It has a file called config.js like this:

API_URL: api.prod.some-server.com

But I want that "prod" changed to "dev" when I'm on develop branch / on develop stage environment.
It's kinda exhausting if I should change the API_URL back-and-forth (prod -> dev) when I changed the current branch/dev_stage.

Is there any possible to automate this?
Thank you

Terry Djony
  • 1,975
  • 4
  • 23
  • 41

1 Answers1

1

Is there any possible to automate this?

Yes: You would need to not version config.js (keep it private), but to generate it (with the right value in it).

The generation script will determine the name of the checked out branch with:

branch=$(git rev-parse --symbolic --abbrev-ref HEAD)

Or (since Git 2.22, Q2 2019)

branch=$(git branch --show-current)

That means you could:

  • version only a template file config.js.tpl
  • version value files named after the branches: config.js.dev, config.js.master: since they are different, there is no merge issue when merging or switching branches.

Finally, you would register (in a .gitattributes declaration) a content filter driver.

smudge (image from "Customizing Git - Git Attributes", from "Pro Git book")

The smudge script, associated to the template file (config.js.tpl), would generate (automatically, on git checkout) the actual config.js file by looking values in the right config.js.<branch> value file.
The generated actual config.js file remains ignored (by the .gitignore).

See a complete example at "git smudge/clean filter between branches".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250