GitLab API:创建作业并在运行器中运行

问题描述 投票:0回答:1

我想为跑步者触发新工作,但我收到了床位请求。如何正确做?

 public async Task<string> TriggerCommandOnRunner(int runnerId, string command)
    {
        try
        {
            // Create a new job to run the specified command on the GitLab Runner.
            var jobRequest = new
            {
                job = new
                {
                    runner_id = runnerId,
                    script = command
                }
            };

            var content = new StringContent(JsonSerializer.Serialize(jobRequest), Encoding.UTF8,
                "application/json");
            var response = await _httpClient.PostAsync($"{_gitLabUrl}jobs/request", content);

            if (response.IsSuccessStatusCode)
            {
                var responseBody = await response.Content.ReadAsStringAsync();
                // Parse and process the responseBody as needed.
                return responseBody;
            }

            return await Task.FromResult("failed with connection");
        }
        catch (Exception ex)
        {
            return await Task.FromResult($"failed {ex.Message}");
        }

        return string.Empty;
    }
c# gitlab gitlab-ci-runner gitlab-api
1个回答
0
投票

驻留在“/jobs/request”的 API 不用于创建作业。它只能由 GitLab 提供的实际 GitLab Runner 执行程序使用,而不能由用户代码使用。该 API 用于让运行者请求已创建并位于 GitLab 服务器队列中的作业。

如果您尝试创建新作业供运行器执行,则应该使用 pipelines API 创建管道或其他创建管道/作业的常规方法。

© www.soinside.com 2019 - 2024. All rights reserved.