Injecting environment variables into Jenkins build process with shell script

initial situation

I have a Jenkins build project where I do pretty much everything by calling my build script ( ./jenkins.sh

). I am building a Cordova project that depends on specific versions of Node and Xcode. I am running builds on Macs with the latest macOS Sierra.

While I am setting environment variables in Jenkins build using EnvInject Plugin ( https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin ):

Injecting environment variables in Jenkins

purpose

I want the environment variables to be set using the build script as well, not in Jenkins Build. This way the environment variables are also under version control and I don't need to touch Jenkins Build itself.

Basically I need to rebuild the EnvInject Plugin logic using bash.

What I tried # 1

In my jenkins.sh

build script, I have set environment variables withexport

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 ""

      

This gives:

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

      

PATH

, DEVELOPER_DIR

, ANDROID_HOME

Think properly configured, but instead of v7.7.8, it still uses the system version of Node v0.10.48 as set out in PATH

.

What I tried # 2

I used variables:

jenkins.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 ""

      

config.sh

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

      

The result was the same as in What I've tried #1

: still using the Node v0.10.48 system instead of Node v7.7.8

Question

How can I properly set environment variables PATH

, DEVELOPER_DIR

, ANDROID_HOME

which will only be used within the assembly script?

@tripleee Above, I define Node by calling node: $(node -v)

. In the build script, I run gulp, which runs Ionic / Apache Cordova. Do parentheses match around a node -v

subshell that has its own environment variables?

@Jacob We used nvm before, but we want fewer dependencies. Using nvm requires nvm to be installed on all build machines. We have a Node install standard with brew. This is why I am using /usr/local/Cellar/node/${nodeVersion}

node.

@ Christopher Stoby

env:

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

      

alias:

jenkins@jenkins:~$ alias
alias l='ls -lAh'
alias rvm-restart='rvm_reload_flag=1 source '\''/Users/jenkins/.rvm/scripts/rvm'\'''

      

+3


source to share


1 answer


This doesn't seem like an environment variable issue. It looks like a permissions issue. The user executing the script is either:

  • could not read directory / usr / local / Cellar / node / 7.7.8 / bin or
  • could not read the node executable from this directory, or
  • cannot execute the node executable from this directory

To test, become this user on the machine and run the node command in the full path:

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

      



or, if you need to, change the script to avoid using PATH lookups (Im suggesting this for diagnostic purposes only, not as a solution):

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

      

If you're still at a loss, try this line:

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

      

+3


source







All Articles