我注意到我可以在一个 Gitlab CI 作业中访问和导出文件中的值,但不能从其他作业中访问和导出值,即使它显然位于目录中。
这是我的 gitlab-ci.yml 的一部分:
.terraform:
extends: .base
image: registry.gitlab.com/gitlab-org/terraform-images/stable:latest
before_script:
- pwd
- ls
- if [ -f build.env ] ; then set -o allexport && source build.env && set +o allexport ; fi
- env | sort
...
.docker-build:
extends: .base
stage: deploy
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
before_script:
- pwd
- ls
- if [ -f build.env ] ; then set -o allexport && source build.env && set +o allexport ; fi
我的 .terraform 扩展作业可以很好地使用 build.env :
Executing "step_script" stage of the job script
00:23
$ pwd
/builds/project/prototypes-cicd
$ ls
Dockerfile
LICENCE.txt
Procfile
README.md
VERSION.txt
app
build.env
docs
lib
listen-on-port.js
package-lock.json
package.json
public
scripts
server.js
start.js
terraform
usage-data-config.json
$ if [ -f build.env ] ; then set -o allexport && source build.env && set +o allexport ; fi
$ env | sort
但是 .docker-build 扩展作业不能:
Executing "step_script" stage of the job script
00:00
$ pwd
/builds/project/prototypes-cicd
$ ls
Dockerfile
LICENCE.txt
Procfile
README.md
VERSION.txt
app
build.env
docs
ecr_token.env
lib
listen-on-port.js
package-lock.json
package.json
public
scripts
server.js
start.js
terraform
usage-data-config.json
$ if [ -f build.env ] ; then set -o allexport && source build.env && set +o allexport ; fi
/scripts-89568-12282862/step_script: source: line 181: build.env: not found
我尝试了完全不同的 .env 文件并得到了相同的结果
这个我不太懂。
原因是
gcr.io/kaniko-project/executor:debug
图像正在使用busybox,它具有超级有限的shell二进制文件。
$ docker run --entrypoint="" -it --rm gcr.io/kaniko-project/executor:debug sh
/workspace # which sh
/busybox/sh
/workspace # cat > a.sh << EOF
> echo 123
> EOF
/workspace # chmod +x a.sh
/workspace # ./a.sh
123
/workspace # if [ -f a.sh ]; then source a.sh; fi
sh: source: a.sh: not found
/workspace # source a.sh
sh: source: a.sh: not found
您有两个选择: