# CommandsAction

CodePipeline compute action that uses AWS Commands.

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

- Source: [`aws-codepipeline-actions/lib/commands/commands-action.ts`](https://github.com/aws/aws-cdk/blob/v2.278.0/packages/aws-cdk-lib/aws-codepipeline-actions/lib/commands/commands-action.ts#L60)
- Type last changed: v2.179.0 · 18 Feb 2025

## Example

TypeScript:

```ts
declare const sourceArtifact: codepipeline.Artifact;
declare const outputArtifact: codepipeline.Artifact;

const commandsAction = new codepipeline_actions.CommandsAction({
  actionName: 'Commands',
  commands: [
    'export MY_OUTPUT=my-key',
  ],
  input: sourceArtifact,
  output: outputArtifact,
  outputVariables: ['MY_OUTPUT', 'CODEBUILD_BUILD_ID'], // CODEBUILD_BUILD_ID is a variable defined by CodeBuild
});

// Deploy action
const deployAction = new codepipeline_actions.S3DeployAction({
  actionName: 'DeployAction',
  extract: true,
  input: outputArtifact,
  bucket: new s3.Bucket(this, 'DeployBucket'),
  objectKey: commandsAction.variable('MY_OUTPUT'), // the variable emitted by the Commands action
});
```

Python:

```python
# source_artifact: codepipeline.Artifact
# output_artifact: codepipeline.Artifact

commands_action = codepipeline_actions.CommandsAction(
    action_name="Commands",
    commands=["export MY_OUTPUT=my-key"
    ],
    input=source_artifact,
    output=output_artifact,
    output_variables=["MY_OUTPUT", "CODEBUILD_BUILD_ID"]
)

# Deploy action
deploy_action = codepipeline_actions.S3DeployAction(
    action_name="DeployAction",
    extract=True,
    input=output_artifact,
    bucket=s3.Bucket(self, "DeployBucket"),
    object_key=commands_action.variable("MY_OUTPUT")
)
```

Java:

```java
Artifact sourceArtifact;
Artifact outputArtifact;

CommandsAction commandsAction = CommandsAction.Builder.create()
        .actionName("Commands")
        .commands(List.of("export MY_OUTPUT=my-key"))
        .input(sourceArtifact)
        .output(outputArtifact)
        .outputVariables(List.of("MY_OUTPUT", "CODEBUILD_BUILD_ID"))
        .build();

// Deploy action
S3DeployAction deployAction = S3DeployAction.Builder.create()
        .actionName("DeployAction")
        .extract(true)
        .input(outputArtifact)
        .bucket(new Bucket(this, "DeployBucket"))
        .objectKey(commandsAction.variable("MY_OUTPUT"))
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
Artifact sourceArtifact;
Artifact outputArtifact;

var commandsAction = new CommandsAction(new CommandsActionProps {
    ActionName = "Commands",
    Commands = new [] { "export MY_OUTPUT=my-key" },
    Input = sourceArtifact,
    Output = outputArtifact,
    OutputVariables = new [] { "MY_OUTPUT", "CODEBUILD_BUILD_ID" }
});

// Deploy action
var deployAction = new S3DeployAction(new S3DeployActionProps {
    ActionName = "DeployAction",
    Extract = true,
    Input = outputArtifact,
    Bucket = new Bucket(this, "DeployBucket"),
    ObjectKey = commandsAction.Variable("MY_OUTPUT")
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
var sourceArtifact Artifact
var outputArtifact Artifact

commandsAction := codepipeline_actions.NewCommandsAction(&CommandsActionProps{
	ActionName: jsii.String("Commands"),
	Commands: []*string{
		jsii.String("export MY_OUTPUT=my-key"),
	},
	Input: sourceArtifact,
	Output: outputArtifact,
	OutputVariables: []*string{
		jsii.String("MY_OUTPUT"),
		jsii.String("CODEBUILD_BUILD_ID"),
	},
})

// Deploy action
deployAction := codepipeline_actions.NewS3DeployAction(&S3DeployActionProps{
	ActionName: jsii.String("DeployAction"),
	Extract: jsii.Boolean(true),
	Input: outputArtifact,
	Bucket: s3.NewBucket(this, jsii.String("DeployBucket")),
	ObjectKey: commandsAction.Variable(jsii.String("MY_OUTPUT")),
})
```

## Initializer

TypeScript:

```ts
new CommandsAction(props: CommandsActionProps)
```

Python:

```python
CommandsAction(props: CommandsActionProps)
```

Java:

```java
new CommandsAction(CommandsActionProps props)
```

C# / .NET:

```csharp
new CommandsAction(CommandsActionProps props)
```

Golang:

```go
NewCommandsAction(Props CommandsActionProps)
```

## Properties

- `actionProperties`: [ActionProperties](https://docs.aws.amazon.com/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.

## 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.

- `variable(variableName: string): string` — Reference a CodePipeline variable exported in the Commands action.

  **Parameters**

  - `variableName` — the name of the variable exported by `outputVariables`.
