Travis:如何在不运行模板作业的情况下

时间:2018-09-14 15:00:48

标签: syntax yaml travis-ci dry

我目前正在研究an open source example project to run a Unity3d test, build and deploy pipeline for multiple CIs。我设法在gitlab-ci中正常工作,但现在我正在与 travis CI进行斗争。介绍足够了,这就是我目前遇到的问题:

说我有以下(简体).travis.yml

sudo: required
language: bash

jobs:
  include:
  - &test
    stage: test
    script: |
      echo "some multi-line script I don't want to repeat"
      echo "some complicated command with $FOO" | tee test_output.txt
    addons:
      artifacts:
        target_paths:
        - $(pwd)/test_output.txt
  - <<: *test
    env: FOO=1
  - <<: *test
    env: FOO=2
  - &build
    stage: build
    script: |
      echo "some other multi-line script I don't want to repeat"
      echo "some other complicated command with $BAR" | tee build_output.txt
    addons:
      artifacts:
        target_paths:
        - $(pwd)/build_output.txt
  - <<: *build
    env: BAR=a
  - <<: *build
    env: BAR=b
  - <<: *build
    env: BAR=c
  - <<: *build
    env: BAR=d

这几乎可行,这里是每个运行:

job 1: some multi-line script I don't want to repeat
job 1: some complicated command with

job 2: some multi-line script I don't want to repeat
job 2: some complicated command with 1

job 3: some multi-line script I don't want to repeat
job 3: some complicated command with 2

job 4: some other multi-line script I don't want to repeat
job 4: some other complicated command with

job 5: some other multi-line script I don't want to repeat
job 5: some other complicated command with a

job 6: some other multi-line script I don't want to repeat
job 6: some other complicated command with b

job 7: some other multi-line script I don't want to repeat
job 7: some other complicated command with c

job 8: some other multi-line script I don't want to repeat
job 8: some other complicated command with d

如您所见,作业1和作业4具有空的env变量,这是因为即使没有给出env var,模板&test也会被执行。我想摆脱上面的作业1和4,并仍然保持travis文件干燥。我不想添加一些if条件来检查是否设置了env var,因为这会在travis中创建空作业

非常不错的解决方法

我设法做到这一点的唯一方法是使用shell脚本并重复诸如artifacts之类的所有内容(我真的很难读):

test.sh

#!/usr/bin/env bash

echo "some multi-line script I don't want to repeat"
echo "some complicated command with $FOO" | tee test_output.txt

build.sh

#!/usr/bin/env bash

echo "some other multi-line script I don't want to repeat"
echo "some other complicated command with $FOO" | tee build_output.txt

.travis.yml

jobs:
  include:
  - stage: test
    script: ./test.sh
    env: FOO=1
    addons:
      artifacts:
        target_paths:
        - $(pwd)/test_output.txt
  - stage: test
    script: ./test.sh
    env: FOO=2
    addons:
      artifacts:
        target_paths:
        - $(pwd)/test_output.txt
  - stage: build
    script: ./build.sh
    env: BAR=a
    addons:
      artifacts:
        target_paths:
        - $(pwd)/build_output.txt
  - stage: build
    script: ./build.sh
    env: BAR=b
    addons:
      artifacts:
        target_paths:
        - $(pwd)/build_output.txt
  - stage: build
    script: ./build.sh
    env: BAR=c
    addons:
      artifacts:
        target_paths:
        - $(pwd)/build_output.txt
  - stage: build
    script: ./build.sh
    env: BAR=d
    addons:
      artifacts:
        target_paths:
        - $(pwd)/build_output.txt

问题是,如果不在travis CI中运行它,我们如何拥有yaml作业模板?

0 个答案:

没有答案
相关问题