代码之家  ›  专栏  ›  技术社区  ›  Laser

Gitlab CI变量,带引号的选项

  •  1
  • Laser  · 技术社区  · 6 年前

    我有以下内容 gitab-ci.yml file :

    stages:
      - tests
    
    .test: &test_job
        image:
          name: test.com/test:latest
          entrypoint: [""]
        script:
        - py.test /test  -v $EXTRA_OPTIONS 
    
    testing:
      variables:
        EXTRA_OPTIONS: -m "not slow"
      <<: *test_job
      stage: tests
    

    我想通过选项来运行pytest,如下所示:
    py.test /tests -v -m "not slow"
    为了避免运行缓慢的测试,但Gitlab正在尝试转义引号。
    我有点像: py.test /tests -v -m '"not\' 'slow"'

    是否可以创建一个没有转义的内联变量?
    我只找到了它 this link 但这没有帮助。

    1 回复  |  直到 6 年前
        1
  •  0
  •   hoefling    6 年前

    首先,要避免变量中的空格转义,请使用单引号:

    variables:
        EXTRA_OPTIONS: -m 'not slow'
    

    要应用变量,有两个选项:

    1. 使用 addopts 结合 -o . 奥多普斯 是一个inifile密钥, enables you to persist command line args in pytest.ini . 这个 -o/--override-ini arg允许重写inifile值,包括 奥多普斯 . 两者的结合是通过环境变量传递命令行参数的绝妙技巧:

      script:
        - pytest -v -o "addopts=$EXTRA_OPTIONS" /test
      
    2. 使用 eval :

      script:
        - eval pytest -v "$EXTRA_OPTIONS" /test
      

      但是,使用时要非常小心 埃瓦 Why should eval be avoided in Bash, and what should I use instead? . 因此,我更喜欢第一种选择。