Skip to content

Commit

Permalink
Merge pull request #1161 from DataDog/andrea.moscatelli/support-custo…
Browse files Browse the repository at this point in the history
…m-tags-for-github-jobs

Support custom tags and metrics for GH jobs
  • Loading branch information
andrea-mosk authored Jan 26, 2024
2 parents c0a0cc6 + f51cbd1 commit c870b20
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 32 deletions.
6 changes: 2 additions & 4 deletions src/commands/metric/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 6 additions & 7 deletions src/commands/metric/__tests__/metric.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
})
})
4 changes: 2 additions & 2 deletions src/commands/metric/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]}.`
)
Expand Down
6 changes: 2 additions & 4 deletions src/commands/tag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 6 additions & 7 deletions src/commands/tag/__tests__/tag.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
})
})
4 changes: 2 additions & 2 deletions src/commands/tag/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]}.`
)
Expand Down
23 changes: 17 additions & 6 deletions src/helpers/ci.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>'
// and returns { name: 'John Doe', email: 'john.doe@gmail.com' }
const parseEmailAndName = (emailAndName: string | undefined) => {
Expand Down Expand Up @@ -713,7 +717,14 @@ export const getCIEnv = (): {ciEnv: Record<string, string>; 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',
}
}
Expand Down Expand Up @@ -760,20 +771,20 @@ export const getCIEnv = (): {ciEnv: Record<string, string>; provider: string} =>

const filterEnv = (values: string[]): Record<string, string> => {
const ciEnvs: Record<string, string> = {}
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
Expand Down

0 comments on commit c870b20

Please sign in to comment.