Unable to replace .env value with environment variables from Gitlab CI

How can I replace the value from .env file with gitlab’s environment variables correctly?

I am using Laravel framework for my web and I need to assign those protected variables before deploying to AWS(Elastic Beanstalk). However, when I checked those values are not showing what I expected. Please guide me to the right path as I am new to dev-ops.

Notes: $LARAVEL_APP_KEY & $PROD_DB_DATABASE are not masked variables, I can see the correct value during echo command but uploaded .env file is showing $LARAVEL_APP_KEY & $PROD_DB_DATABASE instead of correct value. I even tried with several approaches but still no luck and please look at the example below.

.gitlab-ci.yml file:

default:
  image: custom_image

stages:
 - deploy

deploy_production:
  stage: deploy
  variables:
    LOCAL_PROD_DB_HOST: $PROD_DB_HOST
    LOCAL_PROD_DB_DATABASE: $PROD_DB_DATABASE
    LOCAL_PROD_DB_USERNAME: $PROD_DB_USERNAME
    LOCAL_PROD_DB_PASSWORD: $PROD_DB_PASSWORD
    LOCAL_LARAVEL_APP_KEY: $LARAVEL_APP_KEY
  script:
    - echo ${LARAVEL_APP_KEY} . " " . ${PROD_DB_HOST} . " " . ${PROD_DB_DATABASE} . " " . ${PROD_DB_USERNAME} . " " . ${PROD_DB_PASSWORD}
    - cp .env.example .env
    - sed -i 's|^APP_ENV=.*|APP_ENV=production|' .env
    - sed -i 's|^APP_DEBUG=.*|APP_DEBUG=false|' .env
    - sed -i 's|^APP_KEY=.*|APP_KEY=${LARAVEL_APP_KEY}|' .env
    - sed -i 's|^PROD_DB_HOST=.*|PROD_DB_HOST=${PROD_DB_HOST}|' .env
    - sed -i 's|^PROD_DB_DATABASE=.*|PROD_DB_DATABASE=${PROD_DB_DATABASE}|' .env
    - sed -i 's|^PROD_DB_USERNAME=.*|PROD_DB_USERNAME=${PROD_DB_USERNAME}|' .env
    - sed -i 's|^PROD_DB_PASSWORD=.*|PROD_DB_PASSWORD=${PROD_DB_PASSWORD}|' .env
    - cp .env.example .env1
    - sed -i 's|^APP_ENV=.*|APP_ENV=production|' .env1
    - sed -i 's|^APP_DEBUG=.*|APP_DEBUG=false|' .env1
    - sed -i 's|^APP_KEY=.*|APP_KEY=$LARAVEL_APP_KEY|' .env1
    - sed -i 's|^PROD_DB_HOST=.*|PROD_DB_HOST=$PROD_DB_HOST|' .env1
    - sed -i 's|^PROD_DB_DATABASE=.*|PROD_DB_DATABASE=$PROD_DB_DATABASE|' .env1
    - sed -i 's|^PROD_DB_USERNAME=.*|PROD_DB_USERNAME=$PROD_DB_USERNAME|' .env1
    - sed -i 's|^PROD_DB_PASSWORD=.*|PROD_DB_PASSWORD=$PROD_DB_PASSWORD|' .env1
    - echo ${LOCAL_LARAVEL_APP_KEY} . " " . ${LOCAL_PROD_DB_HOST} . " " . ${LOCAL_PROD_DB_DATABASE} . " " . ${LOCAL_PROD_DB_USERNAME} . " " . ${LOCAL_PROD_DB_PASSWORD}
    - cp .env.example .env2
    - sed -i 's|^APP_ENV=.*|APP_ENV=production|' .env2
    - sed -i 's|^APP_DEBUG=.*|APP_DEBUG=false|' .env2
    - sed -i 's|^APP_KEY=.*|APP_KEY=${LOCAL_LARAVEL_APP_KEY}|' .env2
    - sed -i 's|^PROD_DB_HOST=.*|PROD_DB_HOST=${LOCAL_PROD_DB_HOST}|' .env2
    - sed -i 's|^PROD_DB_DATABASE=.*|PROD_DB_DATABASE=${LOCAL_PROD_DB_DATABASE}|' .env2
    - sed -i 's|^PROD_DB_USERNAME=.*|PROD_DB_USERNAME=${LOCAL_PROD_DB_USERNAME}|' .env2
    - sed -i 's|^PROD_DB_PASSWORD=.*|PROD_DB_PASSWORD=${LOCAL_PROD_DB_PASSWORD}|' .env2

Result in .env ~ .env2

#.env
APP_ENV=production
APP_KEY=${LARAVEL_APP_KEY}
APP_DEBUG=false
PROD_DB_HOST=${PROD_DB_HOST}
PROD_DB_DATABASE=${PROD_DB_DATABASE}
PROD_DB_USERNAME=${PROD_DB_USERNAME}
PROD_DB_PASSWORD=${PROD_DB_PASSWORD}

#.env1
APP_ENV=production
APP_KEY=${LOCAL_LARAVEL_APP_KEY}
APP_DEBUG=false
PROD_DB_HOST="$PROD_DB_HOST"
PROD_DB_DATABASE="$PROD_DB_DATABASE"
PROD_DB_USERNAME="$PROD_DB_USERNAME"
PROD_DB_PASSWORD="$PROD_DB_PASSWORD"

#.env2
APP_ENV=production
APP_KEY=${LOCAL_LARAVEL_APP_KEY}
APP_DEBUG=false
PROD_DB_HOST=${LOCAL_PROD_DB_HOST}
PROD_DB_DATABASE=${LOCAL_PROD_DB_DATABASE}
PROD_DB_USERNAME=${LOCAL_PROD_DB_USERNAME}
PROD_DB_PASSWORD=${LOCAL_PROD_DB_PASSWORD}
1 Like

Use double quotes, i.e.:

- sed -i "s|^APP_ENV=.*|APP_ENV=production|" .env

1 Like