

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 Step AWS CodeBuild Functions 管理构建
<a name="connect-codebuild"></a>

您可以将 Step Functi AWS CodeBuild ons 与集成，以启动、停止和管理构建。本页列出了 CodeBuild APIs 可以与 Step Functions 配合使用的支持功能。

要了解如何在 Step Functions 中与 AWS 服务集成，请参阅[集成 服务](integrate-services.md)和[在 Step Functions 中将参数传递给服务 API](connect-parameters.md)。

通过与 Step Functions 的集成， AWS CodeBuild 你可以使用 Step Functions 来触发、停止和管理构建，以及共享生成报告。使用 Step Functions，您可以设计和运行持续的集成管道，以验证应用程序的软件更改。

**优化 CodeBuild 集成的关键功能**  
支持[运行作业 (.sync)](connect-to-resource.md#connect-sync) 集成模式。
调用`StopBuild`或后`StopBuildBatch`，只有在内部完成一些内部工作 CodeBuild 以最终确定一个或多个生成状态后，才能立即删除生成或生成批次。  
如果您在此期间尝试使用 `BatchDeleteBuilds` 或 `DeleteBuildBatch`，则可能无法删除构建或构建批处理。  
`BatchDeleteBuilds` 和 `DeleteBuildBatch` 的优化服务集成包括内部重试，以简化停止后立即删除的用例。

并非所有集成模式都 APIs 支持所有集成模式，如下表所示。


| API | 请求响应 | 运行作业 (.sync) | 
| --- | --- | --- | 
| StartBuild | 支持 | 支持 | 
| StopBuild | 支持 | 不支持 | 
| BatchDeleteBuilds | 支持 | 不支持 | 
| BatchGetReports | 支持 | 不支持 | 
| StartBuildBatch | 支持 | 支持 | 
| StopBuildBatch | 支持 | 不支持 | 
| RetryBuildBatch | 支持 | 支持 | 
| DeleteBuildBatch | 支持 | 不支持 | 

**中的Step Functions参数表示为 PascalCase**  
即使原生服务 API 在 camelCase 中（例如 API 操作）`startSyncExecution`，您也可以在中指定参数 PascalCase，例如:。`StateMachineArn`

## 已优化 CodeBuild APIs
<a name="connect-codebuild-api"></a>
+ [https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StartBuild.html#API_StartBuild_RequestSyntax](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StartBuild.html#API_StartBuild_RequestSyntax)
+ [https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StopBuild.html](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StopBuild.html)
+ [https://docs.aws.amazon.com/codebuild/latest/APIReference/API_BatchDeleteBuilds.html](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_BatchDeleteBuilds.html)
+ [https://docs.aws.amazon.com/codebuild/latest/APIReference/API_BatchGetReports.html](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_BatchGetReports.html)
+ [https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StartBuildBatch.html](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StartBuildBatch.html)
+ [https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StopBuildBatch.html](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_StopBuildBatch.html)
+ [https://docs.aws.amazon.com/codebuild/latest/APIReference/API_RetryBuildBatch.html](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_RetryBuildBatch.html)
+ [https://docs.aws.amazon.com/codebuild/latest/APIReference/API_DeleteBuildBatch.html](https://docs.aws.amazon.com/codebuild/latest/APIReference/API_DeleteBuildBatch.html)

**注意**  
使用时 JSONPath，您可以使用递归下降运算符 (`..`) 为提供参数。`BatchDeleteBuilds`借助返回的数组，可以将 `Arn` 字段从 `StartBuild` 转换为复数 `Ids` 参数，如以下示例所示。  

```
"BatchDeleteBuilds": {
    "Type": "Task",
    "Resource": "arn:aws:states:::codebuild:batchDeleteBuilds",
    "Arguments": {
        "Ids.$": "$.Build..Arn"
    },
    "Next": "MyNextState"
},
```

## 用于呼叫的 IAM 政策 AWS CodeBuild
<a name="codebuild-iam"></a>

以下示例模板展示了如何根据状态机定义中的资源 AWS Step Functions 生成 IAM 策略。有关更多信息，请参阅[Step Functions 如何为集成服务生成 IAM 策略](service-integration-iam-templates.md)和[探索 Step Functions 中的服务集成模式](connect-to-resource.md)。

*资源：*

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Action": [
                "sns:Publish"
            ],
            "Resource": [
                "arn:aws:sns:us-east-1:123456789012:StepFunctionsSample-CodeBuildExecution1111-2222-3333-wJalrXUtnFEMI-SNSTopic-bPxRfiCYEXAMPLEKEY"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "codebuild:StartBuild",
                "codebuild:StopBuild",
                "codebuild:BatchGetBuilds",
                "codebuild:BatchGetReports"
            ],
            "Resource": "*",
            "Effect": "Allow"
        },
        {
            "Action": [
                "events:PutTargets",
                "events:PutRule",
                "events:DescribeRule"
            ],
            "Resource": [
                "arn:aws:events:us-east-1:123456789012:rule/StepFunctionsGetEventForCodeBuildStartBuildRule"
            ],
            "Effect": "Allow"
        }
    ]
}
```

### `StartBuild`
<a name="codebuild-iam-startbuild"></a>

*静态资源*

------
#### [ Run a Job (.sync) ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:StartBuild",
        "codebuild:StopBuild",
        "codebuild:BatchGetBuilds"
      ],
      "Resource": [
        "arn:aws:codebuild:us-east-1:123456789012:project/myProjectName"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "events:PutTargets",
        "events:PutRule",
        "events:DescribeRule"
      ],
      "Resource": [
        "arn:aws:events:us-east-1:123456789012:rule/StepFunctionsGetEventForCodeBuildStartBuildRule"
      ]
    }
  ]
}
```

------
#### [ Request Response ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:StartBuild"
      ],
      "Resource": [
        "arn:aws:codebuild:us-east-1:123456789012:project/myProjectName"
      ]
    }
  ]
}
```

------

*动态资源*

------
#### [ Run a Job (.sync) ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:StartBuild",
        "codebuild:StopBuild",
        "codebuild:BatchGetBuilds"
      ],
      "Resource": [
        "arn:aws:codebuild:us-east-1:*:project/*"
      ]
    },
    {
      "Effect": "Allow",
      "Action": [
        "events:PutTargets",
        "events:PutRule",
        "events:DescribeRule"
      ],
      "Resource": [
        "arn:aws:events:us-east-1:123456789012:rule/StepFunctionsGetEventForCodeBuildStartBuildRule"
      ]
    }
  ]
}
```

------
#### [ Request Response ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:StartBuild"
      ],
      "Resource": [
        "arn:aws:codebuild:us-east-1:*:project/*"
      ]
    }
  ]
}
```

------

### `StopBuild`
<a name="codebuild-iam-stopbuild"></a>

*静态资源*

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:StopBuild"
      ],
      "Resource": [
        "arn:aws:codebuild:us-east-1:123456789012:project/myProjectName"
      ]
    }
  ]
}
```

*动态资源*

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:StopBuild"
      ],
      "Resource": [
        "arn:aws:codebuild:us-east-1:*:project/*"
      ]
    }
  ]
}
```

### `BatchDeleteBuilds`
<a name="codebuild-iam-batchdeletebuilds"></a>

*静态资源*

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:BatchDeleteBuilds"
      ],
      "Resource": [
        "arn:aws:codebuild:us-east-1:123456789012:project/myProjectName"
      ]
    }
  ]
}
```

*动态资源*

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:BatchDeleteBuilds"
      ],
      "Resource": [
        "arn:aws:codebuild:us-east-1:*:project/*"
      ]
    }
  ]
}
```

### `BatchGetReports`
<a name="codebuild-iam-batchgetreports"></a>

*静态资源*

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:BatchGetReports"
      ],
      "Resource": [
        "arn:aws:codebuild:us-east-1:123456789012:report-group/myReportName"
      ]
    }
  ]
}
```

*动态资源*

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "codebuild:BatchGetReports"
      ],
      "Resource": [
        "arn:aws:codebuild:us-east-1:*:report-group/*"
      ]
    }
  ]
}
```

### `StartBuildBatch`
<a name="codebuild-iam-startbuildbatch"></a>

*静态资源*

------
#### [ Run a Job (.sync) ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:StartBuildBatch",
                "codebuild:StopBuildBatch",
                "codebuild:BatchGetBuildBatches"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/myProjectName"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "events:PutTargets",
                "events:PutRule",
                "events:DescribeRule"
            ],
            "Resource": [
                "arn:aws:events:us-east-1:123456789012:rule/StepFunctionsGetEventForCodeBuildStartBuildBatchRule"
            ]
        }
    ]
}
```

------
#### [ Request Response ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:StartBuildBatch"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/myProjectName"
            ]
        }
    ]
}
```

------

*动态资源*

------
#### [ Run a Job (.sync) ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:StartBuildBatch",
                "codebuild:StopBuildBatch",
                "codebuild:BatchGetBuildBatches"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "events:PutTargets",
                "events:PutRule",
                "events:DescribeRule"
            ],
            "Resource": [
                "arn:aws:events:us-east-1:123456789012:rule/StepFunctionsGetEventForCodeBuildStartBuildBatchRule"
            ]
        }
    ]
}
```

------
#### [ Request Response ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:StartBuildBatch"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/*"
            ]
        }
    ]
}
```

------

### `StopBuildBatch`
<a name="codebuild-iam-stopbuildbatch"></a>

*静态资源*

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:StopBuildBatch"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/myProjectName"
            ]
        }
    ]
}
```

*动态资源*

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:StopBuildBatch"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/*"
            ]
        }
    ]
}
```

### `RetryBuildBatch`
<a name="codebuild-iam-retrybuildbatch"></a>

*静态资源*

------
#### [ Run a Job (.sync) ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:RetryBuildBatch",
                "codebuild:StopBuildBatch",
                "codebuild:BatchGetBuildBatches"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/myProjectName"
            ]
        }
    ]
}
```

------
#### [ Request Response ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:RetryBuildBatch"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/myProjectName"
            ]
        }
    ]
}
```

------

*动态资源*

------
#### [ Run a Job (.sync) ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:RetryBuildBatch",
                "codebuild:StopBuildBatch",
                "codebuild:BatchGetBuildBatches"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/*"
            ]
        }
    ]
}
```

------
#### [ Request Response ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:RetryBuildBatch"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/*"
            ]
        }
    ]
}
```

------

### `DeleteBuildBatch`
<a name="codebuild-iam-deletebuildbatch"></a>

*静态资源*

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:DeleteBuildBatch"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/myProjectName"
            ]
        }
    ]
}
```

*动态资源*

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "codebuild:DeleteBuildBatch"
            ],
            "Resource": [
                "arn:aws:codebuild:us-east-1:123456789012:project/*"
            ]
        }
    ]
}
```