优先考虑并行阶段内的一些工作实例

时间:2021-07-21 10:25:47

标签: gitlab gitlab-ci gitlab-ci-runner

我的管道从公司管道继承(使用 include)。我的管道中有连续的阶段,最后一个包含三个工作:

There are 2 of 3 running parallel jobs inside the same stage

由于 gitlab-runner 的资源非常有限,所以不能同时运行所有作业。 Gitlab 似乎在同一阶段内按字母顺序启动作业。

作业“sonarqube”是最期望首先运行的作业,如何在保持它们并行的同时优先其实例化?

在我的 .gitlab-ci.yml 中,作业已按预期顺序覆盖,但这不起作用:

include:
  - project: "project/parent"
    ref: production
    file: "main.yml"

# ...

sonarqube:
  stage: analysis

license-finder:
  allow_failure: true

checkmarx:
  dependencies: []
  timeout: 1 hours

你有线索吗?

2 个答案:

答案 0 :(得分:0)

您可以在 .gitlab-ci.yml 中使用 resource_group attribute 限制并行性,但不要认为这可以满足您的需求... 也许您可以将这些作业配置为手动并在之前的作业中使用 API call 触发它们?类似:

include:
  - project: "project/parent"
    ref: production
    file: "main.yml"

call_jobs:
  script:
    # execute first job
    - curl --request POST --header "PRIVATE-TOKEN: <your_access_token>" "https://gitlab.example.com/api/v4/projects/1/jobs/1/play"
    - sleep 60
    # execute the second job after some time 
    .....

sonarqube:
  stage: analysis
  when: manual

license-finder:
  allow_failure: true
  when: manual

checkmarx:
  dependencies: []
  timeout: 1 hours
  when: manual

您甚至可以获取最昂贵工作的状态,并在完成后触发另外两个工作... 使用 GET /projects/:id/pipelines/:pipeline_id/jobs 您可以获得最昂贵工作的 ID(不能使用 JOB_ID 环境变量,因为您不在您想要获得 ID 的工作中!)然后使用 GET /projects/:id/jobs/:job_id 您可以获得状态...

答案 1 :(得分:0)

可以通过使用以下关键字 when: delayedstart_in: x seconds 延迟其他作业来确定某些作业的优先级。

这是一个工作示例:

sonarqube:
  stage: analysis

license-finder:
  stage: analysis
  when: delayed
  start_in: 1 seconds

checkmarx:
  stage: analysis
  when: delayed
  start_in: 1 seconds
相关问题