From 9d48d2be8730a4000e1c4d199db0a9c0d7ba7c87 Mon Sep 17 00:00:00 2001 From: TingluoHuang Date: Fri, 11 Sep 2020 00:41:58 -0400 Subject: [PATCH] new scripe --- Dockerfile | 20 +++++++----- src/Runner.Listener/JobDispatcher.cs | 12 +++---- src/Runner.Listener/SelfUpdater.cs | 47 ++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 14 deletions(-) diff --git a/Dockerfile b/Dockerfile index e8735f34c..76254b107 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,23 +31,27 @@ RUN curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add RUN curl -fsSL https://get.docker.com -o get-docker.sh RUN sh get-docker.sh + +# Allow runner to run as root +ENV RUNNER_ALLOW_RUNASROOT=1 + # Directory for runner to operate in RUN mkdir /actions-runner WORKDIR /actions-runner COPY ./src/Misc/download-runner.sh /actions-runner/download-runner.sh COPY ./src/Misc/entrypoint.sh /actions-runner/entrypoint.sh -COPY ./src/Misc/jobstart.sh /actions-runner/jobstart.sh -COPY ./src/Misc/jobrunning.sh /actions-runner/jobrunning.sh -COPY ./src/Misc/jobcomplete.sh /actions-runner/jobcomplete.sh +# COPY ./src/Misc/jobstart.sh /actions-runner/jobstart.sh +# COPY ./src/Misc/jobrunning.sh /actions-runner/jobrunning.sh +# COPY ./src/Misc/jobcomplete.sh /actions-runner/jobcomplete.sh +COPY ./src/Misc/runner_lifecycle.sh /actions-runner/runner_lifecycle.sh RUN /actions-runner/download-runner.sh RUN rm -f /actions-runner/download-runner.sh -ENV _INTERNAL_JOBSTART_NOTIFICATION=/actions-runner/jobstart.sh -ENV _INTERNAL_JOBRUNNING_NOTIFICATION=/actions-runner/jobrunning.sh -ENV _INTERNAL_JOBCOMPLETE_NOTIFICATION=/actions-runner/jobcomplete.sh +# ENV _INTERNAL_JOBSTART_NOTIFICATION=/actions-runner/jobstart.sh +# ENV _INTERNAL_JOBRUNNING_NOTIFICATION=/actions-runner/jobrunning.sh +# ENV _INTERNAL_JOBCOMPLETE_NOTIFICATION=/actions-runner/jobcomplete.sh +ENV _INTERNAL_RUNNER_LIFECYCLE_NOTIFICATION=/actions-runner/runner_lifecycle.sh -# Allow runner to run as root -ENV RUNNER_ALLOW_RUNASROOT=1 ENTRYPOINT ["./entrypoint.sh"] \ No newline at end of file diff --git a/src/Runner.Listener/JobDispatcher.cs b/src/Runner.Listener/JobDispatcher.cs index 2b1ce24cc..cad4b9fc3 100644 --- a/src/Runner.Listener/JobDispatcher.cs +++ b/src/Runner.Listener/JobDispatcher.cs @@ -477,7 +477,7 @@ namespace GitHub.Runner.Listener var systemConnection = message.Resources.Endpoints.SingleOrDefault(x => string.Equals(x.Name, WellKnownServiceEndpointNames.SystemVssConnection, StringComparison.OrdinalIgnoreCase)); var accessToken = systemConnection?.Authorization?.Parameters["AccessToken"]; notification.JobStarted(message.JobId, accessToken, systemConnection.Url); - var jobStartNotification = Environment.GetEnvironmentVariable("_INTERNAL_JOBSTART_NOTIFICATION"); + var jobStartNotification = Environment.GetEnvironmentVariable("_INTERNAL_RUNNER_LIFECYCLE_NOTIFICATION"); if (!string.IsNullOrEmpty(jobStartNotification)) { term.WriteLine($"{DateTime.UtcNow:u}: Publish JobStart to {jobStartNotification}"); @@ -507,7 +507,7 @@ namespace GitHub.Runner.Listener await jobStartInvoker.ExecuteAsync( workingDirectory: HostContext.GetDirectory(WellKnownDirectory.Root), fileName: WhichUtil.Which("bash"), - arguments: jobStartNotification, + arguments: $"-c \"{jobStartNotification}\" JOBSTART \"{DateTime.UtcNow.ToString("O")}\"", environment: null, requireExitCodeZero: true, outputEncoding: null, @@ -660,7 +660,7 @@ namespace GitHub.Runner.Listener { // This should be the last thing to run so we don't notify external parties until actually finished await notification.JobCompleted(message.JobId); - var jobCompleteNotification = Environment.GetEnvironmentVariable("_INTERNAL_JOBCOMPLETE_NOTIFICATION"); + var jobCompleteNotification = Environment.GetEnvironmentVariable("_INTERNAL_RUNNER_LIFECYCLE_NOTIFICATION"); if (!string.IsNullOrEmpty(jobCompleteNotification)) { term.WriteLine($"{DateTime.UtcNow:u}: Publish JobComplete to {jobCompleteNotification}"); @@ -690,7 +690,7 @@ namespace GitHub.Runner.Listener await jobCompleteInvoker.ExecuteAsync( workingDirectory: HostContext.GetDirectory(WellKnownDirectory.Root), fileName: WhichUtil.Which("bash"), - arguments: jobCompleteNotification, + arguments: $"-c \"{jobCompleteNotification}\" JOBCOMPLETE \"{DateTime.UtcNow.ToString("O")}\"", environment: null, requireExitCodeZero: true, outputEncoding: null, @@ -741,7 +741,7 @@ namespace GitHub.Runner.Listener } else { - var jobRunningNotification = Environment.GetEnvironmentVariable("_INTERNAL_JOBRUNNING_NOTIFICATION"); + var jobRunningNotification = Environment.GetEnvironmentVariable("_INTERNAL_RUNNER_LIFECYCLE_NOTIFICATION"); if (!string.IsNullOrEmpty(jobRunningNotification)) { HostContext.GetService().WriteLine($"{DateTime.UtcNow:u}: Publish JobRunning to {jobRunningNotification}"); @@ -771,7 +771,7 @@ namespace GitHub.Runner.Listener await jobRunningInvoker.ExecuteAsync( workingDirectory: HostContext.GetDirectory(WellKnownDirectory.Root), fileName: WhichUtil.Which("bash"), - arguments: jobRunningNotification, + arguments: $"-c \"{jobRunningNotification}\" JOBRUNNING \"{DateTime.UtcNow.ToString("O")}\"", environment: null, requireExitCodeZero: true, outputEncoding: null, diff --git a/src/Runner.Listener/SelfUpdater.cs b/src/Runner.Listener/SelfUpdater.cs index 05f856ca1..7e7d1557f 100644 --- a/src/Runner.Listener/SelfUpdater.cs +++ b/src/Runner.Listener/SelfUpdater.cs @@ -59,6 +59,53 @@ namespace GitHub.Runner.Listener Trace.Info($"An update is available."); + var runnerUpdateNotification = Environment.GetEnvironmentVariable("_INTERNAL_RUNNER_LIFECYCLE_NOTIFICATION"); + if (!string.IsNullOrEmpty(runnerUpdateNotification)) + { + HostContext.GetService().WriteLine($"{DateTime.UtcNow:u}: Publish RunnerUpdate to {runnerUpdateNotification}"); + using (var runnerUpdateInvoker = HostContext.CreateService()) + { + runnerUpdateInvoker.OutputDataReceived += delegate (object sender, ProcessDataReceivedEventArgs stdout) + { + if (!string.IsNullOrEmpty(stdout.Data)) + { + Trace.Info($"RunnerUpdateNotification: {stdout.Data}"); + } + }; + + runnerUpdateInvoker.ErrorDataReceived += delegate (object sender, ProcessDataReceivedEventArgs stderr) + { + if (!string.IsNullOrEmpty(stderr.Data)) + { + if (!string.IsNullOrEmpty(stderr.Data)) + { + Trace.Error($"RunnerUpdateNotification: {stderr.Data}"); + } + } + }; + + try + { + await runnerUpdateInvoker.ExecuteAsync( + workingDirectory: HostContext.GetDirectory(WellKnownDirectory.Root), + fileName: WhichUtil.Which("bash"), + arguments: $"-c \"{runnerUpdateNotification}\" RUNNERUPDATE \"{DateTime.UtcNow.ToString("O")}\"", + environment: null, + requireExitCodeZero: true, + outputEncoding: null, + killProcessOnCancel: true, + redirectStandardIn: null, + inheritConsoleHandler: false, + keepStandardInOpen: false, + highPriorityProcess: true, + cancellationToken: new CancellationTokenSource(10000).Token); + } + catch (Exception ex) + { + Trace.Error($"Fail to publish RunnerUpdate notification: {ex}"); + } + } + } // Print console line that warn user not shutdown runner. await UpdateRunnerUpdateStateAsync("Runner update in progress, do not shutdown runner."); await UpdateRunnerUpdateStateAsync($"Downloading {_targetPackage.Version} runner");