首页 > 其他分享 >GitlabCI学习笔记之五:GitLabRunner pipeline语法之artifacts dependencies

GitlabCI学习笔记之五:GitLabRunner pipeline语法之artifacts dependencies

时间:2023-06-10 21:47:49浏览次数:48  
标签:pipeline GitLabRunner target script artifacts cobertura build 工件

artifacts

用于指定在作业成功或者失败时应附加到作业的文件或目录的列表。作业完成后,工件将被发送到GitLab,并可在GitLab UI中下载。

artifacts:paths

路径是相对于项目目录的,不能直接链接到项目目录之外。

将制品设置为target目录

artifacts:
  paths:
    - target/

禁用工件传递 

job:
  stage: build
  script: make build
  dependencies: []

您可能只想为标记的发行版创建构件,以避免用临时构建构件填充构建服务器存储。仅为标签创建工件( default-job不会创建工件):

default-job:
  script:
    - mvn test -U
  except:
    - tags

release-job:
  script:
    - mvn package -U
  artifacts:
    paths:
      - target/*.war
  only:
    - tags

artifacts:expose_as

关键字expose_as可用于在合并请求 UI中公开作业工件。

例如,要匹配单个文件:

test:
  script: 
    - echo 1
  artifacts:
    expose_as: 'artifact 1'
    paths: 
      - path/to/file.txt

使用此配置,GitLab将在指向的相关合并请求中添加链接file1.txt

制品浏览

请注意以下几点:

每个合并请求最多可以公开10个作业工件。
如果指定了目录,那么如果目录中有多个文件,则该链接将指向指向作业工件浏览器。
如果开启GitlabPages可以对.html .htm .txt .json .log扩展名单个文件工件渲染工件。

artifacts:name

通过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/

artifacts:when

用于在作业失败时或尽管失败而上传工件。on_success仅在作业成功时上载工件。这是默认值。on_failure仅在作业失败时上载工件。always 上载工件,无论作业状态如何。

要仅在作业失败时上传工件:

job:
  artifacts:
    when: on_failure

artifacts:expire_in

制品的有效期,从上传和存储到GitLab的时间开始算起。如果未定义过期时间,则默认为30天。

expire_in的值以秒为单位的经过时间,除非提供了单位。可解析值的示例:

‘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

用于从作业中收集测试报告,代码质量报告和安全报告. 在GitLab的UI中显示这些报告。

**注意:**无论作业结果(成功或失败),都将收集测试报告。

artifacts:reports:junit

收集junit单元测试报告,收集的JUnit报告将作为工件上传到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
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>

参考链接:https://docs.gitlab.com/ee/ci/junit_test_reports.html

artifacts:reports:cobertura

收集的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
$ 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

maven集成cobertura插件

<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 覆盖率报告。

参考链接:https://docs.gitlab.com/12.9/ee/user/project/merge_requests/test_coverage_visualization.html

dependencies

定义要获取工件的作业列表,只能从当前阶段之前执行的阶段定义作业。定义一个空数组将跳过下载该作业的任何工件不会考虑先前作业的状态,因此,如果它失败或是未运行的手动作业,则不会发生错误。

如果设置为依赖项的作业的工件已过期或删除,那么依赖项作业将失败。

综合示例

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"
  

 

 

标签:pipeline,GitLabRunner,target,script,artifacts,cobertura,build,工件
From: https://www.cnblogs.com/panwenbin-logs/p/17471996.html

相关文章

  • GitlabCI学习笔记之五:GitLabRunner pipeline语法之cache
    cache缓存用来指定需要在job之间缓存的文件或目录。只能使用该项目工作空间内的路径。不要使用缓存在阶段之间传递工件,因为缓存旨在存储编译项目所需的运行时依赖项。如果在job范围之外定义了cache ,则意味着它是全局设置,所有job都将使用该定义。如果未全局定义或未按job定义则......
  • 2023-06-03:redis中pipeline有什么好处,为什么要用 pipeline?
    2023-06-03:redis中pipeline有什么好处,为什么要用pipeline?答案2023-06-03:Redis客户端执行一条命令通常包括以下四个阶段:1.发送命令:客户端将要执行的命令发送到Redis服务器。2.命令排队:Redis服务器将收到的命令放入队列中,按照先进先出(FIFO)的原则等待执行。3.命令执行:当Redis服务器轮......
  • 2023-06-03:redis中pipeline有什么好处,为什么要用 pipeline?
    2023-06-03:redis中pipeline有什么好处,为什么要用pipeline?答案2023-06-03:Redis客户端执行一条命令通常包括以下四个阶段:1.发送命令:客户端将要执行的命令发送到Redis服务器。2.命令排队:Redis服务器将收到的命令放入队列中,按照先进先出(FIFO)的原则等待执行。3.命令执行:当Redis服......
  • gitlab--不同的 stage 不重新下载代码、GIT_CHECKOUT、制品 artifacts
    介绍在gitlabci中,不同的stage都会重新下载代码,例如下面的.gitlab-ci.ymldefault:image:ruby:2.7.5stages:#运行的阶段顺序-build-test-deploybuild:#job的名称stage:build#阶段的名称script:-ls-l-echo123>test1.txt#在......
  • gitlab--不同的 stage 不重新下载代码、GIT_CHECKOUT、制品 artifacts
    介绍在gitlabci中,不同的stage都会重新下载代码,例如下面的.gitlab-ci.ymldefault:image:ruby:2.7.5stages:#运行的阶段顺序-build-test-deploybuild:#job的名称stage:build#阶段的名称script:-ls-l-echo123>test1.txt#在......
  • GitlabCI学习笔记之四:GitLabRunner pipeline语法之only except rules workflow
    1.only&except参考文档:https://docs.gitlab.com/ee/ci/yaml/#only--exceptonly和except是两个参数用分支策略来限制jobs构建,后面会逐步被rules替代only定义哪些分支和标签的git项目将会被job执行。except定义哪些分支和标签的git项目将不会被job执行示例job:#use......
  • gitlab--不同的 stage 不重新下载代码、GIT_CHECKOUT、制品 artifacts
    介绍在gitlabci中,不同的stage都会重新下载代码,例如下面的.gitlab-ci.ymldefault:image:ruby:2.7.5stages:#运行的阶段顺序-build-test-deploybuild:#job的名称stage:build#阶段的名称script:-ls-l-echo123>test1.txt#......
  • GitlabCI学习笔记之三:GitLabRunner pipeline语法之tags allow_faillure when retry ti
    1.tags用于从允许运行该项目的所有Runner列表中选择特定的Runner,在Runner注册期间,您可以指定Runner的标签。tags可让您使用指定了标签的runner来运行作业,此runner具有ruby和postgres标签。示例给定带有osx标签的OSXRunner和带有windows标签的WindowsRunner,以下作业将在......
  • GitlabCI学习笔记之二:GitLabRunner pipeline语法
    1.pipeline语法之语法校验进入项目中,点击CI/CD下pipeline页面中CIint 输入pipeline内容,点击Validate2.pipeline语法之job在每个项目中,我们使用名为.gitlab-ci.yml的YAML文件配置GitLabCI/CD管道。这里在pipeline中定义了两个作业,每个作业运行不同的命令。命令可以......
  • GitlabCI学习笔记之一:安装Gitlab和GitLabRunner
    1.安装GitLab#下载地址https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/wgethttps://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.9.0-ce.0.el7.x86_64.rpmrpm-ivhgitlab-ce-12.9.0-ce.0.el7.x86_64.rpmvim/etc/gitlab/gitlab.rbexterna......