Skip to content
v2.273.0

AWS CodeCommit Construct Library ​

AWS CodeCommit is a version control service that enables you to privately store and manage Git repositories in the AWS cloud.

For further information on CodeCommit, see the AWS CodeCommit documentation.

To add a CodeCommit Repository to your stack:

ts
const repo = new codecommit.Repository(this, 'Repository', {
  repositoryName: 'MyRepositoryName',
  description: 'Some description.', // optional property
});
python
repo = codecommit.Repository(self, "Repository",
    repository_name="MyRepositoryName",
    description="Some description."
)
java
Repository repo = Repository.Builder.create(this, "Repository")
        .repositoryName("MyRepositoryName")
        .description("Some description.")
        .build();
csharp
var repo = new Repository(this, "Repository", new RepositoryProps {
    RepositoryName = "MyRepositoryName",
    Description = "Some description."
});
go
repo := codecommit.NewRepository(this, jsii.String("Repository"), &RepositoryProps{
	RepositoryName: jsii.String("MyRepositoryName"),
	Description: jsii.String("Some description."),
})

Use the repositoryCloneUrlHttp, repositoryCloneUrlSsh or repositoryCloneUrlGrc property to clone your repository.

To add an Amazon SNS trigger to your repository:

ts
declare const repo: codecommit.Repository;

// trigger is established for all repository actions on all branches by default.
repo.notify('arn:aws:sns:*:123456789012:my_topic');
python
# repo: codecommit.Repository


# trigger is established for all repository actions on all branches by default.
repo.notify("arn:aws:sns:*:123456789012:my_topic")
java
Repository repo;


// trigger is established for all repository actions on all branches by default.
repo.notify("arn:aws:sns:*:123456789012:my_topic");
csharp
Repository repo;


// trigger is established for all repository actions on all branches by default.
repo.Notify("arn:aws:sns:*:123456789012:my_topic");
go
var repo Repository


// trigger is established for all repository actions on all branches by default.
repo.Notify(jsii.String("arn:aws:sns:*:123456789012:my_topic"))

Add initial commit ​

It is possible to initialize the Repository via the Code class. It provides methods for loading code from a directory, .zip file and from a pre-created CDK Asset.

Example:

ts
const repo = new codecommit.Repository(this, 'Repository', {
  repositoryName: 'MyRepositoryName',
  code: codecommit.Code.fromDirectory(path.join(__dirname, 'directory/'), 'develop'), // optional property, branch parameter can be omitted
});
python
repo = codecommit.Repository(self, "Repository",
    repository_name="MyRepositoryName",
    code=codecommit.Code.from_directory(path.join(__dirname, "directory/"), "develop")
)
java
Repository repo = Repository.Builder.create(this, "Repository")
        .repositoryName("MyRepositoryName")
        .code(Code.fromDirectory(join(__dirname, "directory/"), "develop"))
        .build();
csharp
var repo = new Repository(this, "Repository", new RepositoryProps {
    RepositoryName = "MyRepositoryName",
    Code = Code.FromDirectory(Join(__dirname, "directory/"), "develop")
});
go
repo := codecommit.NewRepository(this, jsii.String("Repository"), &RepositoryProps{
	RepositoryName: jsii.String("MyRepositoryName"),
	Code: codecommit.Code_FromDirectory(path.join(__dirname, jsii.String("directory/")), jsii.String("develop")),
})

Use a customer managed key ​

CodeCommit repositories are automatically encrypted with an AWS managed key. To use a customer managed key, specify the kmsKey property.

For more information, see AWS Key Management Service and encryption for AWS CodeCommit repositories.

ts
import * as kms from 'aws-cdk-lib/aws-kms';

declare const kmsKey: kms.IKey;

const repo = new codecommit.Repository(this, 'Repository', {
  repositoryName: 'MyRepositoryName',
  kmsKey,
});
python
import aws_cdk.aws_kms as kms

# kms_key: kms.IKey


repo = codecommit.Repository(self, "Repository",
    repository_name="MyRepositoryName",
    kms_key=kms_key
)
java
import software.amazon.awscdk.services.kms.*;

IKey kmsKey;


Repository repo = Repository.Builder.create(this, "Repository")
        .repositoryName("MyRepositoryName")
        .kmsKey(kmsKey)
        .build();
csharp
using Amazon.CDK.AWS.KMS;

IKey kmsKey;


var repo = new Repository(this, "Repository", new RepositoryProps {
    RepositoryName = "MyRepositoryName",
    KmsKey = kmsKey
});
go
import kms "github.com/aws/aws-cdk-go/awscdk"

var kmsKey IKey


repo := codecommit.NewRepository(this, jsii.String("Repository"), &RepositoryProps{
	RepositoryName: jsii.String("MyRepositoryName"),
	KmsKey: KmsKey,
})

Events ​

CodeCommit repositories emit Amazon CloudWatch events for certain activities. Use the repo.onXxx methods to define rules that trigger on these events and invoke targets as a result:

ts
import * as sns from 'aws-cdk-lib/aws-sns';
import * as targets from 'aws-cdk-lib/aws-events-targets';

declare const repo: codecommit.Repository;
declare const project: codebuild.PipelineProject;
declare const myTopic: sns.Topic;

// starts a CodeBuild project when a commit is pushed to the "main" branch of the repo
repo.onCommit('CommitToMain', {
  target: new targets.CodeBuildProject(project),
  branches: ['main'],
});

// publishes a message to an Amazon SNS topic when a comment is made on a pull request
const rule = repo.onCommentOnPullRequest('CommentOnPullRequest', {
  target: new targets.SnsTopic(myTopic),
});
python
import aws_cdk.aws_sns as sns
import aws_cdk.aws_events_targets as targets

# repo: codecommit.Repository
# project: codebuild.PipelineProject
# my_topic: sns.Topic


# starts a CodeBuild project when a commit is pushed to the "main" branch of the repo
repo.on_commit("CommitToMain",
    target=targets.CodeBuildProject(project),
    branches=["main"]
)

# publishes a message to an Amazon SNS topic when a comment is made on a pull request
rule = repo.on_comment_on_pull_request("CommentOnPullRequest",
    target=targets.SnsTopic(my_topic)
)
java
import software.amazon.awscdk.services.sns.*;
import software.amazon.awscdk.services.events.targets.*;

Repository repo;
PipelineProject project;
Topic myTopic;


// starts a CodeBuild project when a commit is pushed to the "main" branch of the repo
repo.onCommit("CommitToMain", OnCommitOptions.builder()
        .target(new CodeBuildProject(project))
        .branches(List.of("main"))
        .build());

// publishes a message to an Amazon SNS topic when a comment is made on a pull request
Rule rule = repo.onCommentOnPullRequest("CommentOnPullRequest", OnEventOptions.builder()
        .target(new SnsTopic(myTopic))
        .build());
csharp
using Amazon.CDK.AWS.SNS;
using Amazon.CDK.AWS.Events.Targets;

Repository repo;
PipelineProject project;
Topic myTopic;


// starts a CodeBuild project when a commit is pushed to the "main" branch of the repo
repo.OnCommit("CommitToMain", new OnCommitOptions {
    Target = new CodeBuildProject(project),
    Branches = new [] { "main" }
});

// publishes a message to an Amazon SNS topic when a comment is made on a pull request
var rule = repo.OnCommentOnPullRequest("CommentOnPullRequest", new OnEventOptions {
    Target = new SnsTopic(myTopic)
});
go
import sns "github.com/aws/aws-cdk-go/awscdk"
import "github.com/aws/aws-cdk-go/awscdk"

var repo Repository
var project PipelineProject
var myTopic Topic


// starts a CodeBuild project when a commit is pushed to the "main" branch of the repo
repo.onCommit(jsii.String("CommitToMain"), &OnCommitOptions{
	Target: targets.NewCodeBuildProject(project),
	Branches: []*string{
		jsii.String("main"),
	},
})

// publishes a message to an Amazon SNS topic when a comment is made on a pull request
rule := repo.onCommentOnPullRequest(jsii.String("CommentOnPullRequest"), &OnEventOptions{
	Target: targets.NewSnsTopic(myTopic),
})

CodeStar Notifications ​

To define CodeStar Notification rules for Repositories, use one of the notifyOnXxx() methods. They are very similar to onXxx() methods for CloudWatch events:

ts
import * as chatbot from 'aws-cdk-lib/aws-chatbot';

declare const repository: codecommit.Repository;
const target = new chatbot.SlackChannelConfiguration(this, 'MySlackChannel', {
  slackChannelConfigurationName: 'YOUR_CHANNEL_NAME',
  slackWorkspaceId: 'YOUR_SLACK_WORKSPACE_ID',
  slackChannelId: 'YOUR_SLACK_CHANNEL_ID',
});
const rule = repository.notifyOnPullRequestCreated('NotifyOnPullRequestCreated', target);
python
import aws_cdk.aws_chatbot as chatbot

# repository: codecommit.Repository

target = chatbot.SlackChannelConfiguration(self, "MySlackChannel",
    slack_channel_configuration_name="YOUR_CHANNEL_NAME",
    slack_workspace_id="YOUR_SLACK_WORKSPACE_ID",
    slack_channel_id="YOUR_SLACK_CHANNEL_ID"
)
rule = repository.notify_on_pull_request_created("NotifyOnPullRequestCreated", target)
java
import software.amazon.awscdk.services.chatbot.*;

Repository repository;

SlackChannelConfiguration target = SlackChannelConfiguration.Builder.create(this, "MySlackChannel")
        .slackChannelConfigurationName("YOUR_CHANNEL_NAME")
        .slackWorkspaceId("YOUR_SLACK_WORKSPACE_ID")
        .slackChannelId("YOUR_SLACK_CHANNEL_ID")
        .build();
INotificationRule rule = repository.notifyOnPullRequestCreated("NotifyOnPullRequestCreated", target);
csharp
using Amazon.CDK.AWS.Chatbot;

Repository repository;

var target = new SlackChannelConfiguration(this, "MySlackChannel", new SlackChannelConfigurationProps {
    SlackChannelConfigurationName = "YOUR_CHANNEL_NAME",
    SlackWorkspaceId = "YOUR_SLACK_WORKSPACE_ID",
    SlackChannelId = "YOUR_SLACK_CHANNEL_ID"
});
var rule = repository.NotifyOnPullRequestCreated("NotifyOnPullRequestCreated", target);
go
import chatbot "github.com/aws/aws-cdk-go/awscdk"

var repository Repository

target := chatbot.NewSlackChannelConfiguration(this, jsii.String("MySlackChannel"), &SlackChannelConfigurationProps{
	SlackChannelConfigurationName: jsii.String("YOUR_CHANNEL_NAME"),
	SlackWorkspaceId: jsii.String("YOUR_SLACK_WORKSPACE_ID"),
	SlackChannelId: jsii.String("YOUR_SLACK_CHANNEL_ID"),
})
rule := repository.notifyOnPullRequestCreated(jsii.String("NotifyOnPullRequestCreated"), target)

Types ​

NameKindSummary
RepositoryConstruct (L2)Provides a CodeCommit Repository.
CfnRepositoryConstruct (L1)Creates a new, empty repository.
CfnRepository.CodePropertyStructInformation about code to be committed.
CfnRepository.RepositoryTriggerPropertyStructInformation about a trigger for a repository.
CfnRepository.S3PropertyStructInformation about the Amazon S3 bucket that contains the code that will be committed to the new repository.
CfnRepositoryPropsStructProperties for defining a CfnRepository.
CodeClassRepresents the contents to initialize the repository with.
CodeConfigStructRepresents the structure to pass into the underlying CfnRepository class.
IRepositoryInterface
OnCommitOptionsStructOptions for the onCommit() method.
ReferenceEventClassFields of CloudWatch Events that change references.
RepositoryEventTriggerEnumRepository events that will cause the trigger to run actions in another service.
RepositoryGrantsFacadeCollection of grant methods for a IRepositoryRef.
RepositoryNotificationEventsEnumList of event types for AWS CodeCommit.
RepositoryNotifyOnOptionsStructAdditional options to pass to the notification rule.
RepositoryPropsStruct
RepositoryTriggerOptionsStructCreates for a repository trigger to an SNS topic or Lambda function.