【发布时间】:2021-09-02 10:30:48
【问题描述】:
我正在为我的项目实施 gitlab-ci.yml。在这个 yml 文件中,我需要执行一个 script.py 文件。这个 script.py 位于不同的项目中,无论如何要包含这个 python 脚本而不将它上传到我的项目中。
类似:“包括:'https://gitlab.com/khalilazennoud3//-/blob/main/script.py'
【问题讨论】:
我正在为我的项目实施 gitlab-ci.yml。在这个 yml 文件中,我需要执行一个 script.py 文件。这个 script.py 位于不同的项目中,无论如何要包含这个 python 脚本而不将它上传到我的项目中。
类似:“包括:'https://gitlab.com/khalilazennoud3//-/blob/main/script.py'
【问题讨论】:
没有办法“包含”不是管道定义模板的文件,但您仍然可以获取该文件。我这样做的方法是在前一阶段添加第二个管道作业以克隆其他存储库,然后上传您需要的文件作为工件。然后在您需要该文件的作业中,它将提供可用的工件。
这是一个仅包含这两个作业的示例管道:
stages:
- "Setup other Project Files" # or whatever
- Build
Grab Python Script from Other Repo:
stage: "Setup other Project Files"
image: gitscm/git
variables:
GIT_STRATEGY: none
script:
- git clone git@gitlab.example.com:user/project.git
artifacts:
paths:
- path/to/script.py.
when: on_success # since if the clone fails, there's nothing to upload
expire_in: 1 week # or whatever makes sense
Build Job:
stage: Build
image: python
dependencies: ['Grab Python Script from Other Repo']
script:
- ls -la # this will show `script.py` from the first step along with the contents of "this" project where the pipeline is running
- ./do_something_with_the_file.sh
让我们逐行浏览这些内容。第一份工作:
git
GIT_STRATEGY: none 变量告诉 Gitlab Runner 不要克隆/获取管道正在运行的项目。如果工作正在执行诸如向 Slack 发送通知、访问另一个 API 等操作,这将非常有用。第二份工作:
dependencies 关键字控制之前阶段的哪些工件将是 1) 需要和 2) 为该特定作业下载的。默认情况下,会为所有作业下载所有可用的工件。这个关键字控制,因为我们只需要script.py 文件。【讨论】: