Oks*_*iuk 5 continuous-integration gitlab
一个多星期以来,我一直在努力在 GitLab CI 的多项目管道中的作业之间传递变量,并遇到了很多奇怪的错误。该机制看起来非常基本,它让我发疯,这样一个明显的事情仍然对我不起作用,如果有人遇到类似的问题 - 我将感谢你的帮助!
所以我一直在尝试做的事情:我在 gitlab 上有两个项目,我试图将它们链接到一个多项目管道中,作业模式如下所示: 在项目 A 中:
variables: BUILD_PATH:""
build:
script:
- $BUILD_PATH="some-path" #the important point here that this value sets inside the job, it's not static
bridge:
variables:
PATH: $BUILD_PATH
RUN_TYPE: test #this value is a static and it passes correctly, no issues here
trigger:
project: project-B-path
Run Code Online (Sandbox Code Playgroud)
在项目B中:
variables:
PATH: ""
RUN_TYPE: ""
test:
script:
echo "From upstream pipeline dynamic: $PATH"
echo "From upstream pipeline static: $RUN_TYPE"
...
Run Code Online (Sandbox Code Playgroud)
当我在 CI 上运行它时,我正确传递了 $RUN_TYPE 变量,并且 $PATH 变量中的值为空(尽管 $BUILD_PATH 在构建作业运行期间具有正确的值)。尝试了很多方法 - 在脚本之前设置 $BUILD_PATH 值,将环境值(如 CI_JOB_ID)传递给项目 B 中的作业,根本不在项目 B 中创建此变量,等等。没有任何帮助,动态变量始终具有空值。
然后我尝试将动态变量 $BUILD_PATH 保存在 .env 文件中并将其发布为 artefact,以便桥接作业可以从那里读取它。我是这样做的:
build:
script:
- some code here
- echo "BUILD_VERSION=hello" >> vars.env
artifacts:
reports:
dotenv: vars.env
Run Code Online (Sandbox Code Playgroud)
当我在 CI 作业上运行它时总是失败并出现以下错误:
Uploading artifacts...
825vars.env: found 1 matching files and directories
826WARNING: Failed to load system CertPool: crypto/x509: system root pool is not available on Windows
827WARNING: Uploading artifacts as "dotenv" to coordinator... failed id=1877748 responseStatus=500 Internal Server Error status=500 token=some-token-here
828WARNING: Retrying... context=artifacts-uploader error=invalid argument
829WARNING: Uploading artifacts as "dotenv" to coordinator... failed id=1877748 responseStatus=500 Internal Server Error status=500 token=some-token-here
830WARNING: Retrying... context=artifacts-uploader error=invalid argument
831WARNING: Uploading artifacts as "dotenv" to coordinator... failed id=1877748 responseStatus=500 Internal Server Error status=500 token=some-token-here
832FATAL: invalid argument
Run Code Online (Sandbox Code Playgroud)
我还尝试上传没有名称的 .env 文件,就像我在某处看到的那样
Uploading artifacts...
825vars.env: found 1 matching files and directories
826WARNING: Failed to load system CertPool: crypto/x509: system root pool is not available on Windows
827WARNING: Uploading artifacts as "dotenv" to coordinator... failed id=1877748 responseStatus=500 Internal Server Error status=500 token=some-token-here
828WARNING: Retrying... context=artifacts-uploader error=invalid argument
829WARNING: Uploading artifacts as "dotenv" to coordinator... failed id=1877748 responseStatus=500 Internal Server Error status=500 token=some-token-here
830WARNING: Retrying... context=artifacts-uploader error=invalid argument
831WARNING: Uploading artifacts as "dotenv" to coordinator... failed id=1877748 responseStatus=500 Internal Server Error status=500 token=some-token-here
832FATAL: invalid argument
Run Code Online (Sandbox Code Playgroud)
但还是没有运气,同样的 500 错误。我一直在研究这个错误,但到目前为止 - 它就在我身边。
所以重点是——在多项目管道中将变量传递到下游管道的方法都不适合我。如果有人遇到相同的问题或以不同的方式使其工作 - 请帮助
更新:以不同的方式解决了这个问题 - 使用项目 A 中的 cUrl 触发器,例如:
- echo "BUILD_VERSION=hello" >> .env
Run Code Online (Sandbox Code Playgroud)
我能够通过使用 build.env 来做到这一点
上游项目:
stages:
- build
- deploy
build_job:
stage: build
script:
- echo var="defined in the job" >> $CI_PROJECT_DIR/build.env
artifacts:
reports:
dotenv: build.env
trigger:
stage: deploy
trigger:
project: path/to/downstream
Run Code Online (Sandbox Code Playgroud)
下游项目:
downstream:
script:
- echo $var
needs:
- project: path/to/upstream
job: build_job
Run Code Online (Sandbox Code Playgroud)
作为奖励,您还可以通过将此块添加到作业中来传递上游项目的路径和分支trigger:
variables:
UPSTREAM_REF_NAME: $CI_COMMIT_REF_NAME
UPSTREAM_PROJECT_PATH: $CI_PROJECT_PATH
Run Code Online (Sandbox Code Playgroud)
然后将下游项目的needs块更改为
needs:
- project: $UPSTREAM_PROJECT_PATH
ref: $UPSTREAM_REF_NAME
job: buildinfo
Run Code Online (Sandbox Code Playgroud)
needs:您的下游项目作业需要在上游项目作业上声明。
上游项目:
build_vars:
stage: build
script:
- echo "BUILD_VERSION=hello" >> build.env
artifacts:
reports:
dotenv: build.env
deploy:
stage: deploy
trigger: my/downstream_project
Run Code Online (Sandbox Code Playgroud)
下游项目:
test:
stage: test
script:
- echo $BUILD_VERSION
needs:
- project: my/upstream_project
job: build_vars
ref: main
artifacts: true
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9594 次 |
| 最近记录: |