Class: Datadog::CI::Ext::Environment::Providers::GithubActions

Inherits:
Base
  • Object
show all
Defined in:
lib/datadog/ci/ext/environment/providers/github_actions.rb

Overview

Constant Summary collapse

GITHUB_RUNNER_DIAG_PATHS =

Paths to GitHub Actions runner diagnostics folder GitHub-hosted (SaaS) runners use the cached path, self-hosted runners use the non-cached path

[
  "/home/runner/actions-runner/cached/_diag", # GitHub-hosted (SaaS) runners
  "/home/runner/actions-runner/_diag"         # Self-hosted runners
].freeze

Instance Attribute Summary

Attributes inherited from Base

#env

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#git_branch, #git_commit_author_date, #git_commit_author_email, #git_commit_author_name, #git_commit_committer_date, #git_commit_committer_email, #git_commit_committer_name, #git_commit_head_author_date, #git_commit_head_author_email, #git_commit_head_author_name, #git_commit_head_committer_date, #git_commit_head_committer_email, #git_commit_head_committer_name, #git_commit_head_message, #git_commit_message, #git_pull_request_base_branch_sha, #git_tag, #initialize, #node_labels, #node_name, #stage_name

Constructor Details

This class inherits a constructor from Datadog::CI::Ext::Environment::Providers::Base

Class Method Details

.handles?(env) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 25

def self.handles?(env)
  env.key?("GITHUB_SHA")
end

Instance Method Details

#ci_env_varsObject



86
87
88
89
90
91
92
93
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 86

def ci_env_vars
  {
    "GITHUB_SERVER_URL" => github_server_url,
    "GITHUB_REPOSITORY" => env["GITHUB_REPOSITORY"],
    "GITHUB_RUN_ID" => env["GITHUB_RUN_ID"],
    "GITHUB_RUN_ATTEMPT" => env["GITHUB_RUN_ATTEMPT"]
  }.reject { |_, v| v.nil? }.to_json
end

#git_branch_or_tagObject



80
81
82
83
84
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 80

def git_branch_or_tag
  ref = env["GITHUB_HEAD_REF"]
  ref = env["GITHUB_REF"] if ref.nil? || ref.empty?
  ref
end

#git_commit_head_shaObject



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 112

def git_commit_head_sha
  return nil if git_pull_request_base_branch.nil?

  event_json = github_event_json
  return nil if event_json.nil?

  event_json.dig("pull_request", "head", "sha")
rescue => e
  Datadog.logger.error("Failed to extract commit head SHA from GitHub Actions: #{e}")
  Core::Telemetry::Logger.report(e, description: "Failed to extract commit head SHA from GitHub Actions")
  nil
end

#git_commit_shaObject



76
77
78
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 76

def git_commit_sha
  env["GITHUB_SHA"]
end

#git_pull_request_base_branchObject



95
96
97
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 95

def git_pull_request_base_branch
  env["GITHUB_BASE_REF"]
end

#git_pull_request_base_branch_head_shaObject



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 99

def git_pull_request_base_branch_head_sha
  return nil if git_pull_request_base_branch.nil?

  event_json = github_event_json
  return nil if event_json.nil?

  event_json.dig("pull_request", "base", "sha")
rescue => e
  Datadog.logger.error("Failed to extract pull request base branch head SHA from GitHub Actions: #{e}")
  Core::Telemetry::Logger.report(e, description: "Failed to extract pull request base branch head SHA from GitHub Actions")
  nil
end

#git_repository_urlObject



72
73
74
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 72

def git_repository_url
  "#{github_server_url}/#{env["GITHUB_REPOSITORY"]}.git"
end

#job_idObject



46
47
48
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 46

def job_id
  numeric_job_id || env["GITHUB_JOB"]
end

#job_nameObject



33
34
35
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 33

def job_name
  env["GITHUB_JOB"]
end

#job_urlObject



37
38
39
40
41
42
43
44
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 37

def job_url
  numeric_id = numeric_job_id
  if numeric_id
    "#{github_server_url}/#{env["GITHUB_REPOSITORY"]}/actions/runs/#{env["GITHUB_RUN_ID"]}/job/#{numeric_id}"
  else
    "#{github_server_url}/#{env["GITHUB_REPOSITORY"]}/commit/#{env["GITHUB_SHA"]}/checks"
  end
end

#pipeline_idObject



50
51
52
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 50

def pipeline_id
  env["GITHUB_RUN_ID"]
end

#pipeline_nameObject



54
55
56
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 54

def pipeline_name
  env["GITHUB_WORKFLOW"]
end

#pipeline_numberObject



58
59
60
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 58

def pipeline_number
  env["GITHUB_RUN_NUMBER"]
end

#pipeline_urlObject



62
63
64
65
66
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 62

def pipeline_url
  res = "#{github_server_url}/#{env["GITHUB_REPOSITORY"]}/actions/runs/#{env["GITHUB_RUN_ID"]}"
  res = "#{res}/attempts/#{env["GITHUB_RUN_ATTEMPT"]}" if env["GITHUB_RUN_ATTEMPT"]
  res
end

#pr_numberObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 125

def pr_number
  event_json = github_event_json
  return nil if event_json.nil?

  event_json["number"]
rescue => e
  Datadog.logger.error("Failed to extract PR number from GitHub Actions: #{e}")
  Core::Telemetry::Logger.report(e, description: "Failed to extract PR number from GitHub Actions")
  nil
end

#provider_nameObject



29
30
31
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 29

def provider_name
  Provider::GITHUB
end

#workspace_pathObject



68
69
70
# File 'lib/datadog/ci/ext/environment/providers/github_actions.rb', line 68

def workspace_path
  env["GITHUB_WORKSPACE"]
end