bash - 如何在 Gitlab CI 的构建脚本中设置(环境)变量?

标签 bash build continuous-integration environment-variables gitlab

我在 Gitlab CI 中有一个虚拟构建脚本:

pwd
ci_app_path=$(pwd)
echo "INFO: current directory: $ci_app_path"

当系统开始构建过程时我得到这个输出:

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:

所以变量没有设置(或者只为那一行设置:据我所知,每一行都是单独执行的)

我听说推/弹出机制可以实现我想要的功能,但找不到任何细节,如何实现它。

更新:

如我所想,每一行都将单独执行。所以变量作用域只有一行,定义的地方:

脚本:

pwd
ci_app_path=$(pwd) && echo "INFO: current directory: $ci_app_path"

输出:

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

我不认为将整个脚本写成一行是个好主意/做法。

如何在执行构建脚本时设置变量?

附言

其实我想知道为什么整个构建脚本不能包含空行?否则返回:

No such file or directory

在这个地方构建失败

最佳答案

Gitlab CI 只执行 shell 命令,所以执行你在 *sh 中执行的操作:

export ci_app_path=$(pwd)
echo "INFO: current directory: $ci_app_path"

关于bash - 如何在 Gitlab CI 的构建脚本中设置(环境)变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19359905/

相关文章:

bash - 停止 Bash 脚本中的无限递归

linux - BASH:只有当函数被管道传输时,全局变量才在函数中不可更新(简单示例)

linux - bash 脚本中的变量未按预期工作

ios - 从命令行运行后仪器无法继续

jenkins - 从 Jenkins 中的单独作业部署构建

bash - 在 bash 脚本中运行 hadoop 命令

regex - 将目录内容打印到文件的 ant 任务

node.js - 纱在构建中找不到模块 'logform' winston

build - CMakeLists.txt 中未使用 CMAKE_BUILD_TYPE

tfs - 我可以从触发 TFS 中的 CI 构建中排除文件吗?