【问题标题】:Gitlab CI SAST access to gl-sast-report.json artifact in subsequent stageGitlab CI SAST 在后续阶段访问 gl-sast-report.json 工件
【发布时间】:2021-01-21 17:40:21
【问题描述】:

我想在我的 CI 的后续阶段使用在 SAST 过程中创建的 gl-sast-report.json 文件,但找不到。

ci.yml

include:
  - template: Security/SAST.gitlab-ci.yml

stages:
  - test
  - .post
sast:
  rules:
    - if: $CI_COMMIT_TAG

send-reports:
  stage: .post
  dependencies: 
    - sast
  script: 
    - ls
    - echo "in post stage"
    - cat gl-sast-report.json

输出:

Running with gitlab-runner 13.2.1 (efa30e33)
on blah blah blah
Preparing the "docker" executor
00:01
.
.
.

Preparing environment
00:01
Running on runner-zqk9bcef-project-4296-concurrent-0 via ff93ba7b6ee2...
Getting source from Git repository
00:01
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in blah blah
Checking out 9c2edf67 as 39-test-dso...
Removing gl-sast-report.json
Skipping Git submodules setup
Executing "step_script" stage of the job script
00:03
$ ls
<stuff in the repo>
$ echo "in .post stage"
in post stage
$ cat gl-sast-report.json
cat: can't open 'gl-sast-report.json': No such file or directory
ERROR: Job failed: exit code 1

您可以看到我认为是问题所在的行 Removing gl-sast-report.json。

我在 https://gitlab.com/gitlab-org/gitlab/-/blob/v11.11.0-rc2-ee/lib/gitlab/ci/templates/Security/SAST.gitlab-ci.yml#L33-45 的 SAST.gitlab-ci.yml 中的任何地方都看不到这一点

关于如何在我的 CI 管道的下一阶段使用此工件有什么想法吗?

更新:

所以我在下面尝试了 k33g_org 的建议,但无济于事。似乎这是由于 sast 模板的限制。做了以下测试。

include:
  - template: Security/SAST.gitlab-ci.yml

stages:
  - test
  - upload

something:
  stage: test
  script:
      - echo "in something"
      - echo "this is something" > something.txt
  artifacts:
      paths: [something.txt]

sast:
  before_script:
      - echo "hello from before sast"
      - echo "this is in the file" > test.txt
  artifacts:
    reports:
      sast: gl-sast-report.json
    paths: [gl-sast-report.json, test.txt]

send-reports:
  stage: upload
  dependencies:
    - sast
    - something
  before_script:
      - echo "This is the send-reports before_script"
  script:
    - echo "in send-reports job"
    - ls
  artifacts:
      reports:
          sast: gl-sast-report.json

三个变化:

  1. 根据 k33g_org 的建议更新了代码
  2. 在 sast 作业中创建了另一个工件(以查看它是否会传递给 send-reports 作业)
  3. 创建了一个新作业(某事),我在其中创建了一个新的 something.txt 工件(以查看它是否会传递给 send-reports 作业)

输出:

Preparing environment
00:01
Running on runner-zqx7qoq-project-4296-concurrent-0 via e3fe672984b4...
Getting source from Git repository
Fetching changes with git depth set to 50...
Reinitialized existing Git repository in /<repo>
Checking out 26501c44 as <branch_name>...
Removing something.txt
Skipping Git submodules setup
Downloading artifacts
00:00
Downloading artifacts for something (64950)...
Downloading artifacts from coordinator... ok        id=64950 
responseStatus=200 OK token=zoJwysdq
Executing "step_script" stage of the job script
00:01
$ echo "This is the send-reports before_script"
This is the send-reports before_script
$ echo "in send-reports job"
in send-reports job
$ ls
...<other stuff in repo>
something.txt
Uploading artifacts for successful job
00:01
Uploading artifacts...
WARNING: gl-sast-report.json: no matching files    
ERROR: No files to upload                          
Cleaning up file based variables
00:01
Job succeeded

注意事项:

  • something.txt 完成了这项工作
  • 来自 sast 作业的所有工件都不能用于后续作业

我只能得出结论,sast 模板的内部存在不允许工件传播到后续作业的内容。

【问题讨论】:

    标签: gitlab gitlab-ci


    【解决方案1】:

    在第一份工作 (sast) 中添加:

      artifacts:
        paths: [gl-sast-report.json]
        reports:
          sast: gl-sast-report.json
    

    在下一份工作中 (send-reports) 添加此内容

      artifacts:
        reports:
          sast: gl-sast-report.json
    

    那么您应该可以在下一份工作中访问该报告 (send-reports)

    【讨论】:

    【解决方案2】:

    不要将 gl-sast-report.json 工件引用为 sast 报告,而是将其引用为常规工件。

    所以你应该以这种方式声明工件

    artifacts:
      paths:
        - 'gl-sast-report.json'
    

    而不是

    reports:
      sast: gl-sast-report.json
    

    【讨论】:

      【解决方案3】:

      我也发现了这个问题,它也会影响其他一些扫描仪;我向 GitLab 提出了要解决的问题:

      https://gitlab.com/gitlab-org/gitlab/-/issues/345696

      【讨论】:

      • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
      猜你喜欢
      • 2021-05-06
      • 2021-11-07
      • 2021-08-20
      • 2019-06-16
      • 2017-05-05
      • 1970-01-01
      • 2019-02-18
      • 2022-01-11
      • 1970-01-01
      相关资源
      最近更新 更多