diff --git a/src/commands/metric/README.md b/src/commands/metric/README.md index 7a50c462f..a5570b431 100644 --- a/src/commands/metric/README.md +++ b/src/commands/metric/README.md @@ -28,10 +28,8 @@ datadog-ci metric --level job --metrics binary.size:1024 ### Supported providers -The metric command only works for the following CI providers: [Buildkite, CircleCI, GitHub, GitLab]. If used in -any other provider it will fail. Note that for GitHub actions only the level `pipeline` is supported. If the -command is invoked in GitHub actions with level `job` it will exit with status code 1 and return an -error. +The metric command only works for the following CI providers: Buildkite, CircleCI, GitHub, GitLab, Azure Pipelines and Jenkins. If used in +any other provider it will fail. ### End-to-end testing process diff --git a/src/commands/metric/__tests__/metric.test.ts b/src/commands/metric/__tests__/metric.test.ts index 6bccb129a..9deea321a 100644 --- a/src/commands/metric/__tests__/metric.test.ts +++ b/src/commands/metric/__tests__/metric.test.ts @@ -92,15 +92,14 @@ describe('execute', () => { ) }) - test('should fail if provider is GitHub and level is job', async () => { + test('should fail if provider is BuddyWorks and level is job', async () => { const {context, code} = await runCLI('job', ['key:1'], { - GITHUB_ACTIONS: 'true', - GITHUB_REPOSITORY: 'example/example', - GITHUB_RUN_ATTEMPT: '10', - GITHUB_RUN_ID: '40', - GITHUB_SERVER_URL: 'github.com', + BUDDY: 'true', + BUDDY_PIPELINE_ID: 'example/example', + BUDDY_EXECUTION_ID: '10', + BUDDY_EXECUTION_START_DATE: '2023-03-08T00:00:00Z', }) expect(code).toBe(1) - expect(context.stderr.toString()).toContain('Cannot use level "job" for GitHub Actions.') + expect(context.stderr.toString()).toContain('Cannot use level "job" for Buddy.') }) }) diff --git a/src/commands/metric/metric.ts b/src/commands/metric/metric.ts index a8bc6dc32..9234cbba0 100644 --- a/src/commands/metric/metric.ts +++ b/src/commands/metric/metric.ts @@ -63,8 +63,8 @@ export class MetricCommand extends Command { try { const metrics = parseMetrics(this.metrics) const {provider, ciEnv} = getCIEnv() - // For GitHub and Buddy only the pipeline level is supported as there is no way to identify the job from the runner. - if ((provider === 'github' || provider === 'buddy') && this.level === 'job') { + // For Buddy only the pipeline level is supported as there is no way to identify the job from the runner. + if (provider === 'buddy' && this.level === 'job') { this.context.stderr.write( `${chalk.red.bold('[ERROR]')} Cannot use level "job" for ${PROVIDER_TO_DISPLAY_NAME[provider]}.` ) diff --git a/src/commands/tag/README.md b/src/commands/tag/README.md index 5ac2b4d99..e17a68276 100644 --- a/src/commands/tag/README.md +++ b/src/commands/tag/README.md @@ -29,10 +29,8 @@ datadog-ci tag --level job --tags "go.version:`go version`" ### Supported providers -The tag command only works for the following CI providers: [Buildkite, CircleCI, GitHub, GitLab]. If used in -any other provider it will fail. Note that for GitHub actions only the level `pipeline` is supported. If the -command is invoked in GitHub actions with level `job` it will exit with status code 1 and return an -error. +The tag command only works for the following CI providers: Buildkite, CircleCI, GitHub, GitLab, Azure Pipelines and Jenkins. If used in +any other provider it will fail. ### End-to-end testing process diff --git a/src/commands/tag/__tests__/tag.test.ts b/src/commands/tag/__tests__/tag.test.ts index b5eb433fa..4c168f6d2 100644 --- a/src/commands/tag/__tests__/tag.test.ts +++ b/src/commands/tag/__tests__/tag.test.ts @@ -71,15 +71,14 @@ describe('execute', () => { ) }) - test('should fail if provider is GitHub and level is job', async () => { + test('should fail if provider is BuddyWorks and level is job', async () => { const {context, code} = await runCLI('job', ['key:value'], { - GITHUB_ACTIONS: 'true', - GITHUB_REPOSITORY: 'example/example', - GITHUB_RUN_ATTEMPT: '10', - GITHUB_RUN_ID: '40', - GITHUB_SERVER_URL: 'github.com', + BUDDY: 'true', + BUDDY_PIPELINE_ID: 'example/example', + BUDDY_EXECUTION_ID: '10', + BUDDY_EXECUTION_START_DATE: '2023-03-08T00:00:00Z', }) expect(code).toBe(1) - expect(context.stderr.toString()).toContain('Cannot use level "job" for GitHub Actions.') + expect(context.stderr.toString()).toContain('Cannot use level "job" for Buddy.') }) }) diff --git a/src/commands/tag/tag.ts b/src/commands/tag/tag.ts index 7b007cd1f..6c25bb3e8 100644 --- a/src/commands/tag/tag.ts +++ b/src/commands/tag/tag.ts @@ -65,8 +65,8 @@ export class TagCommand extends Command { try { const {provider, ciEnv} = getCIEnv() - // For GitHub and Buddy only the pipeline level is supported as there is no way to identify the job from the runner. - if ((provider === 'github' || provider === 'buddy') && this.level === 'job') { + // For BuddyWorks only the pipeline level is supported as there is no way to identify the job from the runner. + if (provider === 'buddy' && this.level === 'job') { this.context.stderr.write( `${chalk.red.bold('[ERROR]')} Cannot use level "job" for ${PROVIDER_TO_DISPLAY_NAME[provider]}.` ) diff --git a/src/helpers/ci.ts b/src/helpers/ci.ts index 6dd5bf197..e814063bf 100644 --- a/src/helpers/ci.ts +++ b/src/helpers/ci.ts @@ -48,6 +48,10 @@ export const PROVIDER_TO_DISPLAY_NAME = { buddy: 'Buddy', } +// DD_GITHUB_JOB_NAME is an override that is required for adding custom tags and metrics +// to GHA jobs if the 'name' property is used. It's ok for it to be missing in case the name property is not used. +const envAllowedToBeMissing = ['DD_GITHUB_JOB_NAME'] + // Receives a string with the form 'John Doe ' // and returns { name: 'John Doe', email: 'john.doe@gmail.com' } const parseEmailAndName = (emailAndName: string | undefined) => { @@ -713,7 +717,14 @@ export const getCIEnv = (): {ciEnv: Record; provider: string} => if (process.env.GITHUB_ACTIONS || process.env.GITHUB_ACTION) { return { - ciEnv: filterEnv(['GITHUB_SERVER_URL', 'GITHUB_REPOSITORY', 'GITHUB_RUN_ID', 'GITHUB_RUN_ATTEMPT']), + ciEnv: filterEnv([ + 'GITHUB_SERVER_URL', + 'GITHUB_REPOSITORY', + 'GITHUB_RUN_ID', + 'GITHUB_RUN_ATTEMPT', + 'GITHUB_JOB', + 'DD_GITHUB_JOB_NAME', + ]), provider: 'github', } } @@ -760,20 +771,20 @@ export const getCIEnv = (): {ciEnv: Record; provider: string} => const filterEnv = (values: string[]): Record => { const ciEnvs: Record = {} - const missing: string[] = [] + const requiredMissing: string[] = [] values.forEach((envKey) => { const envValue = process.env[envKey] if (envValue) { ciEnvs[envKey] = envValue - } else { - missing.push(envKey) + } else if (!envAllowedToBeMissing.includes(envKey)) { + requiredMissing.push(envKey) } }) - if (missing.length > 0) { + if (requiredMissing.length > 0) { // Get the missing values for better error - throw new Error(`Missing environment variables [${missing.toString()}]`) + throw new Error(`Missing environment variables [${requiredMissing.toString()}]`) } return ciEnvs