5

I have a bitbucket repository. I have Deployment environment variables in bitbucket:

BITBUCKET_VARIABLE_PORT : 8080

In my bitbucket-pipelines.yaml script I can Write the variable into the .env file like this:

<...>
step: &deploy-to-environment
        name: Deploy to environment
        deployment: environment
        caches:
          - node
        script:
          - echo Create .env file
          - echo "PORT=$BITBUCKET_VARIABLE_PORT" > .env          
          - cat .env
<...>

But I would like to avoid rewriting the whole lines of .env file.

Is it possible to implement the following?

  1. I would like to have .env file with placeholders (.env file content):
    <...>
    PORT=<BITBUCKET_VARIABLE_PORT>
    HOST=<BITBUCKET_VARIABLE_HOST>
    <...>
  1. Replace these placeholders in yaml script section:
    <...>
        step: &deploy-to-environment
                name: Deploy to environment
                deployment: environment
                caches:
                  - node
                script:
                  - <replace_placeholders_here_in_script>
    <...>
Ihar Dziamidau
  • 337
  • 2
  • 9

3 Answers3

6

You can use sed to replace string in .env file

sed -i "s/BITBUCKET_VARIABLE_PORT/...xxxx.../" .env
sed -i "s/BITBUCKET_VARIABLE_HOST/...hostname.../" .env

You can use also variables for replacement

MYPORT=XXX
sed -i "s/BITBUCKET_VARIABLE_PORT/$MYPORT/" .env
Saboteur
  • 1,331
  • 5
  • 12
  • I would like to make it more general way without writing a line for each variable replacement. – Ihar Dziamidau Sep 18 '21 at 07:23
  • The required script should replace any placeholder with appropriate variable from deployment variables. By their names. – Ihar Dziamidau Sep 18 '21 at 07:24
  • Then provide variables as replacement. You can set variables in deploy utility MYPORT=xxx MYHOSTNAME=yyyy `sed -i "s/BITBUCKET_VARIABLE_PORT/$MYPORT/" .env sed -i "s/BITBUCKET_VARIABLE_HOST/$MYHOSTNAME/" .env ` – Saboteur Sep 18 '21 at 09:08
  • Is it possible to have all variables and their names in the nodejs script? I mean: npm run myscript. And then have these variables inside js script? Without hard-coded names of the variables in yaml script. – Ihar Dziamidau Sep 20 '21 at 05:06
  • Make sure to use the `.env` file in artifacts – power-cut May 11 '23 at 04:20
0

I had the same issue lately and didn't want to include 1 by 1 the env variable, what I ended up doing:

#1 Create in repository a .env.example file (This file will hold variable that required for this repository without values or default values that are not sensitive)

APP_NAME=MyApp
APP_ENV=
APP_KEY=
APP_DEBUG=true

Then I created a pipeline.sh

echo "[+] Building enviroment variables"

# Get contents of example file
ENV_CONTENT=$(cat ./.env.example)

# Output the content into sh script
echo "#! /bin/bash
echo \"
${ENV_CONTENT}
\"" > ./env.sh

# sed replace as key=${BITBUCKET_ENV_VARIABLE:-default_value_from_example}
sed -i -E "s/^([A-Z_]+)=(.*)$/\1=\${\1:-\2}/g" ./env.sh

chmod +x ./build/env.sh

# Exec the env sh script and output content to .env file
./build/env.sh > ./build/.env

So in case you have defined in .env.example APP_NAME and also defined it in bitbucket Repository Variables the final .env will have key APP_NAME=value_of_bitbucket_variable

SergkeiM
  • 3,934
  • 6
  • 36
  • 69
0

Here is the way I found to do it, you just have to be careful with those echo commands (here I'm making env for firebase but you can swap your bitbucket variables in how you like)

definitions:
  steps:
    - step: &Env
            name: Create .env file
            trigger: manual
            script:
              - echo VITE_FIREBASE_API_KEY="${VITE_FIREBASE_API_KEY}" >> .env
              - echo VITE_FIREBASE_AUTH_DOMAIN="${VITE_FIREBASE_AUTH_DOMAIN}" >> .env
              - echo VITE_FIREBASE_PROJECT_ID="${VITE_FIREBASE_PROJECT_ID}" >> .env
              - echo VITE_FIREBASE_STORAGE_BUCKET="${VITE_FIREBASE_STORAGE_BUCKET}" >> .env
              - echo VITE_FIREBASE_MESSAGING_SENDER_ID="${VITE_FIREBASE_MESSAGING_SENDER_ID}" >> .env
              - echo VITE_FIREBASE_APP_ID="${VITE_FIREBASE_APP_ID}" >> .env
              - more .env
            artifacts:
              - .env         
pipelines:
  default:
    - step: *Env