Pipeline enviroment variables
Asked by Ricardo Souza:

Hello. I am having some problems to send environment deployments/variables from pipelines to my .env file. Can you help me to fix it please?

BTW: some variables are defined as repository variables and some at deployments section.

1) #bitbucket-pipelines.yml

image: atlassian/default-image:2

version: 3

pipelines:  
branches:  
devops:  
- step:  
name: Deployment to Staiging-B  
deployment: Staging-B  
script:  
- echo "$SSH_PRIVATE_KEY" \| tr -d '\\r' \> ssh-key  
- chmod 600 ssh-key  
- ssh-keyscan $SERVER \>\> \~/.ssh/known_hosts  
- scp -r -i ssh-key \* $USER@$SERVER:$BASE_PATH  
- ssh -i ssh-key $USER@$SERVER "chmod +x /home/devops/projects/pimentarecruiting/backend-nodejs/deploy/bitbucket-deploy.sh"  
- ssh -i ssh-key $USER@$SERVER "/home/devops/projects/pimentarecruiting/backend-nodejs/deploy/bitbucket-deploy.sh"  
- echo "Finished staging-B deployment!"  
environment:  
SSH_PRIVATE_KEY: $SSH_PRIVATE_KEY  
USER: $USER  
SERVER: $SERVER  
BASE_PATH: $BASE_PATH  
SONAR_HOST: $SONAR_HOST  
SONAR_PROJECT_KEY: $SONAR_PROJECT_KEY  
SONAR_TOKEN: $SONAR_TOKEN  
S3_BUCKET: $S3_BUCKET  
S3_ACCESS_KEY_ID: $S3_ACCESS_KEY_ID  
S3_SECRET_ACCESS_KEY: $S3_SECRET_ACCESS_KEY  
INTERVIEW_USERNAME: $INTERVIEW_USERNAME  
INTERVIEW_PASSWORD: $INTERVIEW_PASSWORD  
SENTRY_ENVIRONMENT: $SENTRY_ENVIRONMENT  
SENTRY_DSN: $SENTRY_DSN  
DRIVE_PRIVATE_KEY_ID: $DRIVE_PRIVATE_KEY_ID  
INTERVIEW_API_URL: $INTERVIEW_API_URL  
APP_DATABASE_HOST: $APP_DATABASE_HOST  
INTERVIEW_URL: $INTERVIEW_URL

develop:  
- step:  
name: Deployment to Staiging  
deployment: Staging  
script:  
- echo "$SSH_PRIVATE_KEY" \| tr -d '\\r' \> ssh-key  
- ssh-keyscan $SERVER \>\> \~/.ssh/known_hosts  
- scp -r -i ssh-key \* $USER@$SERVER:$BASE_PATH  
- ssh -i ssh-key $USER@$SERVER "chmod +x /home/devops/projects/pimentarecruiting/backend-nodejs/deploy/bitbucket-deploy.sh"  
- ssh -i ssh-key $USER@$SERVER "/home/devops/projects/pimentarecruiting/backend-nodejs/deploy/bitbucket-deploy.sh"  
- echo "Finished staging deployment!"  
environment:  
SSH_PRIVATE_KEY: $SSH_PRIVATE_KEY  
USER: $USER  
SERVER: $SERVER  
BASE_PATH: $BASE_PATH  
SONAR_HOST: $SONAR_HOST  
SONAR_PROJECT_KEY: $SONAR_PROJECT_KEY  
SONAR_TOKEN: $SONAR_TOKEN  
S3_BUCKET: $S3_BUCKET  
S3_ACCESS_KEY_ID: $S3_ACCESS_KEY_ID  
S3_SECRET_ACCESS_KEY: $S3_SECRET_ACCESS_KEY  
INTERVIEW_USERNAME: $INTERVIEW_USERNAME  
INTERVIEW_PASSWORD: $INTERVIEW_PASSWORD  
SENTRY_ENVIRONMENT: $SENTRY_ENVIRONMENT  
SENTRY_DSN: $SENTRY_DSN  
DRIVE_PRIVATE_KEY_ID: $DRIVE_PRIVATE_KEY_ID  
INTERVIEW_API_URL: $INTERVIEW_API_URL  
APP_DATABASE_HOST: $APP_DATABASE_HOST  
INTERVIEW_URL: $INTERVIEW_URL

master:  
- step:  
name: Deploy to Production  
deployment: production  
script:  
- echo "$SSH_PRIVATE_KEY" \| tr -d '\\r' \> ssh-key  
- chmod 600 ssh-key  
- ssh-keyscan $SERVER \>\> \~/.ssh/known_hosts  
- scp -r -i ssh-key \* $USER@$SERVER:$BASE_PATH  
- ssh -i ssh-key $USER@$SERVER "chmod +x /home/devops/projects/pimentarecruiting/backend-nodejs/backend-nodejs/deploy/bitbucket-deploy.sh"  
- ssh -i ssh-key $USER@$SERVER "/home/devops/projects/pimentarecruiting/backend-nodejs/deploy/bitbucket-deploy.sh"  
- echo "Finished production deployment!"  
environment:  
SSH_PRIVATE_KEY: $SSH_PRIVATE_KEY  
USER: $USER  
SERVER: $SERVER  
BASE_PATH: $BASE_PATH  
SONAR_HOST: $SONAR_HOST  
SONAR_PROJECT_KEY: $SONAR_PROJECT_KEY  
SONAR_TOKEN: $SONAR_TOKEN  
S3_BUCKET: $S3_BUCKET  
S3_ACCESS_KEY_ID: $S3_ACCESS_KEY_ID  
S3_SECRET_ACCESS_KEY: $S3_SECRET_ACCESS_KEY  
INTERVIEW_USERNAME: $INTERVIEW_USERNAME  
INTERVIEW_PASSWORD: $INTERVIEW_PASSWORD  
SENTRY_ENVIRONMENT: $SENTRY_ENVIRONMENT  
SENTRY_DSN: $SENTRY_DSN  
DRIVE_PRIVATE_KEY_ID: $DRIVE_PRIVATE_KEY_ID  
INTERVIEW_API_URL: $INTERVIEW_API_URL  
APP_DATABASE_HOST: $APP_DATABASE_HOST  
INTERVIEW_URL: $INTERVIEW_URL  
2) #bitbucket-deploy.sh  
#!/bin/bash  
# echo environment variables

cd /home/devops/projects/pimentarecruiting/backend-nodejs

ENVIRONMENT_VARIABLES=('SERVER' 'USER' 'SSH_PRIVATE_KEY' 'BASE_PATH' 'APP_DATABASE_HOST' 'SONAR_HOST' 'SONAR_PROJECT_KEY' 'SONAR_TOKEN' 'SENTRY_ENVIRONMENT' 'SENTRY_DSN' 'S3_BUCKET' 'S3_ACCESS_KEY_ID' 'S3_SECRET_ACCESS_KEY' 'INTERVIEW_API_URL' 'INTERVIEW_URL' 'INTERVIEW_USERNAME' 'INTERVIEW_PASSWORD' 'DRIVE_PRIVATE_KEY_ID' 'DRIVE_PRIVATE_KEY' 'DRIVE_CLIENT_EMAIL' 'DRIVE_CLIENT_ID' 'DRIVE_CLIENT_X509_CERT_URL')

<br />

echo $BASE_PATH \> test.txt  
echo $SONAR_HOST \>\> test.txt  
echo $USER \>\> test.txt  
echo $APP_DATABASE_HOST \>\> test.txt

echo "SSH_PRIVATE_KEY"=$SSH_PRIVATE_KEY \> .env  
echo "USER"=$USER \>\> .env  
echo "SERVER"=$SERVER \>\> .env  
echo "BASE_PATH"=$BASE_PATH \>\> .env  
echo "APP_DATABASE_HOST"=$APP_DATABASE_HOST \>\> .env  
echo "SONAR_HOST"=$SONAR_HOST \>\> .env  
echo "SONAR_PROJECT_KEY"=$SONAR_PROJECT_KEY \>\> .env  
echo "SONAR_TOKEN"=$SONAR_TOKEN \>\> .env  
echo "SENTRY_ENVIRONMENT"=$SENTRY_ENVIRONMENT \>\> .env  
echo "SENTRY_DSN"=$SENTRY_DSN \>\> .env  
echo "S3_BUCKET"=$S3_BUCKET \>\> .env  
echo "S3_ACCESS_KEY_ID"=$S3_ACCESS_KEY_ID \>\> .env  
echo "S3_SECRET_ACCESS_KEY"=$S3_SECRET_ACCESS_KEY \>\> .env  
echo "INTERVIEW_API_URL"=$INTERVIEW_API_URL \>\> .env  
echo "INTERVIEW_URL"=$INTERVIEW_URL \>\> .env  
echo "INTERVIEW_USERNAME"=$INTERVIEW_USERNAME \>\> .env  
echo "INTERVIEW_PASSWORD"=$INTERVIEW_PASSWORD \>\> .env  
echo "DRIVE_PRIVATE_KEY"=$DRIVE_PRIVATE_KEY \>\> .env  
echo "DRIVE_CLIENT_EMAIL"=$DRIVE_CLIENT_EMAIL \>\> .env  
echo "DRIVE_CLIENT_ID"=$DRIVE_CLIENT_ID \>\> .env  
echo "DRIVE_CLIENT_X509_CERT_URL"=$DRIVE_CLIENT_X509_CERT_URL \>\> .env  
echo "DRIVE_PRIVATE_KEY_ID"=$DRIVE_PRIVATE_KEY_ID \>\> .env

<br />

#for item in ${ENVIRONMENT_VARIABLES\[@\]}; do echo "$item=${!item}" \>\> .env;done

echo "Turning docker down... Rebuilding image and turning ON!"

docker-compose down \&\& \\  
docker-compose up -d --build \&\& \\  
docker-compose restart web \&\& \\  
history -c  
3) after deploy, I check at my VPS and the .env is missing values:  
cat .env  
SSH_PRIVATE_KEY=  
USER=devops  
SERVER=  
BASE_PATH=  
APP_DATABASE_HOST=  
SONAR_HOST=  
SONAR_PROJECT_KEY=  
SONAR_TOKEN=  
SENTRY_ENVIRONMENT=  
SENTRY_DSN=  
S3_BUCKET=  
S3_ACCESS_KEY_ID=  
S3_SECRET_ACCESS_KEY=  
INTERVIEW_API_URL=  
INTERVIEW_URL=  
INTERVIEW_USERNAME=  
INTERVIEW_PASSWORD=  
DRIVE_PRIVATE_KEY=  
DRIVE_CLIENT_EMAIL=  
DRIVE_CLIENT_ID=  
DRIVE_CLIENT_X509_CERT_URL=  
DRIVE_PRIVATE_KEY_ID=  


Answers:
author: Ricardo Souza
body: Any help?


Tags: bitbucket-cloud

cloud