bash 如何在 Gitlab CI 的构建脚本中设置(环境)变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19359905/
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
How to set an (environment) variable in the build script in Gitlab CI?
提问by static
I have a dummy build script in the Gitlab CI
:
我有一个虚拟构建脚本Gitlab CI
:
pwd
ci_app_path=$(pwd)
echo "INFO: current directory: $ci_app_path"
and I get this output when the system start the build process:
当系统开始构建过程时,我得到了这个输出:
pwd
/home/kai/gitlab-runners/gitlab-ci-runner/tmp/builds/project-1
ci_app_path=$(pwd)
echo "INFO: current directory: $ci_app_path"
INFO: current directory:
so the variable was not set (or was set only for that line: as I know each line executed separately)
所以未设置变量(或仅为该行设置:据我所知,每行单独执行)
I heard about push/pop mechanism to reach the functionality I desired, but could not find any details, how to implement that.
我听说过推送/弹出机制来达到我想要的功能,但找不到任何细节,如何实现它。
Update:
更新:
as I thought, each line is going executed separately. So the variable scope is only one line, where it is defined:
正如我所想,每一行都将单独执行。所以变量作用域只有一行,它的定义是:
script:
脚本:
pwd
ci_app_path=$(pwd) && echo "INFO: current directory: $ci_app_path"
output:
输出:
pwd
/home/devuser/gitlab-runners/gitlab-ci-runner/tmp/builds/project-1
ci_app_path=$(pwd) && echo "INFO: current directory: $ci_app_path"
INFO: current directory: /home/kai/gitlab-runners/gitlab-ci-runner/tmp/builds/project-1
I don't think that writing the whole script as a one-liner is a good idea/practice.
我不认为将整个脚本写成一行代码是一个好主意/做法。
How to get the variables set while executing the build script?
如何在执行构建脚本时获取变量集?
P.S.
聚苯乙烯
actually I wonder why the whole build script must contain no empty lines?, otherwise it returns:
实际上我想知道为什么整个构建脚本不能包含空行?,否则返回:
No such file or directory
and a build fails on this place
并且在这个地方构建失败
回答by Jakub Kania
Gitlab CI just plays shell commands so do what you do in *sh:
Gitlab CI 只是播放 shell 命令,所以做你在 *sh 中所做的:
export ci_app_path=$(pwd)
echo "INFO: current directory: $ci_app_path"
回答by rhtyd
By the time Gitlab-CI folks fix it, you may create a Makefile and export variables inside it and in Gitlab-CI pass the variables, for example
到 Gitlab-CI 人员修复它时,您可以创建一个 Makefile 并在其中导出变量并在 Gitlab-CI 中传递变量,例如
PWD=$PWD make
PWD=$PWD 制作
Makefile starts with a: export GOPATH=$(PWD)
Makefile 以一个开头:export GOPATH=$(PWD)