mirror of
https://github.com/actions/runner.git
synced 2025-12-11 12:57:05 +00:00
adding support for a service container docker logs
This commit is contained in:
@@ -32,6 +32,7 @@ namespace GitHub.Runner.Worker.Container
|
|||||||
Task<int> DockerExec(IExecutionContext context, string containerId, string options, string command);
|
Task<int> DockerExec(IExecutionContext context, string containerId, string options, string command);
|
||||||
Task<int> DockerExec(IExecutionContext context, string containerId, string options, string command, List<string> outputs);
|
Task<int> DockerExec(IExecutionContext context, string containerId, string options, string command, List<string> outputs);
|
||||||
Task<List<string>> DockerInspect(IExecutionContext context, string dockerObject, string options);
|
Task<List<string>> DockerInspect(IExecutionContext context, string dockerObject, string options);
|
||||||
|
Task<List<string>> DockerInspectLogs(IExecutionContext context, string dockerContainerId);
|
||||||
Task<List<PortMapping>> DockerPort(IExecutionContext context, string containerId);
|
Task<List<PortMapping>> DockerPort(IExecutionContext context, string containerId);
|
||||||
Task<int> DockerLogin(IExecutionContext context, string configFileDirectory, string registry, string username, string password);
|
Task<int> DockerLogin(IExecutionContext context, string configFileDirectory, string registry, string username, string password);
|
||||||
}
|
}
|
||||||
@@ -352,6 +353,11 @@ namespace GitHub.Runner.Worker.Container
|
|||||||
return await ExecuteDockerCommandAsync(context, "inspect", $"{options} {dockerObject}");
|
return await ExecuteDockerCommandAsync(context, "inspect", $"{options} {dockerObject}");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<List<string>> DockerInspectLogs(IExecutionContext context, string dockerContainerId)
|
||||||
|
{
|
||||||
|
return await ExecuteDockerCommandAsync(context, "logs", $"{dockerContainerId}");
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<List<PortMapping>> DockerPort(IExecutionContext context, string containerId)
|
public async Task<List<PortMapping>> DockerPort(IExecutionContext context, string containerId)
|
||||||
{
|
{
|
||||||
List<string> portMappingLines = await ExecuteDockerCommandAsync(context, "port", containerId);
|
List<string> portMappingLines = await ExecuteDockerCommandAsync(context, "port", containerId);
|
||||||
|
|||||||
@@ -101,7 +101,8 @@ namespace GitHub.Runner.Worker
|
|||||||
executionContext.Output("##[group]Waiting for all services to be ready");
|
executionContext.Output("##[group]Waiting for all services to be ready");
|
||||||
foreach (var container in containers.Where(c => !c.IsJobContainer))
|
foreach (var container in containers.Where(c => !c.IsJobContainer))
|
||||||
{
|
{
|
||||||
await ContainerHealthcheck(executionContext, container);
|
var healthcheck = await Healthcheck(executionContext, container);
|
||||||
|
await ContainerHealthcheckLogs(executionContext, container, healthcheck);
|
||||||
}
|
}
|
||||||
executionContext.Output("##[endgroup]");
|
executionContext.Output("##[endgroup]");
|
||||||
}
|
}
|
||||||
@@ -395,14 +396,13 @@ namespace GitHub.Runner.Worker
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task ContainerHealthcheck(IExecutionContext executionContext, ContainerInfo container)
|
private async Task<string> Healthcheck(IExecutionContext executionContext, ContainerInfo container){
|
||||||
{
|
|
||||||
string healthCheck = "--format=\"{{if .Config.Healthcheck}}{{print .State.Health.Status}}{{end}}\"";
|
string healthCheck = "--format=\"{{if .Config.Healthcheck}}{{print .State.Health.Status}}{{end}}\"";
|
||||||
string serviceHealth = (await _dockerManager.DockerInspect(context: executionContext, dockerObject: container.ContainerId, options: healthCheck)).FirstOrDefault();
|
string serviceHealth = (await _dockerManager.DockerInspect(context: executionContext, dockerObject: container.ContainerId, options: healthCheck)).FirstOrDefault();
|
||||||
if (string.IsNullOrEmpty(serviceHealth))
|
if (string.IsNullOrEmpty(serviceHealth))
|
||||||
{
|
{
|
||||||
// Container has no HEALTHCHECK
|
// Container has no HEALTHCHECK
|
||||||
return;
|
return String.Empty;
|
||||||
}
|
}
|
||||||
var retryCount = 0;
|
var retryCount = 0;
|
||||||
while (string.Equals(serviceHealth, "starting", StringComparison.OrdinalIgnoreCase))
|
while (string.Equals(serviceHealth, "starting", StringComparison.OrdinalIgnoreCase))
|
||||||
@@ -413,12 +413,20 @@ namespace GitHub.Runner.Worker
|
|||||||
serviceHealth = (await _dockerManager.DockerInspect(context: executionContext, dockerObject: container.ContainerId, options: healthCheck)).FirstOrDefault();
|
serviceHealth = (await _dockerManager.DockerInspect(context: executionContext, dockerObject: container.ContainerId, options: healthCheck)).FirstOrDefault();
|
||||||
retryCount++;
|
retryCount++;
|
||||||
}
|
}
|
||||||
|
return serviceHealth;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ContainerHealthcheckLogs(IExecutionContext executionContext, ContainerInfo container, string serviceHealth)
|
||||||
|
{
|
||||||
|
|
||||||
if (string.Equals(serviceHealth, "healthy", StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(serviceHealth, "healthy", StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
executionContext.Output($"{container.ContainerNetworkAlias} service is healthy.");
|
executionContext.Output($"{container.ContainerNetworkAlias} service is healthy.");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
List<string> dockerLogs = await _dockerManager.DockerInspectLogs(context: executionContext, dockerContainerId: container.ContainerId);
|
||||||
|
dockerLogs.ForEach(log => executionContext.Output(log));
|
||||||
throw new InvalidOperationException($"Failed to initialize, {container.ContainerNetworkAlias} service is {serviceHealth}.");
|
throw new InvalidOperationException($"Failed to initialize, {container.ContainerNetworkAlias} service is {serviceHealth}.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
44
src/Test/L0/Worker/ContainerOperationProviderL0.cs
Normal file
44
src/Test/L0/Worker/ContainerOperationProviderL0.cs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
using System;
|
||||||
|
using GitHub.Runner.Worker;
|
||||||
|
using GitHub.Runner.Worker.Container;
|
||||||
|
using Moq;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace GitHub.Runner.Common.Tests.Worker
|
||||||
|
{
|
||||||
|
public sealed class ContainerOperationProviderL0
|
||||||
|
{
|
||||||
|
// private Mock<DockerCommandManager> _commandManager;
|
||||||
|
// private ContainerOperationProvider operationProvider;
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
[Trait("Level", "L0")]
|
||||||
|
[Trait("Category", "Worker")]
|
||||||
|
public void EnablePluginInternalCommand()
|
||||||
|
{
|
||||||
|
|
||||||
|
// _ec.Setup(x => x.Write(It.IsAny<string>(), It.IsAny<string>()))
|
||||||
|
// .Returns((string tag, string line) =>
|
||||||
|
// {
|
||||||
|
// hc.GetTrace().Info($"{tag} {line}");
|
||||||
|
// return 1;
|
||||||
|
// });
|
||||||
|
// _ec.Setup(x => x.AddIssue(It.IsAny<Issue>(), It.IsAny<string>()))
|
||||||
|
// .Callback((Issue issue, string message) =>
|
||||||
|
// {
|
||||||
|
// hc.GetTrace().Info($"{issue.Type} {issue.Message} {message ?? string.Empty}");
|
||||||
|
// });
|
||||||
|
|
||||||
|
// _commandManager.EnablePluginInternalCommand();
|
||||||
|
|
||||||
|
// Assert.True(_commandManager.TryProcessCommand(_ec.Object, "##[internal-set-repo-path repoFullName=actions/runner;workspaceRepo=true]somepath", null));
|
||||||
|
|
||||||
|
// _pipelineDirectoryManager.Verify(x => x.UpdateRepositoryDirectory(_ec.Object, "actions/runner", "somepath", true), Times.Once);
|
||||||
|
|
||||||
|
}
|
||||||
|
// private TestHostContext CreateTestContext([CallerMemberName] string testName = "") {
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user