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:
const repo = new codecommit.Repository(this, 'Repository', {
repositoryName: 'MyRepositoryName',
description: 'Some description.', // optional property
});
repo = codecommit.Repository(self, "Repository",
repository_name="MyRepositoryName",
description="Some description."
)
Repository repo = Repository.Builder.create(this, "Repository")
.repositoryName("MyRepositoryName")
.description("Some description.")
.build();
var repo = new Repository(this, "Repository", new RepositoryProps {
RepositoryName = "MyRepositoryName",
Description = "Some description."
});
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:
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');
# repo: codecommit.Repository
# trigger is established for all repository actions on all branches by default.
repo.notify("arn:aws:sns:*:123456789012:my_topic")
Repository repo;
// trigger is established for all repository actions on all branches by default.
repo.notify("arn:aws:sns:*:123456789012:my_topic");
Repository repo;
// trigger is established for all repository actions on all branches by default.
repo.Notify("arn:aws:sns:*:123456789012:my_topic");
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:
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
});
repo = codecommit.Repository(self, "Repository",
repository_name="MyRepositoryName",
code=codecommit.Code.from_directory(path.join(__dirname, "directory/"), "develop")
)
Repository repo = Repository.Builder.create(this, "Repository")
.repositoryName("MyRepositoryName")
.code(Code.fromDirectory(join(__dirname, "directory/"), "develop"))
.build();
var repo = new Repository(this, "Repository", new RepositoryProps {
RepositoryName = "MyRepositoryName",
Code = Code.FromDirectory(Join(__dirname, "directory/"), "develop")
});
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.
import * as kms from 'aws-cdk-lib/aws-kms';
declare const kmsKey: kms.IKey;
const repo = new codecommit.Repository(this, 'Repository', {
repositoryName: 'MyRepositoryName',
kmsKey,
});
import aws_cdk.aws_kms as kms
# kms_key: kms.IKey
repo = codecommit.Repository(self, "Repository",
repository_name="MyRepositoryName",
kms_key=kms_key
)
import software.amazon.awscdk.services.kms.*;
IKey kmsKey;
Repository repo = Repository.Builder.create(this, "Repository")
.repositoryName("MyRepositoryName")
.kmsKey(kmsKey)
.build();
using Amazon.CDK.AWS.KMS;
IKey kmsKey;
var repo = new Repository(this, "Repository", new RepositoryProps {
RepositoryName = "MyRepositoryName",
KmsKey = kmsKey
});
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:
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),
});
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)
)
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());
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)
});
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:
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);
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)
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);
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);
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
Constructs (L2)
RepositoryConstruct (L2)
Provides a CodeCommit Repository.
Constructs (L1)
CfnRepositoryConstruct (L1)
Creates a new, empty repository.
Structs
CfnRepository.CodePropertyStruct
Information about code to be committed.CfnRepository.StructRepositoryTriggerProperty
Information about a trigger for a repository.CfnRepository.S3PropertyStruct
Information about the Amazon S3 bucket that contains the code that will be committed to the new repository.CfnRepositoryPropsStruct
Properties for defining aCfnRepository.CodeConfigStruct
Represents the structure to pass into the underlying CfnRepository class.OnCommitOptionsStruct
Options for the onCommit() method.RepositoryNotifyOnOptionsStruct
Additional options to pass to the notification rule.RepositoryPropsStructRepositoryTriggerOptionsStruct
Creates for a repository trigger to an SNS topic or Lambda function.
Classes
CodeClass
Represents the contents to initialize the repository with.ReferenceEventClass
Fields of CloudWatch Events that change references.
Interfaces
IRepositoryInterface
Enums
RepositoryEventTriggerEnum
Repository events that will cause the trigger to run actions in another service.RepositoryNotificationEventsEnum
List of event types for AWS CodeCommit.
Facades
RepositoryGrantsFacade
Collection of grant methods for a IRepositoryRef.