bash 使用 shell 脚本将环境变量注入 Jenkins 构建过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43372175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 16:01:06  来源:igfitidea点击:

Inject Environment variables into a Jenkins build process with a shell script

bashmacosjenkins

提问by mles

The starting situation

开局情况

I have a Jenkins build Project where I'm doing almost everything by calling my build script (./jenkins.sh). I'm building a Cordova Project, which is dependent on certain versions of Node and Xcode. I'm running the builds on Macs with the latest MacOS Sierra.

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

So far I'm setting the environment variables in the Jenkins Build with the EnvInject Plugin(https://wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin):

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

Injecting environment variables into Jenkins

将环境变量注入 Jenkins

The Goal

目标

I want to have the environment variables also set by the build script instead of in the Jenkins Build. This way the environment variables are also in version control and I don't have to touch the Jenkins Build itself.

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

Basically I need to rebuild the logic of the EnvInject Plugin with bash.

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

What I've tried #1

我试过的#1

Within my jenkins.shbuild script I've set the environment variables with export

在我的jenkins.sh构建脚本中,我设置了环境变量export

jenkins.sh:

詹金斯.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 yields:

这产生:

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_HOMEseems to be set correctly, however it is still using the system version of node v0.10.48 instead of v7.7.8 as set in PATH.

PATH, DEVELOPER_DIR,ANDROID_HOME似乎设置正确,但是它仍然使用节点 v0.10.48 的系统版本,而不是在 中设置的 v7.7.8 PATH

What I've tried #2

我试过的#2

I've sourced the variables:

我已经找到了变量:

jenkins.sh:

詹金斯.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 system node v0.10.48 instead of node v7.7.8

结果与以下相同What I've tried #1:仍然使用系统节点 v0.10.48 而不是节点 v7.7.8

The question

问题

How can I set the PATH, DEVELOPER_DIR, ANDROID_HOMEenvironment variables properly to be used only within the build script?

如何正确设置PATH, DEVELOPER_DIR,ANDROID_HOME环境变量以仅在构建脚本中使用?

@tripleee Above I'm determining node by calling node: $(node -v). In the build script I'm running gulp which triggers Ionic / Apache Cordova. Do the brackets around node -vstart a subshell which has it's own environment variables?

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

@Jacob We have used nvm before, but we want to have less dependencies. Using nvm requires to install nvm on all build machines. We have a standard of installing node with brew. That's why I'm using /usr/local/Cellar/node/${nodeVersion}as path to node.

@Jacob 我们之前使用过 nvm,但我们希望减少依赖。使用 nvm 需要在所有构建机器上安装 nvm。我们有一个用 brew 安装节点的标准。这就是我使用/usr/local/Cellar/node/${nodeVersion}作为节点路径的原因。

@Christopher Stobie

@克里斯托弗·斯托比

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'\'''

采纳答案by spacepickle

This doesnt look like an environment variable issue. It looks like a permissions issue. The user executing the script is either:

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

  • not able to read the /usr/local/Cellar/node/7.7.8/bin directory, or
  • not able to read the node executable from that directory, or
  • not able to execute the node executable from that directory
  • 无法读取 /usr/local/Cellar/node/7.7.8/bin 目录,或
  • 无法从该目录读取节点可执行文件,或
  • 无法从该目录执行节点可执行文件

In order to test, become that user on the machine and execute the node command against 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 diagnosis only, not as a solution):

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

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

If you are still at a loss, try this line:

如果您仍然不知所措,请尝试以下行:

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