python - Gitlab-ci:如何使用 cl 参数运行 python 脚本

标签 python bash yaml command-line-arguments gitlab-ci

我想在实际构建之前运行一个需要 cl 参数的 python 脚本,所以我在 .gitlab-ci.yml 中添加了以下部分:

.deploy_template: &template_name
  variables:
  - arg1: "https://some/url/"
  - arg2: "https://another/url/"
  - arg3: "https://one/more/url/"
  script:
  - python3 some/script/file.py $arg1 $arg2 $arg3

但我收到以下错误:

usage: file.py [-h] arg1 arg2 arg3

file.py: error: the following arguments are required: arg1 arg2 arg3

如果参数只是字符串(即不是变量),那么它工作正常,但它不会读取变量 $arg1 等。

在 gitlab 文档中提到,我可以使用 bash 语法来提供变量,因此 $arg1 必须是正确的引用。

我使用 argparse在脚本中获取 cl 参数。

调用 gitlab-ci.yml 中的变量作为 python 脚本参数的正确方法是什么?

最佳答案

您的变量 是一个列表,但它需要一个字典。只需从中删除 -

.deploy_template: &template_name
  variables:
    arg1: "https://some/url/"
    arg2: "https://another/url/"
    arg3: "https://one/more/url/"
  script:
  - python3 some/script/file.py $arg1 $arg2 $arg3

More about yaml syntax

关于python - Gitlab-ci:如何使用 cl 参数运行 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46526769/

相关文章:

python - 无法检测到该元素并通过 Selenium 将输入发送到信用卡号码字段

python - Boost.Python + OpenGL 段错误

linux - 在脚本中执行 cqlsh

amazon-web-services - 使用 cloudformation (yaml) 在 Ubuntu 实例中启动 cfn-init

python - Python 中的 JSON 到 YAML : How to get correct string manipulation?

python - 根据条件对数据帧进行排序,并通过动态分配给随机向量来返回所有组

python - 如何有效地确定给定值所在的区间?

Bash双方括号正则表达式匹配问题

linux - 将文件拆分为 HDFS 中的小文件

ruby - Ruby 的 YAML 模块可以用来嵌入评论吗?