Module: Datadog::CI::Ext::Environment

Defined in:
lib/datadog/ci/ext/environment.rb,
lib/datadog/ci/ext/environment/extractor.rb,
lib/datadog/ci/ext/environment/providers.rb,
lib/datadog/ci/ext/environment/providers/base.rb,
lib/datadog/ci/ext/environment/providers/azure.rb,
lib/datadog/ci/ext/environment/providers/buddy.rb,
lib/datadog/ci/ext/environment/providers/drone.rb,
lib/datadog/ci/ext/environment/providers/gitlab.rb,
lib/datadog/ci/ext/environment/providers/travis.rb,
lib/datadog/ci/ext/environment/providers/bitrise.rb,
lib/datadog/ci/ext/environment/providers/jenkins.rb,
lib/datadog/ci/ext/environment/providers/appveyor.rb,
lib/datadog/ci/ext/environment/providers/circleci.rb,
lib/datadog/ci/ext/environment/providers/teamcity.rb,
lib/datadog/ci/ext/environment/providers/bitbucket.rb,
lib/datadog/ci/ext/environment/providers/buildkite.rb,
lib/datadog/ci/ext/environment/providers/codefresh.rb,
lib/datadog/ci/ext/environment/providers/local_git.rb,
lib/datadog/ci/ext/environment/providers/github_actions.rb,
lib/datadog/ci/ext/environment/providers/aws_code_pipeline.rb,
lib/datadog/ci/ext/environment/providers/user_defined_tags.rb,
lib/datadog/ci/ext/environment/configuration_discrepancy_checker.rb

Overview

Defines constants for CI tags

Defined Under Namespace

Modules: Provider, Providers Classes: ConfigurationDiscrepancyChecker, Extractor

Constant Summary collapse

TAG_JOB_ID =
"ci.job.id"
TAG_JOB_NAME =
"ci.job.name"
TAG_JOB_URL =
"ci.job.url"
TAG_PIPELINE_ID =
"ci.pipeline.id"
TAG_PIPELINE_NAME =
"ci.pipeline.name"
TAG_PIPELINE_NUMBER =
"ci.pipeline.number"
TAG_PIPELINE_URL =
"ci.pipeline.url"
TAG_PROVIDER_NAME =
"ci.provider.name"
TAG_STAGE_NAME =
"ci.stage.name"
TAG_WORKSPACE_PATH =
"ci.workspace_path"
TAG_NODE_LABELS =
"ci.node.labels"
TAG_NODE_NAME =
"ci.node.name"
TAG_CI_ENV_VARS =
"_dd.ci.env_vars"
TAG_PR_NUMBER =
"pr.number"
POSSIBLE_BUNDLE_LOCATIONS =
%w[vendor/bundle .bundle].freeze
ENV_SPECIAL_KEY_FOR_GIT_COMMIT_HEAD_SHA =
"_dd.ci.environment.git_commit_head_sha"

Class Method Summary collapse

Class Method Details

.ensure_post_conditions(tags) ⇒ Object



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

def ensure_post_conditions(tags)
  validate_repository_url(tags[Git::TAG_REPOSITORY_URL])
  validate_git_sha(tags[Git::TAG_COMMIT_SHA])
end

.tags(env) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/datadog/ci/ext/environment.rb', line 55

def tags(env)
  # Extract metadata from CI provider environment variables
  tags = Environment::Extractor.new(env).tags

  # If user defined metadata is defined, overwrite
  user_provided_tags = Environment::Extractor.new(env, provider_klass: Providers::UserDefinedTags).tags
  tags.merge!(user_provided_tags)

  # NOTE: we need to provide head commit sha as part of the environment if it was discovered from provider.
  #
  # This info will be later used by LocalGit provider to extract commit message and user info for head commit.
  # It is useful for CI providers that run jobs on artificial merge commits instead of a head commit of a
  # feature branch.
  #
  # NOTE 2: when we discover that head commit sha exists it means that we are running on an artificial merge
  # commit created by CI provider. Most likely we also operate on a shallow clone of a repo - in this case
  # we need to unshallow at least the parent of the current merge commit to be able to extract information
  # from the real original commit.
  if tags[Git::TAG_COMMIT_HEAD_SHA]
    CI::Git::LocalRepository.fetch_head_commit_sha(tags[Git::TAG_COMMIT_HEAD_SHA]) if CI::Git::LocalRepository.git_shallow_clone?

    env[ENV_SPECIAL_KEY_FOR_GIT_COMMIT_HEAD_SHA] = tags[Git::TAG_COMMIT_HEAD_SHA]
  end

  # Fill out tags from local git as fallback
  local_git_tags = Environment::Extractor.new(env, provider_klass: Providers::LocalGit).tags
  local_git_tags.each do |key, value|
    tags[key] ||= value
  end

  # send some telemetry for the cases where git commit sha is overriden
  discrepancy_checker = ConfigurationDiscrepancyChecker.new(tags, local_git_tags, user_provided_tags)
  discrepancy_checker.check_for_discrepancies

  ensure_post_conditions(tags)

  tags
end

.validate_git_sha(git_sha) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/datadog/ci/ext/environment.rb', line 106

def validate_git_sha(git_sha)
  return if Utils::Git.valid_commit_sha?(git_sha)

  message = "DD_GIT_COMMIT_SHA must be a full-length git SHA."

  message += if git_sha.nil? || git_sha.empty?
    " No value was set and no SHA was automatically extracted."
  elsif git_sha.length < Git::SHA_LENGTH
    " Expected SHA length #{Git::SHA_LENGTH}, was #{git_sha.length}."
  else
    " Expected SHA to be a valid HEX number, got #{git_sha}."
  end

  Datadog.logger.error(message)
  Core::Telemetry::Logger.error(message)
end

.validate_repository_url(repo_url) ⇒ Object



99
100
101
102
103
104
# File 'lib/datadog/ci/ext/environment.rb', line 99

def validate_repository_url(repo_url)
  return if !repo_url.nil? && !repo_url.empty?

  Datadog.logger.error("DD_GIT_REPOSITORY_URL is not set or empty; no repo URL was automatically extracted")
  Core::Telemetry::Logger.error("DD_GIT_REPOSITORY_URL is not set or empty; no repo URL was automatically extracted")
end