1

actually I need GIT_BRANCH parameter in my shell script under post build action and that script will run on my remote ssh machine I have echo GIT_BRANCH parameter but it is empty can anyone help how to I will pass my environment variable in my remote shell script

#!/bin/bash

echo "Branch Name=>" $GIT_BRANCH

if [ $GIT_BRANCH == 'origin/master' ] then   
   DOCKERFILE_NAME='Dockerfile.master'    docker build -f
   $DOCKERFILE_NAME -t master_image:v1 .
elif [ $GIT_BRANCH=='origin/development' ] then   
   DOCKERFILE_NAME='Dockerfile.development'    docker build -f
   $DOCKERFILE_NAME -t development_image:v1 .
else
   echo "branch cannot be handle"
fi

echo "DOCKERFILE NAME:"$DOCKERFILE_NAME
joanis
  • 10,635
  • 14
  • 30
  • 40
Niaz Ahmad
  • 11
  • 5
  • if you connect to the remote server via ssh, you can append some env variables as well - this needs some changes to the ssh server configuration. See https://stackoverflow.com/questions/4409951/can-i-forward-env-variables-over-ssh if you're using the native ssh you can do something like this then ssh user@host "GIT_BRANCH=${GIT_BRANCH}". Not tried yer, but could work. Otherwise please update your question and give some more details about the build. – Manuel Jul 05 '19 at 14:37

1 Answers1

0

As you know GIT_BRANCH is not set on your remote machine, you need to pass it as an parameter of the script. Script:

#!/bin/bash
GIT_BRANCH="$1"
echo "Branch Name=>" $GIT_BRANCH

if [ $GIT_BRANCH == 'origin/master' ] then   
   DOCKERFILE_NAME='Dockerfile.master'    docker build -f
   $DOCKERFILE_NAME -t master_image:v1 .
elif [ $GIT_BRANCH=='origin/development' ] then   
   DOCKERFILE_NAME='Dockerfile.development'    docker build -f
   $DOCKERFILE_NAME -t development_image:v1 .
else
   echo "branch cannot be handle"
fi

echo "DOCKERFILE NAME:"$DOCKERFILE_NAME

and run it like below

ssh ${remote_address} ${script_path} $GIT_BRANCH

for Ex, remote address is 10.0.0.1 and script path is /var/lib/test.sh,

ssh 10.0.0.1 /var/lib/test.sh $GIT_BRANCH
RyanKim
  • 1,557
  • 9
  • 10