的出發情況進樣環境變量爲詹金斯與shell腳本
建設過程中,我有一個詹金斯建設項目中,我通過調用我的構建腳本(./jenkins.sh
)做幾乎所有的東西。我正在構建一個Cordova項目,它依賴於某些版本的Node和Xcode。我正在使用最新的MacOS Sierra運行Mac上的內部版本。
到目前爲止我設置環境變量在詹金斯與EnvInject插件版本(https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin):
目標
我想有環境變量也由構建腳本而不是在Jenkins構建中設置。這樣環境變量也在版本控制中,我不必碰觸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
PATH
,DEVELOPER_DIR
ANDROID_HOME
似乎設置正確,但它仍然使用系統版本的節點v0.10.48而不是v7.7.8在PATH
中設置。
我已經試過#2
我採購的變量:
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
結果是一樣What I've tried #1
:還是用系統節點v0.10.48而不是節點v7.7.8
問題
我怎樣才能正確設置PATH
,DEVELOPER_DIR
,ANDROID_HOME
環境變量只能在構建腳本中使用?
@tripleee 上面我通過調用node: $(node -v)
來確定節點。在構建腳本中,我正在運行觸發Ionic/Apache Cordova的gulp。圍繞node -v
的括號是否會啓動一個包含它自己的環境變量的子shell?
@Jacob 我們之前使用過nvm,但我們希望擁有更少的依賴關係。使用nvm需要在所有構建機器上安裝nvm。我們有一個用brew來安裝節點的標準。這就是爲什麼我使用/usr/local/Cellar/node/${nodeVersion}
作爲節點的路徑。
@Christopher斯托比
ENV:
[email protected]:~$ 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
別名:
[email protected]:~$ alias
alias l='ls -lAh'
alias rvm-restart='rvm_reload_flag=1 source '\''/Users/jenkins/.rvm/scripts/rvm'\'''
'buildApp()'不是用於調用函數的有效shell語法。沒有什麼比這更平凡。 – chepner
您是否正在執行腳本(而不是源代碼)並希望在腳本退出後將值保留在當前shell中?因爲,不,那不行。你*不能*影響子進程中進程的環境;您必須改爲編寫腳本。 – chepner
我更正了buildApp調用。這只是爲了表明在導出後實際的構建過程開始。 如果可能,我想避免有我需要的單獨文件來源。將它全部放在我的shell腳本('jenkins.sh')中將會很棒。 – mles