Skip to content

feat(core): add git source metadata to CloudFormation templates#37368

Open
otaviomacedo wants to merge 10 commits intomainfrom
otaviom/git-source
Open

feat(core): add git source metadata to CloudFormation templates#37368
otaviomacedo wants to merge 10 commits intomainfrom
otaviom/git-source

Conversation

@otaviomacedo
Copy link
Copy Markdown
Contributor

@otaviomacedo otaviomacedo commented Mar 26, 2026

Synthesized CloudFormation templates now include an AWS::CloudFormation::Source
entry in the top-level Metadata section containing the git remote repository URL and
the latest commit hash. This helps trace deployed stacks back to their source code.

Example output:

Metadata:
  AWS::CloudFormation::Source:
    Repository: git@github.com:org/app.git
    Commit: 19e3e594a5512b75074526af89bf3b67a3164cc1

The metadata is also added to the cloud assembly.

The metadata is generated by default. It can be suppressed by setting the
CDK_DISABLE_GIT_SOURCE environment variable.

If the current directory is not a git repository or git is not available, the metadata entry
is silently omitted.


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

Synthesized CloudFormation templates now include an `AWS::CloudFormation::Source`
entry in the top-level `Metadata` section containing the git remote repository
URL and the latest commit hash. This helps trace deployed stacks back to their
source code.

Example output:

    Metadata:
      AWS::CloudFormation::Source:
        Repository: git@github.com:org/app.git
        Commit: 19e3e594a5512b75074526af89bf3b67a3164cc1

The metadata is generated by default. It can be suppressed by:
- Setting the `CDK_DISABLE_GIT_SOURCE` environment variable
- Setting the `@aws-cdk/core:disableGitSource` context key to `true`

If the current directory is not a git repository or git is not available,
the metadata entry is silently omitted.
@otaviomacedo otaviomacedo requested a review from a team as a code owner March 26, 2026 13:32
@aws-cdk-automation aws-cdk-automation requested a review from a team March 26, 2026 13:32
@github-actions github-actions bot added the p2 label Mar 26, 2026
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Mar 26, 2026
Copy link
Copy Markdown
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter fails with the following errors:

❌ Features must contain a change to an integration test file and the resulting snapshot.

If you believe this pull request should receive an exemption, please comment and provide a justification. A comment requesting an exemption should contain the text Exemption Request. Additionally, if clarification is needed, add Clarification Request to a comment.

@hoegertn
Copy link
Copy Markdown
Contributor

Is there a way to get that information on a stack or app?
I'm working on a similar thing that contains git information and also versioning information, and I want to also be able to use that programmatically in my CDK application.

https://github.com/open-constructs/cdk-devops (Sorry no docs yet)

Add it to the cloud assembly metadata
Copy link
Copy Markdown
Contributor

@mrgrain mrgrain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The metadata is generated by default. It can be suppressed by setting the
CDK_DISABLE_GIT_SOURCE environment variable.

Why an env variable? shouldn't this be an option/context value to be more inline with other optional features?

// add the permissions boundary aspect
this.addPermissionsBoundaryAspect();

const gitSource = getGitSource();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably be able to make this configurable at the App level.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but if it's enabled by default, we would have to disable it in every integ test, for example. I had this in some iteration, but decided against it when the PR had 1000+ changes, and GitHub would truncate it and not even get to the important bits :/

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should have a mechanism to make config changes to every integ test. Might need a separate PR.

@@ -0,0 +1,3 @@
// Disable git source metadata in CloudFormation templates during tests
// to avoid breaking existing snapshot/assertion tests.
process.env.CDK_DISABLE_GIT_SOURCE = '1';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid negative parameters, something like:

Suggested change
process.env.CDK_DISABLE_GIT_SOURCE = '1';
process.env.CDK_GIT_SOURCE = '0';

?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

contribution/core This is a PR that came from AWS. p2

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants