如何使用 GitLab API 获取子管道的作业工件?

Sev*_*maz 2 gitlab gitlab-ci gitlab-api

job 1 ________ child-pipeline ________ job 3 (x)
         /                       \
job 2 __/                         \___ job 4
Run Code Online (Sandbox Code Playgroud)

我正在努力达到 的神器job 3。这是我到目前为止到达的地方:

job 3我需要根据GitLab 文档获取 ID 。我使用Gitbeaker来简化 NodeJS 中的事情,但如果您不熟悉它,可以用curl 示例来回答。

首先,我获得了提交的管道,然后检索了这些管道的作业,但这些只给了我,job 1即使job 2文档说“在 GitLab 13.3 及更高版本中,此端点返回包括子管道在内的任何管道的数据。(参考)”

job 1 ________ child-pipeline ________ job 3 (x)
         /                       \
job 2 __/                         \___ job 4
Run Code Online (Sandbox Code Playgroud)

然后我选择了另一条路线来获取舞台上的提交状态,child-pipelines假设我将获取子管道的管道 ID 并且我将到达它们的作业。但我得到的 ID 不是管道 ID。

api.Pipelines.all(PROJECT_ID, {
  sha: 'commit_sha',
  ref: 'master',
}).then(async (commitPipelines) => {
  for await (const pipeline of commitPipelines) {
    api.Jobs.showPipelineJobs(PROJECT_ID, pipeline.id, {
      scope: 'success',
    }).then((pipelineJobs) => {
      console.log(pipelineJobs);
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

最后,我的两种方法都卡在了子管道上。也许,我忽略了这一点。有什么建议么?

Sev*_*maz 6

我发现 Gitlab 有一个端点来访问子管道,称为管道桥,这帮助我找到了解决方案。

async function getCommitPipelines(commitId) {
  return await api.Pipelines.all(GITLAB_PROJECT_ID, {
    sha: commitId,
    ref: 'master',
    status: 'success',
  });
}

async function getPipelineJobs(pipelineId) {
  return await api.Jobs.showPipelineJobs(GITLAB_PROJECT_ID, pipelineId, {
    scope: 'success',
  });
}

async function getPipelineBridges(pipelineId) {
  return await api.Jobs.showPipelineBridges(GITLAB_PROJECT_ID, pipelineId);
}

async function downloadArtifactFile(jobId) {
  return await api.Jobs.downloadSingleArtifactFile(
    GITLAB_PROJECT_ID,
    jobId,
    'artifact-file.json',
  );
}

async function downloadCommitArtifacts(commitId) {
  const commitArtifacts = [];
  // 1. Get all of the pipelines of a the commit
  const commitPipelines = await getCommitPipelines(commitId);

  for (const commitPipeline of commitPipelines) {
    // 2. Get all of the downstream pipelines of each pipeline
    const bridgePipelines = await getPipelineBridges(commitPipeline.id);

    for (const bridgePipeline of bridgePipelines) {
      // 3. Get all the child pipeline jobs
      // Here you are looking at the job 3 and job 4
      const job = await getPipelineJobs(
        bridgePipeline.downstream_pipeline.id
      );

      for (const job of jobs) {
        if (job.name !== 'job-3') {
          continue;
        }

        // 4. Get the artifact (the artifact file in this case) from the job
        const artifactFile = await downloadArtifactFile(job.id);
        commitArtifacts.push(artifactFile);
      }
    }
  }

  return commitArtifacts;
}
Run Code Online (Sandbox Code Playgroud)