Gitlab:通过作业和阶段传递人工制品
Gitlab: Passing artefacts through jobs and stages
我使用 node.js mocha 和 selenium-webdriver 进行端到端测试。
我有不同的阶段,比如安装、测试按钮 1、测试按钮 2。
每个阶段(安装除外)都有 2 个作业(chrome 和 safari)。
我的目标,最后,得到 1 个 txt 文件,其中包含所有阶段的所有作业的测试结果。
我尝试了很多不同的配置:
- 始终在每个作业中传递工件 report.txt 和 运行 ./mocha >> report.txt。
- 导致文件中的数据不一致(不是所有作业写入,部分写入等)
- 总是传递 2 个工件 report1.txt、report2.txt,所以 chrome 的作业只写在 #1 中,safari 的作业写在 #2 中
- 根本不起作用,天知道为什么
- 具有 2 个工件的依赖项 report1.txt、report2.txt
- 出于某种原因,在安装阶段生成的工件 node_modules 未传递到下一阶段。
我没有这个设置的例子了,它们很乱,但我给你一个我的 .gitlab 的例子-ci.yml也许你可以帮助我。
stages:
- install
- startup
- quality
- language
- report
.chrome_template: &chrome_template
environment:
name: chrome
variables: &chrome_template_var
SELENIUM_BROWSER: chrome
tags:
- "macbook"
.safari_template: &safari_template
environment:
name: safari
variables: &safari_template_var
SELENIUM_BROWSER: safari
tags:
- "macbook"
# install job
install:
stage: install
tags:
- "macbook"
script:
- npm install
artifacts:
paths:
- node_modules/
except:
- tags
# testing jobs
.startup: &startup
stage: startup
variables: &startup-var
SPEC: startup
script:
- npm run test
retry: 1
artifacts:
when: on_failure
paths:
- screenshot
.quality: &quality
stage: quality
variables: &quality-var
SPEC: quality
script:
- npm run test
retry: 1
artifacts:
when: on_failure
paths:
- screenshot/
.language: &language
stage: language
variables: &language-var
SPEC: language
script:
- npm run test
retry: 1
artifacts:
when: on_failure
paths:
- screenshot/
startup:chrome:
<<: *startup
<<: *chrome_template
variables:
<<: *startup-var
<<: *chrome_template_var
language:chrome:
<<: *language
<<: *chrome_template
variables:
<<: *language-var
<<: *chrome_template_var
quality:chrome:
<<: *quality
<<: *chrome_template
variables:
<<: *quality-var
<<: *chrome_template_var
startup:safari:
<<: *startup
<<: *safari_template
variables:
<<: *startup-var
<<: *safari_template_var
language:safari:
<<: *language
<<: *safari_template
variables:
<<: *language-var
<<: *safari_template_var
quality:safari:
<<: *quality
<<: *safari_template
variables:
<<: *quality-var
<<: *safari_template_var
report:
stage: report
tags:
- "macbook"
script:
- cat *.txt > report.txt
artifacts:
paths:
- report.txt
您发布的文件中至少有几个可能的问题:
artifacts.name
未定义,因此将使用默认的 "artifacts" 字符串。当多个作业在同一台主机上并行运行时,这会带来一个工件文件被另一个工件文件覆盖的风险。
定义自定义 artifacts:name 以修复覆盖。
如果您将 report.txt
存储在同一个文件夹中,请在完成作业之前重命名它,这样报告文件就不会在您的 report
阶段作业中被覆盖,其中所有请求的工件档案都将解压到同一位置。
似乎 none 的作业 (启动、质量、语言) 导出 report.txt
文件。添加 report.txt
和 screenshots
文件夹以将其导出。
屏幕截图 (和报告) 仅在作业失败时才会导出。如果这不是想要的行为,请将 artifacts:when 更改为 on_success
或 always
如果其中一个测试作业失败,则整个管道都会失败。如果此管道仅用于测试和导出结果,您应该允许测试作业失败而不会使整个管道失败 allow_failure
总结上面写的所有内容,这里是建议更改的 yml 文件:
stages:
- install
- test
- report
#
# Templates
#
# Tags can't be defined on a global scope for now
# Ref: https://gitlab.com/gitlab-org/gitlab-ce/issues/23434
.default: &default
tags: ['macbook']
.node_modules:
artifacts: &node_modules
paths: ['node_modules']
when: always
.test_reports:
artifacts: &test_reports
paths:
- screenshot
- reports
when: always
.chrome_template: &chrome_template
environment:
name: chrome
variables: &chrome_template_var
SELENIUM_BROWSER: chrome
.safari_template: &safari_template
environment:
name: safari
variables: &safari_template_var
SELENIUM_BROWSER: safari
#
# Jobs
#
# Install
install:
<<: *default
stage: install
except: ['tags']
script:
- npm install
artifacts: *node_modules
# Test
.test: &test
<<: *default
stage: test
script:
- npm run test
# Move report to individual file to fix overwriting it by other artifacts
- mkdir -p reports
- mv report.txt reports/report_${CI_JOB_NAME}.txt
retry: 1
artifacts: *test_reports
# Allow tests to fail, exporting artifacts to the final stage
allow_failure: true
.test_startup: &test_startup
<<: *test
variables: &startup_var
SPEC: startup
.test_quality: &test_quality
<<: *test
variables: &quality_var
SPEC: quality
.test_language: &test_language
<<: *test
variables: &language_var
SPEC: language
test:startup:chrome:
<<: *test_startup
<<: *chrome_template
variables:
<<: *startup_var
<<: *chrome_template_var
test:language:chrome:
<<: *test_language
<<: *chrome_template
variables:
<<: *language_var
<<: *chrome_template_var
test:quality:chrome:
<<: *test_quality
<<: *chrome_template
variables:
<<: *quality_var
<<: *chrome_template_var
test:startup:safari:
<<: *test_startup
<<: *safari_template
variables:
<<: *startup_var
<<: *safari_template_var
test:language:safari:
<<: *test_language
<<: *safari_template
variables:
<<: *language_var
<<: *safari_template_var
test:quality:safari:
<<: *test_quality
<<: *safari_template
variables:
<<: *quality_var
<<: *safari_template_var
# Report
report:
<<: *default
stage: report
script:
# Summarize reports before exporting
# Ignore errors when there are no reports to export
- cat reports/*.txt > report.txt | true
artifacts:
paths:
- screenshot
- report.txt
请检查多个失败的测试作业是否不会覆盖 "screenshot" 文件夹的内容。否则,您需要以与 "report.txt" 文件相同的方式移动屏幕截图。
我使用 node.js mocha 和 selenium-webdriver 进行端到端测试。 我有不同的阶段,比如安装、测试按钮 1、测试按钮 2。 每个阶段(安装除外)都有 2 个作业(chrome 和 safari)。 我的目标,最后,得到 1 个 txt 文件,其中包含所有阶段的所有作业的测试结果。
我尝试了很多不同的配置:
- 始终在每个作业中传递工件 report.txt 和 运行 ./mocha >> report.txt。
- 导致文件中的数据不一致(不是所有作业写入,部分写入等)
- 总是传递 2 个工件 report1.txt、report2.txt,所以 chrome 的作业只写在 #1 中,safari 的作业写在 #2 中
- 根本不起作用,天知道为什么
- 具有 2 个工件的依赖项 report1.txt、report2.txt
- 出于某种原因,在安装阶段生成的工件 node_modules 未传递到下一阶段。
我没有这个设置的例子了,它们很乱,但我给你一个我的 .gitlab 的例子-ci.yml也许你可以帮助我。
stages:
- install
- startup
- quality
- language
- report
.chrome_template: &chrome_template
environment:
name: chrome
variables: &chrome_template_var
SELENIUM_BROWSER: chrome
tags:
- "macbook"
.safari_template: &safari_template
environment:
name: safari
variables: &safari_template_var
SELENIUM_BROWSER: safari
tags:
- "macbook"
# install job
install:
stage: install
tags:
- "macbook"
script:
- npm install
artifacts:
paths:
- node_modules/
except:
- tags
# testing jobs
.startup: &startup
stage: startup
variables: &startup-var
SPEC: startup
script:
- npm run test
retry: 1
artifacts:
when: on_failure
paths:
- screenshot
.quality: &quality
stage: quality
variables: &quality-var
SPEC: quality
script:
- npm run test
retry: 1
artifacts:
when: on_failure
paths:
- screenshot/
.language: &language
stage: language
variables: &language-var
SPEC: language
script:
- npm run test
retry: 1
artifacts:
when: on_failure
paths:
- screenshot/
startup:chrome:
<<: *startup
<<: *chrome_template
variables:
<<: *startup-var
<<: *chrome_template_var
language:chrome:
<<: *language
<<: *chrome_template
variables:
<<: *language-var
<<: *chrome_template_var
quality:chrome:
<<: *quality
<<: *chrome_template
variables:
<<: *quality-var
<<: *chrome_template_var
startup:safari:
<<: *startup
<<: *safari_template
variables:
<<: *startup-var
<<: *safari_template_var
language:safari:
<<: *language
<<: *safari_template
variables:
<<: *language-var
<<: *safari_template_var
quality:safari:
<<: *quality
<<: *safari_template
variables:
<<: *quality-var
<<: *safari_template_var
report:
stage: report
tags:
- "macbook"
script:
- cat *.txt > report.txt
artifacts:
paths:
- report.txt
您发布的文件中至少有几个可能的问题:
artifacts.name
未定义,因此将使用默认的 "artifacts" 字符串。当多个作业在同一台主机上并行运行时,这会带来一个工件文件被另一个工件文件覆盖的风险。 定义自定义 artifacts:name 以修复覆盖。如果您将
report.txt
存储在同一个文件夹中,请在完成作业之前重命名它,这样报告文件就不会在您的report
阶段作业中被覆盖,其中所有请求的工件档案都将解压到同一位置。似乎 none 的作业 (启动、质量、语言) 导出
report.txt
文件。添加report.txt
和screenshots
文件夹以将其导出。屏幕截图 (和报告) 仅在作业失败时才会导出。如果这不是想要的行为,请将 artifacts:when 更改为
on_success
或always
如果其中一个测试作业失败,则整个管道都会失败。如果此管道仅用于测试和导出结果,您应该允许测试作业失败而不会使整个管道失败 allow_failure
总结上面写的所有内容,这里是建议更改的 yml 文件:
stages:
- install
- test
- report
#
# Templates
#
# Tags can't be defined on a global scope for now
# Ref: https://gitlab.com/gitlab-org/gitlab-ce/issues/23434
.default: &default
tags: ['macbook']
.node_modules:
artifacts: &node_modules
paths: ['node_modules']
when: always
.test_reports:
artifacts: &test_reports
paths:
- screenshot
- reports
when: always
.chrome_template: &chrome_template
environment:
name: chrome
variables: &chrome_template_var
SELENIUM_BROWSER: chrome
.safari_template: &safari_template
environment:
name: safari
variables: &safari_template_var
SELENIUM_BROWSER: safari
#
# Jobs
#
# Install
install:
<<: *default
stage: install
except: ['tags']
script:
- npm install
artifacts: *node_modules
# Test
.test: &test
<<: *default
stage: test
script:
- npm run test
# Move report to individual file to fix overwriting it by other artifacts
- mkdir -p reports
- mv report.txt reports/report_${CI_JOB_NAME}.txt
retry: 1
artifacts: *test_reports
# Allow tests to fail, exporting artifacts to the final stage
allow_failure: true
.test_startup: &test_startup
<<: *test
variables: &startup_var
SPEC: startup
.test_quality: &test_quality
<<: *test
variables: &quality_var
SPEC: quality
.test_language: &test_language
<<: *test
variables: &language_var
SPEC: language
test:startup:chrome:
<<: *test_startup
<<: *chrome_template
variables:
<<: *startup_var
<<: *chrome_template_var
test:language:chrome:
<<: *test_language
<<: *chrome_template
variables:
<<: *language_var
<<: *chrome_template_var
test:quality:chrome:
<<: *test_quality
<<: *chrome_template
variables:
<<: *quality_var
<<: *chrome_template_var
test:startup:safari:
<<: *test_startup
<<: *safari_template
variables:
<<: *startup_var
<<: *safari_template_var
test:language:safari:
<<: *test_language
<<: *safari_template
variables:
<<: *language_var
<<: *safari_template_var
test:quality:safari:
<<: *test_quality
<<: *safari_template
variables:
<<: *quality_var
<<: *safari_template_var
# Report
report:
<<: *default
stage: report
script:
# Summarize reports before exporting
# Ignore errors when there are no reports to export
- cat reports/*.txt > report.txt | true
artifacts:
paths:
- screenshot
- report.txt
请检查多个失败的测试作业是否不会覆盖 "screenshot" 文件夹的内容。否则,您需要以与 "report.txt" 文件相同的方式移动屏幕截图。