# CodeCommitSourceAction

CodePipeline Source that is provided by an AWS CodeCommit repository.

If the CodeCommit repository is in a different account, you must use
`CodeCommitTrigger.EVENTS` to trigger the pipeline.

(That is because the Pipeline structure normally only has a `RepositoryName`
field, and that is not enough for the pipeline to locate the repository's
source account. However, if the pipeline is triggered via an EventBridge
event, the event itself has the full repository ARN in there, allowing the
pipeline to locate the repository).

Extends: [Action](/cdk/api/v2/docs/aws-cdk-lib.aws_codepipeline.Action.html) › [Action](/cdk/api/v2/docs/aws-cdk-lib.aws_codepipeline_actions.Action.html) › CodeCommitSourceAction
Implements: [IAction](/cdk/api/v2/docs/aws-cdk-lib.aws_codepipeline.IAction.html)

- Source: [`aws-codepipeline-actions/lib/codecommit/source-action.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-codepipeline-actions/lib/codecommit/source-action.ts#L156)

## Example

TypeScript:

```ts
// Source stage: read from repository
const repo = new codecommit.Repository(stack, 'TemplateRepo', {
  repositoryName: 'template-repo',
});
const sourceOutput = new codepipeline.Artifact('SourceArtifact');
const source = new cpactions.CodeCommitSourceAction({
  actionName: 'Source',
  repository: repo,
  output: sourceOutput,
  trigger: cpactions.CodeCommitTrigger.POLL,
});
const sourceStage = {
  stageName: 'Source',
  actions: [source],
};

// Deployment stage: create and deploy changeset with manual approval
const stackName = 'OurStack';
const changeSetName = 'StagedChangeSet';

const prodStage = {
  stageName: 'Deploy',
  actions: [
    new cpactions.CloudFormationCreateReplaceChangeSetAction({
      actionName: 'PrepareChanges',
      stackName,
      changeSetName,
      adminPermissions: true,
      templatePath: sourceOutput.atPath('template.yaml'),
      runOrder: 1,
    }),
    new cpactions.ManualApprovalAction({
      actionName: 'ApproveChanges',
      runOrder: 2,
    }),
    new cpactions.CloudFormationExecuteChangeSetAction({
      actionName: 'ExecuteChanges',
      stackName,
      changeSetName,
      runOrder: 3,
    }),
  ],
};

new codepipeline.Pipeline(stack, 'Pipeline', {
  crossAccountKeys: true,
  stages: [
    sourceStage,
    prodStage,
  ],
});
```

Python:

```python
# Source stage: read from repository
repo = codecommit.Repository(stack, "TemplateRepo",
    repository_name="template-repo"
)
source_output = codepipeline.Artifact("SourceArtifact")
source = cpactions.CodeCommitSourceAction(
    action_name="Source",
    repository=repo,
    output=source_output,
    trigger=cpactions.CodeCommitTrigger.POLL
)
source_stage = {
    "stage_name": "Source",
    "actions": [source]
}

# Deployment stage: create and deploy changeset with manual approval
stack_name = "OurStack"
change_set_name = "StagedChangeSet"

prod_stage = {
    "stage_name": "Deploy",
    "actions": [
        cpactions.CloudFormationCreateReplaceChangeSetAction(
            action_name="PrepareChanges",
            stack_name=stack_name,
            change_set_name=change_set_name,
            admin_permissions=True,
            template_path=source_output.at_path("template.yaml"),
            run_order=1
        ),
        cpactions.ManualApprovalAction(
            action_name="ApproveChanges",
            run_order=2
        ),
        cpactions.CloudFormationExecuteChangeSetAction(
            action_name="ExecuteChanges",
            stack_name=stack_name,
            change_set_name=change_set_name,
            run_order=3
        )
    ]
}

codepipeline.Pipeline(stack, "Pipeline",
    cross_account_keys=True,
    stages=[source_stage, prod_stage
    ]
)
```

Java:

```java
// Source stage: read from repository
Repository repo = Repository.Builder.create(stack, "TemplateRepo")
        .repositoryName("template-repo")
        .build();
Artifact sourceOutput = new Artifact("SourceArtifact");
CodeCommitSourceAction source = CodeCommitSourceAction.Builder.create()
        .actionName("Source")
        .repository(repo)
        .output(sourceOutput)
        .trigger(CodeCommitTrigger.POLL)
        .build();
Map<String, Object> sourceStage = Map.of(
        "stageName", "Source",
        "actions", List.of(source));

// Deployment stage: create and deploy changeset with manual approval
String stackName = "OurStack";
String changeSetName = "StagedChangeSet";

Map<String, Object> prodStage = Map.of(
        "stageName", "Deploy",
        "actions", List.of(
            CloudFormationCreateReplaceChangeSetAction.Builder.create()
                    .actionName("PrepareChanges")
                    .stackName(stackName)
                    .changeSetName(changeSetName)
                    .adminPermissions(true)
                    .templatePath(sourceOutput.atPath("template.yaml"))
                    .runOrder(1)
                    .build(),
            ManualApprovalAction.Builder.create()
                    .actionName("ApproveChanges")
                    .runOrder(2)
                    .build(),
            CloudFormationExecuteChangeSetAction.Builder.create()
                    .actionName("ExecuteChanges")
                    .stackName(stackName)
                    .changeSetName(changeSetName)
                    .runOrder(3)
                    .build()));

Pipeline.Builder.create(stack, "Pipeline")
        .crossAccountKeys(true)
        .stages(List.of(sourceStage, prodStage))
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
// Source stage: read from repository
var repo = new Repository(stack, "TemplateRepo", new RepositoryProps {
    RepositoryName = "template-repo"
});
var sourceOutput = new Artifact("SourceArtifact");
var source = new CodeCommitSourceAction(new CodeCommitSourceActionProps {
    ActionName = "Source",
    Repository = repo,
    Output = sourceOutput,
    Trigger = CodeCommitTrigger.POLL
});
IDictionary<string, object> sourceStage = new Dictionary<string, object> {
    { "stageName", "Source" },
    { "actions", new [] { source } }
};

// Deployment stage: create and deploy changeset with manual approval
var stackName = "OurStack";
var changeSetName = "StagedChangeSet";

IDictionary<string, object> prodStage = new Dictionary<string, object> {
    { "stageName", "Deploy" },
    { "actions", new [] {
        new CloudFormationCreateReplaceChangeSetAction(new CloudFormationCreateReplaceChangeSetActionProps {
            ActionName = "PrepareChanges",
            StackName = stackName,
            ChangeSetName = changeSetName,
            AdminPermissions = true,
            TemplatePath = sourceOutput.AtPath("template.yaml"),
            RunOrder = 1
        }),
        new ManualApprovalAction(new ManualApprovalActionProps {
            ActionName = "ApproveChanges",
            RunOrder = 2
        }),
        new CloudFormationExecuteChangeSetAction(new CloudFormationExecuteChangeSetActionProps {
            ActionName = "ExecuteChanges",
            StackName = stackName,
            ChangeSetName = changeSetName,
            RunOrder = 3
        }) } }
};

new Pipeline(stack, "Pipeline", new PipelineProps {
    CrossAccountKeys = true,
    Stages = new [] { sourceStage, prodStage }
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
// Source stage: read from repository
repo := codecommit.NewRepository(stack, jsii.String("TemplateRepo"), &RepositoryProps{
	RepositoryName: jsii.String("template-repo"),
})
sourceOutput := codepipeline.NewArtifact(jsii.String("SourceArtifact"))
source := cpactions.NewCodeCommitSourceAction(&CodeCommitSourceActionProps{
	ActionName: jsii.String("Source"),
	Repository: repo,
	Output: sourceOutput,
	Trigger: cpactions.CodeCommitTrigger_POLL,
})
sourceStage := map[string]interface{}{
	"stageName": jsii.String("Source"),
	"actions": []CodeCommitSourceAction{
		source,
	},
}

// Deployment stage: create and deploy changeset with manual approval
stackName := "OurStack"
changeSetName := "StagedChangeSet"

prodStage := map[string]interface{}{
	"stageName": jsii.String("Deploy"),
	"actions": []interface{}{
		cpactions.NewCloudFormationCreateReplaceChangeSetAction(&CloudFormationCreateReplaceChangeSetActionProps{
			"actionName": jsii.String("PrepareChanges"),
			"stackName": jsii.String(stackName),
			"changeSetName": jsii.String(changeSetName),
			"adminPermissions": jsii.Boolean(true),
			"templatePath": sourceOutput.atPath(jsii.String("template.yaml")),
			"runOrder": jsii.Number(1),
		}),
		cpactions.NewManualApprovalAction(&ManualApprovalActionProps{
			"actionName": jsii.String("ApproveChanges"),
			"runOrder": jsii.Number(2),
		}),
		cpactions.NewCloudFormationExecuteChangeSetAction(&CloudFormationExecuteChangeSetActionProps{
			"actionName": jsii.String("ExecuteChanges"),
			"stackName": jsii.String(stackName),
			"changeSetName": jsii.String(changeSetName),
			"runOrder": jsii.Number(3),
		}),
	},
}

codepipeline.NewPipeline(stack, jsii.String("Pipeline"), &PipelineProps{
	CrossAccountKeys: jsii.Boolean(true),
	Stages: []StageProps{
		sourceStage,
		prodStage,
	},
})
```

## Initializer

TypeScript:

```ts
new CodeCommitSourceAction(props: CodeCommitSourceActionProps)
```

Python:

```python
CodeCommitSourceAction(props: CodeCommitSourceActionProps)
```

Java:

```java
new CodeCommitSourceAction(CodeCommitSourceActionProps props)
```

C# / .NET:

```csharp
new CodeCommitSourceAction(CodeCommitSourceActionProps props)
```

Golang:

```go
NewCodeCommitSourceAction(Props CodeCommitSourceActionProps)
```

## Properties

- `actionProperties`: [ActionProperties](/cdk/api/v2/docs/aws-cdk-lib.aws_codepipeline.ActionProperties.html) — The simple properties of the Action, like its Owner, name, etc.

  Note that this accessor will be called before the `bind` callback.

- `variables`: [CodeCommitSourceVariables](/cdk/api/v2/docs/aws-cdk-lib.aws_codepipeline_actions.CodeCommitSourceVariables.html) — The variables emitted by this action.

## Methods

- `bind(scope: Construct, stage: IStage, options: ActionBindOptions): ActionConfig` — The callback invoked when this Action is added to a Pipeline.

- `onStateChange(name: string, target?: IRuleTarget, options?: RuleProps): Rule` — Creates an Event that will be triggered whenever the state of this Action changes.
