0

How to generate ".env" when Deploying with Bitbucket AWS CodeDeploy add-on?

I see bitbucket-pipelines.yml can generate .env from bitbucket environment variables, but, how tie it up with Bitbucket AWS CodeDeploy add-on?

appspec.yml - can trigger script on deployment but how can I make it get .env from bitbucket environment variables?

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191

2 Answers2

1

BitBucket should not create .env, this service should know nothing about production .env. Instead the production .env should sit on secure AWS S3 bucket where only AWS CodeDeploy scripts can take it and put on the instance.

it would be copied like this

sudo aws --region us-east-2 s3 cp "s3://${S3_NAME}/prod.env" "${EC2_DIRECTORY}/.env"
Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
0

Looking at the documentation, BitBucket should make the environments available in the build environment, and you should be able to access them directly in your scripts run by your appspec.yml just like you would access any other environment variables.

For example, if we had an appspec like this:

hooks:
   AfterInstall:
     - location: scripts/runTests.sh
       timeout: 180

You could access the environment variables in scripts/runTests.sh like this:

# scripts/runTests.sh
echo "$BITBUCKET_BUILD_NUMBER"
# Or, use in some other valid way in your script
EmptyArsenal
  • 7,314
  • 4
  • 33
  • 56
  • I have not validated myself. I saw no one else responded to this question, so I did a little research and tried to offer a solution. If you try it and it doesn't work, I'll remove the answer. – EmptyArsenal May 16 '18 at 17:46
  • I tried it did not work for me. Sorry. It may work for bitbucket pipelines, but it does not work for AWS CodeDeploy. – Yevgeniy Afanasyev Dec 02 '18 at 08:20