lib/datadog/ci/ext/environment.rb in ddtrace-1.8.0 vs lib/datadog/ci/ext/environment.rb in ddtrace-1.9.0

- old
+ new

@@ -3,10 +3,11 @@ # typed: true require_relative '../../core/git/ext' require 'open3' +require 'json' module Datadog module CI module Ext # Defines constants for CI tags @@ -20,15 +21,17 @@ 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_CI_ENV_VARS = '_dd.ci.env_vars' PROVIDERS = [ ['APPVEYOR', :extract_appveyor], ['TF_BUILD', :extract_azure_pipelines], ['BITBUCKET_COMMIT', :extract_bitbucket], + ['BUDDY', :extract_buddy], ['BUILDKITE', :extract_buildkite], ['CIRCLECI', :extract_circle_ci], ['GITHUB_SHA', :extract_github_actions], ['GITLAB_CI', :extract_gitlab], ['JENKINS_URL', :extract_jenkins], @@ -41,11 +44,10 @@ def tags(env) # Extract metadata from CI provider environment variables _, extractor = PROVIDERS.find { |provider_env_var, _| env.key?(provider_env_var) } tags = extractor ? public_send(extractor, env).reject { |_, v| v.nil? || v.strip.empty? } : {} - tags.delete(Core::Git::Ext::TAG_BRANCH) unless tags[Core::Git::Ext::TAG_TAG].nil? # If user defined metadata is defined, overwrite tags.merge!(extract_user_defined_git(env)) if !tags[Core::Git::Ext::TAG_BRANCH].nil? && tags[Core::Git::Ext::TAG_BRANCH].include?('tags/') tags[Core::Git::Ext::TAG_TAG] = tags[Core::Git::Ext::TAG_BRANCH] @@ -124,20 +126,16 @@ build_id = env['BUILD_BUILDID'] if build_id && (team_foundation_server_uri = env['SYSTEM_TEAMFOUNDATIONSERVERURI']) && (team_project_id = env['SYSTEM_TEAMPROJECTID']) - pipeline_url = "#{team_foundation_server_uri}#{team_project_id}/_build/results?buildId=#{build_id}" - job_url = "#{pipeline_url}&view=logs&j=#{env['SYSTEM_JOBID']}&t=#{env['SYSTEM_TASKINSTANCEID']}" end branch, tag = branch_or_tag( - env['SYSTEM_PULLREQUEST_SOURCEBRANCH'] || - env['BUILD_SOURCEBRANCH'] || - env['BUILD_SOURCEBRANCHNAME'] + env['SYSTEM_PULLREQUEST_SOURCEBRANCH'] || env['BUILD_SOURCEBRANCH'] || env['BUILD_SOURCEBRANCHNAME'] ) { TAG_PROVIDER_NAME => 'azurepipelines', TAG_WORKSPACE_PATH => env['BUILD_SOURCESDIRECTORY'], @@ -154,11 +152,16 @@ || env['BUILD_SOURCEVERSION'], Core::Git::Ext::TAG_BRANCH => branch, Core::Git::Ext::TAG_TAG => tag, Core::Git::Ext::TAG_COMMIT_AUTHOR_NAME => env['BUILD_REQUESTEDFORID'], Core::Git::Ext::TAG_COMMIT_AUTHOR_EMAIL => env['BUILD_REQUESTEDFOREMAIL'], - Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILD_SOURCEVERSIONMESSAGE'] + Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILD_SOURCEVERSIONMESSAGE'], + TAG_CI_ENV_VARS => { + 'SYSTEM_TEAMPROJECTID' => env['SYSTEM_TEAMPROJECTID'], + 'BUILD_BUILDID' => env['BUILD_BUILDID'], + 'SYSTEM_JOBID' => env['SYSTEM_JOBID'] + }.to_json } end def extract_bitbucket(env) url = "https://bitbucket.org/#{env['BITBUCKET_REPO_FULL_NAME']}/addon/pipelines/home#" \ @@ -179,10 +182,28 @@ Core::Git::Ext::TAG_COMMIT_AUTHOR_EMAIL => env['BUILD_REQUESTEDFOREMAIL'], Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILD_SOURCEVERSIONMESSAGE'] } end + def extract_buddy(env) + { + TAG_PROVIDER_NAME => 'buddy', + TAG_PIPELINE_ID => "#{env['BUDDY_PIPELINE_ID']}/#{env['BUDDY_EXECUTION_ID']}", + TAG_PIPELINE_NAME => env['BUDDY_PIPELINE_NAME'], + TAG_PIPELINE_NUMBER => env['BUDDY_EXECUTION_ID'], + TAG_PIPELINE_URL => env['BUDDY_EXECUTION_URL'], + TAG_WORKSPACE_PATH => env['CI_WORKSPACE_PATH'], + Core::Git::Ext::TAG_REPOSITORY_URL => env['BUDDY_SCM_URL'], + Core::Git::Ext::TAG_COMMIT_SHA => env['BUDDY_EXECUTION_REVISION'], + Core::Git::Ext::TAG_BRANCH => env['BUDDY_EXECUTION_BRANCH'], + Core::Git::Ext::TAG_TAG => env['BUDDY_EXECUTION_TAG'], + Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUDDY_EXECUTION_REVISION_MESSAGE'], + Core::Git::Ext::TAG_COMMIT_COMMITTER_NAME => env['BUDDY_EXECUTION_REVISION_COMMITTER_NAME'], + Core::Git::Ext::TAG_COMMIT_COMMITTER_EMAIL => env['BUDDY_EXECUTION_REVISION_COMMITTER_EMAIL'], + } + end + def extract_buildkite(env) { Core::Git::Ext::TAG_BRANCH => env['BUILDKITE_BRANCH'], Core::Git::Ext::TAG_COMMIT_SHA => env['BUILDKITE_COMMIT'], Core::Git::Ext::TAG_REPOSITORY_URL => env['BUILDKITE_REPO'], @@ -194,11 +215,15 @@ TAG_JOB_URL => "#{env['BUILDKITE_BUILD_URL']}##{env['BUILDKITE_JOB_ID']}", TAG_PROVIDER_NAME => 'buildkite', TAG_WORKSPACE_PATH => env['BUILDKITE_BUILD_CHECKOUT_PATH'], Core::Git::Ext::TAG_COMMIT_AUTHOR_NAME => env['BUILDKITE_BUILD_AUTHOR'], Core::Git::Ext::TAG_COMMIT_AUTHOR_EMAIL => env['BUILDKITE_BUILD_AUTHOR_EMAIL'], - Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILDKITE_MESSAGE'] + Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILDKITE_MESSAGE'], + TAG_CI_ENV_VARS => { + 'BUILDKITE_BUILD_ID' => env['BUILDKITE_BUILD_ID'], + 'BUILDKITE_JOB_ID' => env['BUILDKITE_JOB_ID'] + }.to_json } end def extract_circle_ci(env) { @@ -213,11 +238,15 @@ TAG_JOB_URL => env['CIRCLE_BUILD_URL'], TAG_PROVIDER_NAME => 'circleci', TAG_WORKSPACE_PATH => env['CIRCLE_WORKING_DIRECTORY'], Core::Git::Ext::TAG_COMMIT_AUTHOR_NAME => env['BUILD_REQUESTEDFORID'], Core::Git::Ext::TAG_COMMIT_AUTHOR_EMAIL => env['BUILD_REQUESTEDFOREMAIL'], - Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILD_SOURCEVERSIONMESSAGE'] + Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILD_SOURCEVERSIONMESSAGE'], + TAG_CI_ENV_VARS => { + 'CIRCLE_WORKFLOW_ID' => env['CIRCLE_WORKFLOW_ID'], + 'CIRCLE_BUILD_NUM' => env['CIRCLE_BUILD_NUM'] + }.to_json } end def extract_github_actions(env) ref = env['GITHUB_HEAD_REF'] @@ -231,19 +260,26 @@ Core::Git::Ext::TAG_BRANCH => branch, Core::Git::Ext::TAG_COMMIT_SHA => env['GITHUB_SHA'], Core::Git::Ext::TAG_REPOSITORY_URL => "#{env['GITHUB_SERVER_URL']}/#{env['GITHUB_REPOSITORY']}.git", Core::Git::Ext::TAG_TAG => tag, TAG_JOB_URL => "#{env['GITHUB_SERVER_URL']}/#{env['GITHUB_REPOSITORY']}/commit/#{env['GITHUB_SHA']}/checks", + TAG_JOB_NAME => env['GITHUB_JOB'], TAG_PIPELINE_ID => env['GITHUB_RUN_ID'], TAG_PIPELINE_NAME => env['GITHUB_WORKFLOW'], TAG_PIPELINE_NUMBER => env['GITHUB_RUN_NUMBER'], TAG_PIPELINE_URL => pipeline_url, TAG_PROVIDER_NAME => 'github', TAG_WORKSPACE_PATH => env['GITHUB_WORKSPACE'], Core::Git::Ext::TAG_COMMIT_AUTHOR_NAME => env['BUILD_REQUESTEDFORID'], Core::Git::Ext::TAG_COMMIT_AUTHOR_EMAIL => env['BUILD_REQUESTEDFOREMAIL'], - Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILD_SOURCEVERSIONMESSAGE'] + Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILD_SOURCEVERSIONMESSAGE'], + TAG_CI_ENV_VARS => { + 'GITHUB_SERVER_URL' => env['GITHUB_SERVER_URL'], + 'GITHUB_REPOSITORY' => env['GITHUB_REPOSITORY'], + 'GITHUB_RUN_ID' => env['GITHUB_RUN_ID'], + 'GITHUB_RUN_ATTEMPT' => env['GITHUB_RUN_ATTEMPT'], + }.reject { |_k, v| v.nil? }.to_json } end def extract_gitlab(env) commit_author_name, commit_author_email = extract_name_email(env['CI_COMMIT_AUTHOR']) @@ -264,11 +300,16 @@ TAG_PIPELINE_NAME => env['CI_PROJECT_PATH'], TAG_PIPELINE_NUMBER => env['CI_PIPELINE_IID'], TAG_PIPELINE_URL => (url.gsub(%r{/-/pipelines/}, '/pipelines/') if url), TAG_PROVIDER_NAME => 'gitlab', TAG_WORKSPACE_PATH => env['CI_PROJECT_DIR'], - Core::Git::Ext::TAG_COMMIT_MESSAGE => env['CI_COMMIT_MESSAGE'] + Core::Git::Ext::TAG_COMMIT_MESSAGE => env['CI_COMMIT_MESSAGE'], + TAG_CI_ENV_VARS => { + 'CI_PROJECT_URL' => env['CI_PROJECT_URL'], + 'CI_PIPELINE_ID' => env['CI_PIPELINE_ID'], + 'CI_JOB_ID' => env['CI_JOB_ID'] + }.to_json } end def extract_jenkins(env) branch, tag = branch_or_tag(env['GIT_BRANCH']) @@ -288,11 +329,14 @@ TAG_PIPELINE_URL => env['BUILD_URL'], TAG_PROVIDER_NAME => 'jenkins', TAG_WORKSPACE_PATH => env['WORKSPACE'], Core::Git::Ext::TAG_COMMIT_AUTHOR_NAME => env['BUILD_REQUESTEDFORID'], Core::Git::Ext::TAG_COMMIT_AUTHOR_EMAIL => env['BUILD_REQUESTEDFOREMAIL'], - Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILD_SOURCEVERSIONMESSAGE'] + Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILD_SOURCEVERSIONMESSAGE'], + TAG_CI_ENV_VARS => { + 'DD_CUSTOM_TRACE_ID' => env['DD_CUSTOM_TRACE_ID'] + }.to_json } end def extract_teamcity(env) { @@ -301,10 +345,10 @@ Core::Git::Ext::TAG_COMMIT_SHA => env['BUILD_VCS_NUMBER'], TAG_WORKSPACE_PATH => env['BUILD_CHECKOUTDIR'], TAG_PIPELINE_ID => env['BUILD_ID'], TAG_PIPELINE_NUMBER => env['BUILD_NUMBER'], TAG_PIPELINE_URL => ( - env['SERVER_URL'] && env['BUILD_ID'] ? "#{env['SERVER_URL']}/viewLog.html?buildId=#{env['SERVER_URL']}" : nil + env['SERVER_URL'] && env['BUILD_ID'] ? "#{env['SERVER_URL']}/viewLog.html?buildId=#{env['BUILD_ID']}" : nil ), Core::Git::Ext::TAG_COMMIT_AUTHOR_NAME => env['BUILD_REQUESTEDFORID'], Core::Git::Ext::TAG_COMMIT_AUTHOR_EMAIL => env['BUILD_REQUESTEDFOREMAIL'], Core::Git::Ext::TAG_COMMIT_MESSAGE => env['BUILD_SOURCEVERSIONMESSAGE'] }