使用shell脚本

时间:2017-04-12 14:17:47

标签: bash macos jenkins

起始情况

我有一个Jenkins构建项目,我通过调用构建脚本(./jenkins.sh)来完成几乎所有事情。我正在构建Cordova项目,该项目依赖于某些版本的Node和Xcode。我使用最新的MacOS Sierra在Mac上运行构建。

到目前为止,我使用EnvInject插件(https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin)在Jenkins Build中设置环境变量:

Injecting environment variables into Jenkins

目标

我希望环境变量也由构建脚本而不是Jenkins Build设置。这样环境变量也在版本控制中,我不必触及Jenkins Build本身。

基本上我需要用bash重建EnvInject插件的逻辑。

我尝试过#1

在我的jenkins.sh构建脚本中,我使用export

设置了环境变量

jenkins.sh:

#!/bin/bash -ve

nodeVersion=7.7.8
xcodeVersion=8.3.1
androidSDKVersion=21.1.2

export DEVELOPER_DIR=/Applications/Xcode_${xcodeVersion}.app/Contents/Developer
export ANDROID_HOME=/Applications/adt/sdk
export PATH=/usr/local/Cellar/node/${nodeVersion}/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/usr/local/bin:/Applications/adt/sdk/tools:/usr/local/bin/:/Applications/adt/sdk/build-tools/${androidSDKVersion}:$PATH

# print info
echo ""
echo "Building with environment Variables"
echo ""
echo "  DEVELOPER_DIR:  $DEVELOPER_DIR"
echo "  ANDROID_HOME:   $ANDROID_HOME"
echo "  PATH:           $PATH"
echo "  node:           $(node -v)"
echo ""

这会产生:

Building with environment Variables

  DEVELOPER_DIR:  /Applications/Xcode_8.3.1.app/Contents/Developer
  ANDROID_HOME:   /Applications/adt/sdk
  PATH:           /usr/local/Cellar/node/7.7.8/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/usr/local/bin:/Applications/adt/sdk/tools:/usr/local/bin/:/Applications/adt/sdk/build-tools/21.1.2:/Users/mles/.fastlane/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin
node -v
  node:           v0.10.48

PATHDEVELOPER_DIRANDROID_HOME似乎设置正确,但它仍然使用节点v0.10.48的系统版本而不是{{1中设置的v7.7.8 }}。

我尝试了什么#2

我采购了变量:

jenkins.sh:

PATH

config.sh

#!/bin/bash -ve

source config.sh

# print info
echo ""
echo "Building with environment Variables"
echo ""
echo "  DEVELOPER_DIR:  $DEVELOPER_DIR"
echo "  ANDROID_HOME:   $ANDROID_HOME"
echo "  PATH:           $PATH"
echo "  node:           $(node -v)"
echo ""

结果与#!/bin/bash -ve # environment variables nodeVersion=7.7.8 xcodeVersion=8.3.1 androidSDKVersion=21.1.2 export DEVELOPER_DIR=/Applications/Xcode_${xcodeVersion}.app/Contents/Developer export ANDROID_HOME=/Applications/adt/sdk export PATH=/usr/local/Cellar/node/${nodeVersion}/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin:/usr/local/bin:/Applications/adt/sdk/tools:/usr/local/bin/:/Applications/adt/sdk/build-tools/${androidSDKVersion}:$PATH 中的结果相同:仍然使用系统节点v0.10.48而不是节点v7.7.8

问题

如何正确设置What I've tried #1PATHDEVELOPER_DIR环境变量才能在构建脚本中使用?

@tripleee 在我通过调用ANDROID_HOME确定节点之上。在构建脚本中,我运行gulp,触发Ionic / Apache Cordova。 node: $(node -v)周围的括号是否会启动一个子shell,它有自己的环境变量?

@Jacob 我们之前使用过nvm,但我们希望减少依赖性。使用nvm需要在所有构建计算机上安装nvm。我们有一个用brew安装节点的标准。这就是我使用node -v作为节点路径的原因。

@Christopher Stobie

ENV:

/usr/local/Cellar/node/${nodeVersion}

别名:

jenkins@jenkins:~$ env
MANPATH=/Users/jenkins/.nvm/versions/node/v6.4.0/share/man:/usr/local/share/man:/usr/share/man:/Users/jenkins/.rvm/man:/Applications/Xcode_7.2.app/Contents/Developer/usr/share/man:/Applications/Xcode_7.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/share/man
rvm_bin_path=/Users/jenkins/.rvm/bin
NVM_CD_FLAGS=
TERM=xterm-256color
SHELL=/bin/bash
TMPDIR=/var/folders/t0/h77w7t2s1fx5mdnsp8b5s6y00000gn/T/
SSH_CLIENT=**.**.*.** ***** **
NVM_PATH=/Users/jenkins/.nvm/versions/node/v6.4.0/lib/node
SSH_TTY=/dev/ttys000
LC_ALL=en_US.UTF-8
NVM_DIR=/Users/jenkins/.nvm
rvm_stored_umask=0022
USER=jenkins
_system_type=Darwin
rvm_path=/Users/jenkins/.rvm
rvm_prefix=/Users/jenkins
MAIL=/var/mail/jenkins
PATH=/Users/jenkins/.nvm/versions/node/v6.4.0/bin:/Users/jenkins/.fastlane/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/jenkins/.rvm/bin:/Users/jenkins/tools/oclint/bin:/Applications/adt/sdk/tools:/Applications/adt/sdk/platform-tools:/Applications/adt/sdk/build-tools/android-4.4:/Users/jenkins/.rvm/bin
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
rvm_loaded_flag=1
PWD=/Users/jenkins
LANG=en_US.UTF-8
_system_arch=x86_64
_system_version=10.12
rvm_version=1.26.10 (latest)
SHLVL=1
HOME=/Users/jenkins
LS_OPTIONS=--human --color=always
LOGNAME=jenkins
SSH_CONNECTION=**.**.*.** ***** **.**.*.** **
NVM_BIN=/Users/jenkins/.nvm/versions/node/v6.4.0/bin
NVM_IOJS_ORG_MIRROR=https://iojs.org/dist
rvm_user_install_flag=1
_system_name=OSX
_=/usr/bin/env

1 个答案:

答案 0 :(得分:3)

这看起来不像环境变量问题。它看起来像一个权限问题。执行脚本的用户是:

  • 无法读取/usr/local/Cellar/node/7.7.8/bin目录或
  • 无法从该目录读取节点可执行文件,或
  • 无法从该目录执行节点可执行文件

为了测试,在计算机上成为该用户并针对完整路径执行node命令:

/usr/local/Cellar/node/7.7.8/bin/node -v

或者,如果需要,可以更改脚本以避免使用PATH查找(我建议仅用于诊断,而不是作为解决方案):

echo "  node:           $(/usr/local/Cellar/node/7.7.8/bin/node -v)"

如果您仍处于亏损状态,请尝试以下行:

echo "  node:           $(sh -c 'echo $PATH'; which node)"