Skip to content

Commit

Permalink
feat(ci_visibility): add support for AWS codepipeline to CI env var g…
Browse files Browse the repository at this point in the history
…athering (#7269)

This adds support for gathering CI environment variables for AWS
Codepipeline (although it's noteworthy that most of the environment
variables need to be provided by the user).

## Checklist

- [x] Change(s) are motivated and described in the PR description.
- [x] Testing strategy is described if automated tests are not included
in the PR.
- [x] Risk is outlined (performance impact, potential for breakage,
maintainability, etc).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed. If no release note is required, add label
`changelog/no-changelog`.
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/)).
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))

## Reviewer Checklist

- [x] Title is accurate.
- [x] No unnecessary changes are introduced.
- [x] Description motivates each change.
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes unless absolutely necessary.
- [x] Testing strategy adequately addresses listed risk(s).
- [x] Change is maintainable (easy to change, telemetry, documentation).
- [x] Release note makes sense to a user of the library.
- [x] Reviewer has explicitly acknowledged and discussed the performance
implications of this PR as reported in the benchmarks PR comment.
- [x] Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)
- [x] If this PR touches code that signs or publishes builds or
packages, or handles credentials of any kind, I've requested a review
from `@DataDog/security-design-and-guidance`.
- [x] This PR doesn't touch any of that.

---------

Co-authored-by: Tahir H. Butt <[email protected]>
  • Loading branch information
romainkomorn-exdatadog and majorgreys authored Oct 24, 2023
1 parent 77540c1 commit d8d45fe
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ddtrace/ext/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,34 @@ def extract_buddy(env):
}


def extract_codebuild(env):
# type: (MutableMapping[str, str]) -> Dict[str, Optional[str]]
"""Extract CI tags from codebuild environments."""

tags = {}

# AWS Codepipeline
if "CODEBUILD_INITIATOR" in env:
codebuild_initiator = env.get("CODEBUILD_INITIATOR")
if codebuild_initiator and codebuild_initiator.startswith("codepipeline"):
tags.update(
{
PROVIDER_NAME: "awscodepipeline",
PIPELINE_ID: env.get("DD_PIPELINE_EXECUTION_ID"),
_CI_ENV_VARS: json.dumps(
{
"CODEBUILD_BUILD_ARN": env.get("CODEBUILD_BUILD_ARN"),
"DD_PIPELINE_EXECUTION_ID": env.get("DD_PIPELINE_EXECUTION_ID"),
"DD_ACTION_EXECUTION_ID": env.get("DD_ACTION_EXECUTION_ID"),
},
separators=(",", ":"),
),
}
)

return tags


PROVIDERS = (
("APPVEYOR", extract_appveyor),
("TF_BUILD", extract_azure_pipelines),
Expand All @@ -542,4 +570,5 @@ def extract_buddy(env):
("TRAVIS", extract_travis),
("BITRISE_BUILD_SLUG", extract_bitrise),
("BUDDY", extract_buddy),
("CODEBUILD_INITIATOR", extract_codebuild),
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
features:
- |
CI Visibility: add support for AWS Codepipeline to CI env var gathering
62 changes: 62 additions & 0 deletions tests/tracer/fixtures/ci/codebuild.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
[
{
"CODEBUILD_BUILD_ARN": "arn:aws:codebuild:eu-north-1:12345678:build/codebuild-demo-project:b1e6661e-e4f2-4156-9ab9-82a19",
"CODEBUILD_INITIATOR": "codepipeline/test-pipeline",
"DD_ACTION_EXECUTION_ID": "35519dc3-7c45-493c-9ba6-cd78ea11f69d",
"DD_GIT_BRANCH": "user-supplied-branch",
"DD_GIT_COMMIT_AUTHOR_DATE": "usersupplied-authordate",
"DD_GIT_COMMIT_AUTHOR_EMAIL": "usersupplied-authoremail",
"DD_GIT_COMMIT_AUTHOR_NAME": "usersupplied-authorname",
"DD_GIT_COMMIT_COMMITTER_DATE": "usersupplied-comitterdate",
"DD_GIT_COMMIT_COMMITTER_EMAIL": "usersupplied-comitteremail",
"DD_GIT_COMMIT_COMMITTER_NAME": "usersupplied-comittername",
"DD_GIT_COMMIT_MESSAGE": "usersupplied-message",
"DD_GIT_COMMIT_SHA": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"DD_GIT_REPOSITORY_URL": "[email protected]:DataDog/userrepo.git",
"DD_PIPELINE_EXECUTION_ID": "bb1f15ed-fde2-494d-8e13-88785bca9cc0"
},
{
"_dd.ci.env_vars": "{\"CODEBUILD_BUILD_ARN\":\"arn:aws:codebuild:eu-north-1:12345678:build/codebuild-demo-project:b1e6661e-e4f2-4156-9ab9-82a19\",\"DD_PIPELINE_EXECUTION_ID\":\"bb1f15ed-fde2-494d-8e13-88785bca9cc0\",\"DD_ACTION_EXECUTION_ID\":\"35519dc3-7c45-493c-9ba6-cd78ea11f69d\"}",
"ci.pipeline.id": "bb1f15ed-fde2-494d-8e13-88785bca9cc0",
"ci.provider.name": "awscodepipeline",
"git.branch": "user-supplied-branch",
"git.commit.author.date": "usersupplied-authordate",
"git.commit.author.email": "usersupplied-authoremail",
"git.commit.author.name": "usersupplied-authorname",
"git.commit.committer.date": "usersupplied-comitterdate",
"git.commit.committer.email": "usersupplied-comitteremail",
"git.commit.committer.name": "usersupplied-comittername",
"git.commit.message": "usersupplied-message",
"git.commit.sha": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"git.repository_url": "[email protected]:DataDog/userrepo.git"
}
],
[
{
"CODEBUILD_INITIATOR": "lambdafunction/test-lambda",
"DD_GIT_BRANCH": "user-supplied-branch",
"DD_GIT_COMMIT_AUTHOR_DATE": "usersupplied-authordate",
"DD_GIT_COMMIT_AUTHOR_EMAIL": "usersupplied-authoremail",
"DD_GIT_COMMIT_AUTHOR_NAME": "usersupplied-authorname",
"DD_GIT_COMMIT_COMMITTER_DATE": "usersupplied-comitterdate",
"DD_GIT_COMMIT_COMMITTER_EMAIL": "usersupplied-comitteremail",
"DD_GIT_COMMIT_COMMITTER_NAME": "usersupplied-comittername",
"DD_GIT_COMMIT_MESSAGE": "usersupplied-message",
"DD_GIT_COMMIT_SHA": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"DD_GIT_REPOSITORY_URL": "[email protected]:DataDog/userrepo.git"
},
{
"git.branch": "user-supplied-branch",
"git.commit.author.date": "usersupplied-authordate",
"git.commit.author.email": "usersupplied-authoremail",
"git.commit.author.name": "usersupplied-authorname",
"git.commit.committer.date": "usersupplied-comitterdate",
"git.commit.committer.email": "usersupplied-comitteremail",
"git.commit.committer.name": "usersupplied-comittername",
"git.commit.message": "usersupplied-message",
"git.commit.sha": "b9f0fb3fdbb94c9d24b2c75b49663122a529e123",
"git.repository_url": "[email protected]:DataDog/userrepo.git"
}
]
]

0 comments on commit d8d45fe

Please sign in to comment.