我们可以在Bamboo Server的Plan configuration中通过Script section来定义一些全局变量,在用到的时候可以直接使用${bamboo.variable}的方式调用。但当我们在脚本中使用这些变量时,发现它们为空,这是因为Bamboo将变量传入执行脚本的agent时,它们并不会被自动解析并传递。因此,我们需要手动在脚本中解析这些变量。 下面是一个解析Bamboo变量的bash脚本示例:
#!/bin/bash
# Parse bamboo variables
if [ -f "/opt/atlassian/bamboo/agent/bamboo-agent-home/xml-data/build-dir/$bamboo_buildKey_AfterUpperCase/config.properties" ]; then
source /opt/atlassian/bamboo/agent/bamboo-agent-home/xml-data/build-dir/$bamboo_buildKey_AfterUpperCase/config.properties
fi
echo "The value of bamboo_variable is $bamboo_variable"
在这个脚本中,我们首先判断Bamboo变量保存的配置文件是否存在,如果存在则通过source命令来读取变量并自动赋值,然后就可以直接在脚本中使用变量了。