Skip to content

Commit

Permalink
extract Jenkins
Browse files Browse the repository at this point in the history
  • Loading branch information
anmarchenko committed Sep 4, 2023
1 parent 91943fb commit 7a39ffe
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 31 deletions.
30 changes: 0 additions & 30 deletions lib/datadog/ci/ext/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ module Environment
TAG_CI_ENV_VARS = "_dd.ci.env_vars"

PROVIDERS = [
["JENKINS_URL", :extract_jenkins],
["TEAMCITY_VERSION", :extract_teamcity],
["TRAVIS", :extract_travis],
["BITRISE_BUILD_SLUG", :extract_bitrise],
Expand Down Expand Up @@ -79,35 +78,6 @@ def filter_sensitive_info(url)
end

# CI providers
def extract_jenkins(env)
branch, tag = branch_or_tag(env["GIT_BRANCH"])

if (name = env["JOB_NAME"])
name = name.gsub("/#{normalize_ref(branch)}", "") if branch
name = name.split("/").reject { |v| v.nil? || v.include?("=") }.join("/")
end

node_labels = env["NODE_LABELS"] && env["NODE_LABELS"].split.to_json

{
Git::TAG_BRANCH => branch,
Git::TAG_COMMIT_SHA => env["GIT_COMMIT"],
Git::TAG_REPOSITORY_URL => env["GIT_URL"] || env["GIT_URL_1"],
Git::TAG_TAG => tag,
TAG_PIPELINE_ID => env["BUILD_TAG"],
TAG_PIPELINE_NAME => name,
TAG_PIPELINE_NUMBER => env["BUILD_NUMBER"],
TAG_PIPELINE_URL => env["BUILD_URL"],
TAG_PROVIDER_NAME => "jenkins",
TAG_WORKSPACE_PATH => env["WORKSPACE"],
TAG_NODE_LABELS => node_labels,
TAG_NODE_NAME => env["NODE_NAME"],
TAG_CI_ENV_VARS => {
"DD_CUSTOM_TRACE_ID" => env["DD_CUSTOM_TRACE_ID"]
}.to_json
}
end

def extract_teamcity(env)
{
TAG_PROVIDER_NAME => "teamcity",
Expand Down
15 changes: 14 additions & 1 deletion lib/datadog/ci/ext/environment/extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Extractor
require_relative "providers/circleci"
require_relative "providers/github_actions"
require_relative "providers/gitlab"
require_relative "providers/jenkins"

PROVIDERS = [
["APPVEYOR", Providers::Appveyor],
Expand All @@ -29,7 +30,8 @@ class Extractor
["BUILDKITE", Providers::Buildkite],
["CIRCLECI", Providers::Circleci],
["GITHUB_SHA", Providers::GithubActions],
["GITLAB_CI", Providers::Gitlab]
["GITLAB_CI", Providers::Gitlab],
["JENKINS_URL", Providers::Jenkins]
]

def self.for_environment(env)
Expand Down Expand Up @@ -163,6 +165,17 @@ def branch_or_tag(branch_or_tag_string)

[@branch, @tag]
end

def normalize_ref(name)
refs = %r{^refs/(heads/)?}
origin = %r{^origin/}
tags = %r{^tags/}
name.gsub(refs, "").gsub(origin, "").gsub(tags, "") unless name.nil?
end

def filter_sensitive_info(url)
url.gsub(%r{(https?://)[^/]*@}, '\1') unless url.nil?
end
end
end
end
Expand Down
90 changes: 90 additions & 0 deletions lib/datadog/ci/ext/environment/providers/jenkins.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# frozen_string_literal: true

require_relative "../extractor"

module Datadog
module CI
module Ext
module Environment
module Providers
# Jenkins: https://www.jenkins.io/
# Environment variables docs: https://www.jenkins.io/doc/book/pipeline/jenkinsfile/#using-environment-variables
class Jenkins < Extractor
private

# overridden methods
def provider_name
"jenkins"
end

def pipeline_id
env["BUILD_TAG"]
end

def pipeline_name
if (name = env["JOB_NAME"])
name = name.gsub("/#{normalize_ref(git_branch)}", "") if git_branch
name = name.split("/").reject { |v| v.nil? || v.include?("=") }.join("/")
end
name
end

def pipeline_number
env["BUILD_NUMBER"]
end

def pipeline_url
env["BUILD_URL"]
end

def workspace_path
env["WORKSPACE"]
end

def node_name
env["NODE_NAME"]
end

def node_labels
env["NODE_LABELS"] && env["NODE_LABELS"].split.to_json
end

def git_repository_url
env["GIT_URL"] || env["GIT_URL_1"]
end

def git_commit_sha
env["GIT_COMMIT"]
end

def git_branch
return @branch if defined?(@branch)

set_branch_and_tag
@branch
end

def git_tag
return @tag if defined?(@tag)

set_branch_and_tag
@tag
end

def ci_env_vars
{
"DD_CUSTOM_TRACE_ID" => env["DD_CUSTOM_TRACE_ID"]
}.to_json
end

# jenkins-specific methods

def set_branch_and_tag
@branch, @tag = branch_or_tag(env["GIT_BRANCH"])
end
end
end
end
end
end
end
4 changes: 4 additions & 0 deletions sig/datadog/ci/ext/environment/extractor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ module Datadog
def git_commit_sha: () -> nil

def branch_or_tag: (String? branch_or_tag_string) -> [String?, String?]

def normalize_ref: (String? name) -> String?

def filter_sensitive_info: (String? url) -> String?
end
end
end
Expand Down
40 changes: 40 additions & 0 deletions sig/datadog/ci/ext/environment/providers/jenkins.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Datadog
module CI
module Ext
module Environment
module Providers
class Jenkins < Extractor
private
def provider_name: () -> "jenkins"

def pipeline_id: () -> String?

def pipeline_name: () -> String?

def pipeline_number: () -> String?

def pipeline_url: () -> String?

def workspace_path: () -> String?

def node_name: () -> String?

def node_labels: () -> String?

def git_repository_url: () -> String?

def git_commit_sha: () -> String?

def git_branch: () -> String?

def git_tag: () -> String?

def ci_env_vars: () -> String?

def set_branch_and_tag: () -> [String?, String?]
end
end
end
end
end
end

0 comments on commit 7a39ffe

Please sign in to comment.