Skip to content

Commit

Permalink
Merge pull request #19 from Taucher2003/12-show-job-output
Browse files Browse the repository at this point in the history
Add Job logs to action output
  • Loading branch information
Taucher2003 authored Nov 19, 2023
2 parents 7f816c6 + 8a59b44 commit 36f5532
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ jobs:

</details>

## 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.
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
1 change: 1 addition & 0 deletions lib/action/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion lib/action/entrypoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions lib/action/helper/github.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions lib/action/step/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ def initialize(context)
def skip?
false
end

def github
GitlabPipelineAction::Helper::Github
end
end
end
end
2 changes: 2 additions & 0 deletions lib/action/step/prepare_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand Down
36 changes: 36 additions & 0 deletions lib/action/step/show_job_logs.rb
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions lib/gitlab_pipeline_action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
Expand Down
9 changes: 9 additions & 0 deletions test/.gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 36f5532

Please sign in to comment.