From 8a59b449de8f266e018bd9b55644ec01aa4d54fb Mon Sep 17 00:00:00 2001 From: Niklas van Schrick Date: Sun, 19 Nov 2023 16:10:49 +0100 Subject: [PATCH] Add Job logs to action output --- .github/workflows/test.yml | 1 + README.md | 10 +++++++++ action.yml | 3 +++ lib/action/context.rb | 1 + lib/action/entrypoint.rb | 3 ++- lib/action/helper/github.rb | 26 +++++++++++++++++++++ lib/action/step/base.rb | 4 ++++ lib/action/step/prepare_context.rb | 2 ++ lib/action/step/show_job_logs.rb | 36 ++++++++++++++++++++++++++++++ lib/gitlab_pipeline_action.rb | 3 +++ test/.gitlab-ci.yml | 9 ++++++++ 11 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 lib/action/helper/github.rb create mode 100644 lib/action/step/show_job_logs.rb diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 535261f..a520758 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -27,6 +27,7 @@ jobs: GL_PROJECT_ID: '1000' GL_RUNNER_TOKEN: some_long_runner_token GL_API_TOKEN: TEST1234567890123456 + SHOW_JOB_LOGS: all env: GLPA_SOME_VARIABLE: some value for the variable - run: 'curl --silent --header "Private-Token: TEST1234567890123456" "http://127.17.0.1:8080/api/v4/projects/1000/jobs/1/trace"' diff --git a/README.md b/README.md index 7d09c57..3a94dcc 100644 --- a/README.md +++ b/README.md @@ -90,6 +90,16 @@ jobs: +## Show job logs + +By adding the `SHOW_JOB_LOGS` to the input section (`with`), you can show the job logs +from the GitLab pipeline in the output of the GitHub Action Run. + +Available options are `none`, `failures` and `all`. \ +When using `none` or not specifying the option, no job logs will be shown. \ +With `failures`, the job logs of failed jobs will be shown. \ +`all` shows the job log of all jobs in the pipeline. + ## Known issues GitHub does not pass secrets to actions triggered by pull requests from forks. diff --git a/action.yml b/action.yml index dec8a9b..f5fac05 100644 --- a/action.yml +++ b/action.yml @@ -13,6 +13,9 @@ inputs: GL_API_TOKEN: description: 'A token to check the pipeline status with. Required if the project is private' required: true + SHOW_JOB_LOGS: + description: 'Set if the job log should be shown in the action output' + default: 'none' author: 'Taucher2003' runs: using: 'docker' diff --git a/lib/action/context.rb b/lib/action/context.rb index 1014997..472d29e 100644 --- a/lib/action/context.rb +++ b/lib/action/context.rb @@ -5,6 +5,7 @@ class Context attr_accessor :gh_project, :gh_sha, :gh_ref, :gh_server_url, :gl_server_url, :gl_project_id, :gl_project_path, :gl_runner_token, :gl_api_token, :gl_pipeline, :gl_branch_name, :gl_pipeline_variables, + :gl_show_job_logs, :git_repository, :git_path, :gitlab_client, :docker_runner_container diff --git a/lib/action/entrypoint.rb b/lib/action/entrypoint.rb index 7c54cf0..d71d6b4 100644 --- a/lib/action/entrypoint.rb +++ b/lib/action/entrypoint.rb @@ -11,7 +11,8 @@ class Entrypoint GitlabPipelineAction::Step::StartRunner, GitlabPipelineAction::Step::WaitForPipeline, GitlabPipelineAction::Step::StopRunner, - GitlabPipelineAction::Step::RemoveBranchFromGitlab + GitlabPipelineAction::Step::RemoveBranchFromGitlab, + GitlabPipelineAction::Step::ShowJobLogs ].freeze def execute diff --git a/lib/action/helper/github.rb b/lib/action/helper/github.rb new file mode 100644 index 0000000..0af131d --- /dev/null +++ b/lib/action/helper/github.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module GitlabPipelineAction + module Helper + module Github + module_function + + def warning(message) + puts "::warning::#{message}" + end + + def with_group(name) + puts "::group::#{name}" + yield + puts '::endgroup::' + end + + def stop_commands + token = SecureRandom.hex + puts "::stop-commands::#{token}" + yield + puts "::#{token}::" + end + end + end +end diff --git a/lib/action/step/base.rb b/lib/action/step/base.rb index e2cadd5..f5a5e20 100644 --- a/lib/action/step/base.rb +++ b/lib/action/step/base.rb @@ -14,6 +14,10 @@ def initialize(context) def skip? false end + + def github + GitlabPipelineAction::Helper::Github + end end end end diff --git a/lib/action/step/prepare_context.rb b/lib/action/step/prepare_context.rb index 95e73d2..45d6bcd 100644 --- a/lib/action/step/prepare_context.rb +++ b/lib/action/step/prepare_context.rb @@ -18,6 +18,8 @@ def execute context.gl_pipeline_variables = ENV.select { |key| key.start_with?('GLPA_') } .transform_keys { |key| key.delete_prefix('GLPA_') } + context.gl_show_job_logs = ENV.fetch('INPUT_SHOW_JOB_LOGS', nil)&.to_sym + context.git_path = "/tmp/repo/#{SecureRandom.hex}" config = {} diff --git a/lib/action/step/show_job_logs.rb b/lib/action/step/show_job_logs.rb new file mode 100644 index 0000000..7d63e65 --- /dev/null +++ b/lib/action/step/show_job_logs.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +module GitlabPipelineAction + module Step + class ShowJobLogs < Base + VARIANTS = { + all: ->(_) { true }, + failures: ->(job) { job.status == 'failed' }, + }.freeze + + def execute + unless VARIANTS.key?(context.gl_show_job_logs) + github.warning 'Invalid option for SHOW_JOB_LOGS' + return + end + + jobs = context.gitlab_client + .pipeline_jobs(context.gl_project_id, context.gl_pipeline.id) + .select(&VARIANTS[context.gl_show_job_logs]) + .sort_by { |job| job.id.to_i } + + jobs.each do |job| + github.with_group "'#{job.name}' in stage '#{job.stage}' (#{job.status})" do + github.stop_commands do + puts context.gitlab_client.job_trace(context.gl_project_id, job.id) + end + end + end + end + + def skip? + context.gl_show_job_logs.nil? || context.gl_show_job_logs == :none + end + end + end +end diff --git a/lib/gitlab_pipeline_action.rb b/lib/gitlab_pipeline_action.rb index 8175b23..2e680e2 100644 --- a/lib/gitlab_pipeline_action.rb +++ b/lib/gitlab_pipeline_action.rb @@ -5,6 +5,8 @@ require 'securerandom' require 'docker' +require 'action/helper/github' + require 'action/step/base' require 'action/step/clone_project' require 'action/step/fetch_data' @@ -15,6 +17,7 @@ require 'action/step/wait_for_pipeline' require 'action/step/stop_runner' require 'action/step/remove_branch_from_gitlab' +require 'action/step/show_job_logs' require 'action/context' require 'action/pipeline_awaiter' diff --git a/test/.gitlab-ci.yml b/test/.gitlab-ci.yml index 4e7a5dd..a2717ed 100644 --- a/test/.gitlab-ci.yml +++ b/test/.gitlab-ci.yml @@ -2,3 +2,12 @@ job: script: - echo $CI_COMMIT_SHA - echo $SOME_VARIABLE + +job-with-commands: + script: + - echo "::notice::Some output" + +failing-job: + allow_failure: true + script: + - exit 1