jhm
jhm

Reputation: 4539

Is there any way to use dotenv with Bitbucket Pipelines?

As the title says, is there any way to use dotenv with Bitbucket Pipelines for CI purposes, while still adding the (perhaps multiple) (.stage).env to .gitignore?

I know Pipeline supports environment variables, and that they can be referenced in bitbucket-pipelines.yml, but I can't figure out how to use dotenv files instead, and vary which file to use based on i.e. branch patterns.

For example, I'd like commits to develop to use .test.env variables, while commits to master instead uses the variables from .prod.env.

Perhaps I'm going down the wrong path? Although other websites use examples of multiple .env files, the library authors discourage that approach. I'm using Zeit Now for hosting, so I can't just SSH a .env file onto the server.

Any advice is very welcome :-)

Upvotes: 10

Views: 6397

Answers (2)

floflock
floflock

Reputation: 635

Create a base64 string out of your .env file. Then copy this string into your environment variables of your pipeline, see here: https://confluence.atlassian.com/bitbucket/environment-variables-794502608.html

For example, your content is now defined in APP_ENV, then you can use this line in your pipeline configuration file:

echo $APP_ENV | base64 --decode --ignore-garbage > ./www/.env

Now it is save because nobody knows your secrets in this file except your pipeline container itself.

This method could be used for all .env-files, also staging files. :)

Upvotes: 15

floflock
floflock

Reputation: 635

Rename the files inside your develop pipelines:

mv .test.env .env

or in your master pipelines:

mv .prod.env .env

Upvotes: 3

Related Questions