詹金斯如何在我的远程shell脚本中传递GIT_BRANCH参数

时间:2019-07-05 14:08:58

标签: bash shell jenkins

实际上,在构建后的操作下,我的shell脚本中需要GIT_BRANCH参数,该脚本将在我的远程ssh机器上运行。我有echo GIT_BRANCH参数,但是它为空。脚本

#!/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

1 个答案:

答案 0 :(得分:0)

您知道远程计算机上未设置GIT_BRANCH,因此需要将其作为脚本的参数传递。 脚本:

#!/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

并像下面一样运行

ssh ${remote_address} ${script_path} $GIT_BRANCH

对于Ex,远程地址是10.0.0.1,脚本路径是/var/lib/test.sh,

ssh 10.0.0.1 /var/lib/test.sh $GIT_BRANCH