GitLab CI语法 [cache|artifacts|dependencies]
如果在job范围之外定义了cache
使用paths
指令选择要缓存的文件或目录,路径是相对于项目目录,不能直接链接到项目目录之外。
$CI_PROJECT_DIR
在job build中定义缓存,将会缓存target目录下的所有.jar文件。
build:
script: test
cache:
paths:
- target/*.jar
当在全局定义了cache:paths会被job中覆盖。所有的项目在引用这个缓存的时候就会把target/*.jar文件不断覆盖
cache:
paths:
- my/files
build:
script: echo "hello"
cache:
key: build ##指定作业
paths:
- target/
cache:key
cache:key
时, 会为每个 job 分配一个独立的 cache。cache:key变量可以使用任何预定义变量,默认
default ,从GitLab 9.0开始,默认情况下所有内容都在管道和作业之间共享。
按照分支设置缓存
cache:
key: ${CI_COMMIT_REF_SLUG}
cache: key: files: - Gemfile.lock - package.json paths: - vendor/ruby - node_modules
cache:
key:
files:
- Gemfile.lock
prefix: ${CI_JOB_NAME}
paths:
- vendor/ruby
rspec:
script:
- bundle exec rspec
$CI_JOB_NAME
prefix
将使密钥看起来像: rspec-feef9576d21ee9b6a32e30c5c79d0a0ceb68d1e5
,并且作业缓存在不同分支之间共享,如果分支更改了Gemfile.lock
,则该分支将为cache:key:files
具有新的SHA校验和. 将生成一个新的缓存密钥,并为该密钥创建一个新的缓存. 如果Gemfile.lock
未发生变化 ,则将前缀添加default
,因此示例中的键为rspec-default
。
stages: - setup - test prepare: stage: setup cache: key: gems paths: - vendor/bundle script: - bundle install --deployment rspec: stage: test cache: key: gems paths: - vendor/bundle policy: pull script: - bundle exec rspec ...
pull-push
缓存策略.
policy: pull
跳过下载步骤
policy: push
stages:
- setup
- test
prepare:
stage: setup
cache:
key: gems
paths:
- vendor/bundle
script:
- bundle install --deployment
rspec:
stage: test
cache:
key: gems
paths:
- vendor/bundle
policy: pull
script:
- bundle exec rspec ...
before_script: - echo "before-script!!" variables: DOMAIN: example.com cache: paths: - target/ stages: - build - test - deploy build: before_script: - echo "before-script in job" stage: build tags: - build only: - master script: - ls - id - mvn clean package -DskipTests - ls target - echo "$DOMAIN" - false && true ; exit_code=$? - if [ $exit_code -ne 0 ]; then echo "Previous command failed"; fi; - sleep 2; after_script: - echo "after script in job" unittest: stage: test tags: - build only: - master script: - echo "run test" - echo 'test' >> target/a.txt - ls target retry: max: 2 when: - script_failure deploy: stage: deploy tags: - build only: - master script: - echo "run deploy" - ls target retry: max: 2 when: - script_failure after_script: - echo "after-script"
执行流水线,日志分析
build作业运行时会对项目代码打包,然后生成target目录。作业结束创建缓存。
开始第二个作业test,此时会把当前目录中的target目录删除掉(因为做了git 对比)。
获取到第一个作业生成的缓存target目录。
开始第三个作业,同样先删除了target目录,然后获取了第二个作业的缓存。最后生成了当前的缓存。
可以看出这种全局缓存,下一个任务会使用上一个任务的缓存
结论: 全局缓存生效于未在作业中定义缓存的所有作业,这种情况如果每个作业都对缓存目录做了更改,会出现缓存被覆盖的场景。
before_script:
- echo "before-script!!"
variables:
DOMAIN: example.com
cache:
paths:
- target/
stages:
- build
- test
- deploy
build:
before_script:
- echo "before-script in job"
stage: build
tags:
- build
only:
- master
script:
- ls
- id
- cat target/a.txt
- mvn clean package -DskipTests
- ls target
- echo "$DOMAIN"
- false && true ; exit_code=$?
- if [ $exit_code -ne 0 ]; then echo "Previous command failed"; fi;
- sleep 2;
after_script:
- echo "after script in job"
cache:
policy: pull #不下载缓存
unittest:
stage: test
tags:
- build
only:
- master
script:
- echo "run test"
- echo 'test' >> target/a.txt
- ls target
- cat target/a.txt
retry:
max: 2
when:
- script_failure
deploy:
stage: deploy
tags:
- build
only:
- master
script:
- cat target/a.txt
- echo "run deploy"
- ls target
- echo "deploy" >> target/a.txt
retry:
max: 2
when:
- script_failure
after_script:
- echo "after-script"
用于指定在作业成功或者失败时应附加到作业的文件或目录的列表。作业完成后,工件将被发送到GitLab,并可在GitLab UI中下载。
路径是相对于项目目录的,不能直接链接到项目目录之外。
artifacts:
paths:
- target/
收集好的制品就可以在gitlab里进行展示
制品的创建
default-job:
script:
- mvn test -U
except:
- tags
release-job:
script:
- mvn package -U
artifacts:
paths:
- target/*.war
only:
- tags
expose_as
可用于在合并请求 UI中公开作业工件。最多个数是10个
test:
script:
- echo 1
artifacts:
expose_as: 'artifact 1' ##制品的名称
paths:
- path/to/file.txt ##制品的路径
file1.txt
制品浏览
每个合并请求最多可以公开10个作业工件。
如果指定了目录,那么如果目录中有多个文件,则该链接将指向指向作业工件浏览器。
name
指令定义所创建的工件存档的名称。可以为每个存档使用唯一的名称。 artifacts:name
变量可以使用任何预定义变量。默认名称是artifacts
,下载artifacts
改为artifacts.zip
使用当前作业的名称创建存档
job:
artifacts:
name: "$CI_JOB_NAME"
paths:
- binaries/
使用内部分支或标记的名称(仅包括binaries目录)创建存档,
job:
artifacts:
name: "$CI_COMMIT_REF_NAME"
paths:
- binaries/
使用当前作业的名称和当前分支或标记(仅包括二进制文件目录)创建存档
job:
artifacts:
name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
paths:
- binaries/
job:
artifacts:
name: "$CI_JOB_STAGE-$CI_COMMIT_REF_NAME"
paths:
- binaries/
job:
artifacts:
when: on_failure
expire_in
‘42’##42秒
‘3 mins 4 sec’
‘2 hrs 20 min’
‘2h20min’
‘6 mos 1 day’
‘47 yrs 6 mos and 4d’
‘3 weeks and 2 days’
一周后过期
job:
artifacts:
expire_in: 1 week
注意:
artifacts:reports:junit ------单元测试报告
单元测试这个功能gitlab默认是关闭的,要想使用这个功能需要手动进入gitlab控制台进行开启
build:
stage: build
tags:
- build
only:
- master
script:
- mvn test
- mvn cobertura:cobertura
- ls target
artifacts:
name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
when: on_success
expose_as: 'artifact 1'
paths:
- target/*.jar
reports:
junit: target/surefire-reports/TEST-*.xml
如果您使用的JUnit工具导出到多个XML文件,则可以在一个作业中指定多个测试报告路径,它们将被自动串联到一个文件中. 使用文件名模式( junit: rspec-*.xml
),文件名数组( junit: [rspec-1.xml, rspec-2.xml, rspec-3.xml]
)或其组合( junit: [rspec.xml, test-results/TEST-*.xml]
开启单元测试以后,带分支合并请求的时候就会有测试的数据,失败也会显示出来
开启gitlab单元测试
登录gitlab su - git $ gitlab-rails console -------------------------------------------------------------------------------- GitLab: 12.9.0 (9a382ff2c82) FOSS GitLab Shell: 12.0.0 PostgreSQL: 10.12 -------------------------------------------------------------------------------- Feature.enable(:junit_pipeline_view)Loading production environment (Rails 6.0.2) irb(main):001:0> irb(main):002:0> irb(main):003:0> Feature.enable(:junit_pipeline_view) => true irb(main):004:0>
artifacts:reports:cobertura-------覆盖率报告--一般不使用了解即可
cobertura是mvn里的一个插件
收集的Cobertura覆盖率报告将作为工件上传到GitLab,并在合并请求中自动显示。
build:
stage: build
tags:
- build
only:
- master
script:
- mvn test
- mvn cobertura:cobertura
- ls target
artifacts:
name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
when: on_success
expose_as: 'artifact 1'
paths:
- target/*.jar
reports:
junit: target/surefire-reports/TEST-*.xml
cobertura: target/site/cobertura/coverage.xml
要使用cobertura也需要在gitlab控制台进行开启功能
$ gitlab-rails console -------------------------------------------------------------------------------- GitLab: 12.9.0 (9a382ff2c82) FOSS GitLab Shell: 12.0.0 PostgreSQL: 10.12 -------------------------------------------------------------------------------- Loading production environment (Rails 6.0.2) irb(main):001:0> irb(main):002:0> irb(main):003:0> Feature.enable(:coverage_report_view) => true
<plugins>
<!-- cobertura plugin start -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>cobertura-maven-plugin</artifactId>
<version>2.7</version>
<configuration>
<formats>
<format>html</format>
<format>xml</format>
</formats>
</configuration>
</plugin>
<!-- cobertura plugin end -->
</plugins>
执行 mvn cobertura:cobertura 运行测试并产生 Cobertura 覆盖率报告。
before_script:
- echo "before-script!!"
variables:
DOMAIN: example.com
cache:
paths:
- target/
stages:
- build
- test
- deploy
build:
before_script:
- echo "before-script in job"
stage: build
tags:
- build
only:
- master
script:
- ls
- id
- mvn test
- mvn cobertura:cobertura
- ls target
- echo "$DOMAIN"
- false && true ; exit_code=$?
- if [ $exit_code -ne 0 ]; then echo "Previous command failed"; fi;
- sleep 2;
after_script:
- echo "after script in job"
artifacts:
name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
when: on_success
#expose_as: 'artifact 1'
paths:
- target/*.jar
#- target/surefire-reports/TEST*.xml
reports:
junit: target/surefire-reports/TEST-*.xml
cobertura: target/site/cobertura/coverage.xml
coverage: '/Code coverage: \d+\.\d+/'
unittest:
dependencies:
- build
stage: test
tags:
- build
only:
- master
script:
- echo "run test"
- echo 'test' >> target/a.txt
- ls target
retry:
max: 2
when:
- script_failure
deploy:
stage: deploy
tags:
- build
only:
- master
script:
- echo "run deploy"
- ls target
retry:
max: 2
when:
- script_failure
after_script:
- echo "after-script"
unittest: dependencies: - build stage: test tags: - build only: - master script: - echo "run test" - echo 'test' >> target/a.txt - ls target retry: max: 2 when: - script_failure
before_script:
- echo "before-script!!"
variables:
DOMAIN: example.com
cache:
paths:
- target/
stages:
- build
- test
- deploy
build:
before_script:
- echo "before-script in job"
stage: build
tags:
- build
only:
- master
script:
- ls
- id
- mvn test
- mvn cobertura:cobertura
- ls target
- echo "$DOMAIN"
- false && true ; exit_code=$?
- if [ $exit_code -ne 0 ]; then echo "Previous command failed"; fi;
- sleep 2;
after_script:
- echo "after script in job"
artifacts:
name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
when: on_success
#expose_as: 'artifact 1'
paths:
- target/*.jar
#- target/surefire-reports/TEST*.xml
reports:
junit: target/surefire-reports/TEST-*.xml
cobertura: target/site/cobertura/coverage.xml
coverage: '/Code coverage: \d+\.\d+/'
unittest:
dependencies:
- build
stage: test
tags:
- build
only:
- master
script:
- echo "run test"
- echo 'test' >> target/a.txt
- ls target
retry:
max: 2
when:
- script_failure
deploy:
stage: deploy
tags:
- build
only:
- master
script:
- echo "run deploy"
- ls target
retry:
max: 2
when:
- script_failure
after_script:
- echo "after-script"

评论