使用 AWS CLI 的 CodePipeline 示例 - AWS Command Line Interface

本文档仅适用于 AWS CLI 版本 1。有关 AWS CLI 版本 2 的相关文档,请参阅版本 2 用户指南

使用 AWS CLI 的 CodePipeline 示例

以下代码示例演示了如何通过将 AWS Command Line Interface与 CodePipeline 结合使用,来执行操作和实现常见场景。

操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。

每个示例都包含一个指向完整源代码的链接,您可以从中找到有关如何在上下文中设置和运行代码的说明。

主题

操作

以下代码示例演示了如何使用 acknowledge-job

AWS CLI

检索有关指定任务的信息

此示例返回有关指定任务的信息,包括该任务的状态(如果存在)。这仅用于任务工作人员和自定义操作。要确定 nonce 值和作业 ID,请使用 aws codepipeline poll-for-jobs。

命令:

aws codepipeline acknowledge-job --job-id f4f4ff82-2d11-EXAMPLE --nonce 3

输出:

{ "status": "InProgress" }
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 AcknowledgeJob

以下代码示例演示了如何使用 create-custom-action-type

AWS CLI

创建自定义操作

此示例使用包含自定义操作结构的已创建的 JSON 文件(此处名为 MyCustomAction.json)为 AWS CodePipeline 创建自定义操作。有关创建自定义操作的要求(包括文件结构)的更多信息,请参阅《AWS CodePipeline 用户指南》。

aws codepipeline create-custom-action-type --cli-input-json file://MyCustomAction.json

JSON 文件 MyCustomAction.json 的内容:

{ "category": "Build", "provider": "MyJenkinsProviderName", "version": "1", "settings": { "entityUrlTemplate": "https://192.0.2.4/job/{Config:ProjectName}/", "executionUrlTemplate": "https://192.0.2.4/job/{Config:ProjectName}/lastSuccessfulBuild/{ExternalExecutionId}/" }, "configurationProperties": [ { "name": "MyJenkinsExampleBuildProject", "required": true, "key": true, "secret": false, "queryable": false, "description": "The name of the build project must be provided when this action is added to the pipeline.", "type": "String" } ], "inputArtifactDetails": { "maximumCount": 1, "minimumCount": 0 }, "outputArtifactDetails": { "maximumCount": 1, "minimumCount": 0 } }

该命令会返回自定义操作的结构。

以下代码示例演示了如何使用 create-pipeline

AWS CLI

创建管道

此示例使用包含管道结构的已创建的 JSON 文件(此处名为 MySecondPipeline.json)在 AWS CodePipeline 中创建管道。有关创建管道的要求(包括文件结构)的更多信息,请参阅《AWS CodePipeline 用户指南》。

命令:

aws codepipeline create-pipeline --cli-input-json file://MySecondPipeline.json

JSON 文件示例内容:

{ "pipeline": { "roleArn": "arn:aws:iam::111111111111:role/AWS-CodePipeline-Service", "stages": [ { "name": "Source", "actions": [ { "inputArtifacts": [], "name": "Source", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "S3" }, "outputArtifacts": [ { "name": "MyApp" } ], "configuration": { "S3Bucket": "awscodepipeline-demo-bucket", "S3ObjectKey": "aws-codepipeline-s3-aws-codedeploy_linux.zip" }, "runOrder": 1 } ] }, { "name": "Beta", "actions": [ { "inputArtifacts": [ { "name": "MyApp" } ], "name": "CodePipelineDemoFleet", "actionTypeId": { "category": "Deploy", "owner": "AWS", "version": "1", "provider": "CodeDeploy" }, "outputArtifacts": [], "configuration": { "ApplicationName": "CodePipelineDemoApplication", "DeploymentGroupName": "CodePipelineDemoFleet" }, "runOrder": 1 } ] } ], "artifactStore": { "type": "S3", "location": "codepipeline-us-east-1-11EXAMPLE11" }, "name": "MySecondPipeline", "version": 1 } }

输出:

This command returns the structure of the pipeline.
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 CreatePipeline

以下代码示例演示了如何使用 delete-custom-action-type

AWS CLI

删除自定义操作

此示例使用包含要删除的操作的操作类型、提供程序名称和版本号的已创建的 JSON 文件(此处名为 DeleteMyCustomAction.json)删除 AWS CodePipeline 中的自定义操作。使用 list-action-types 命令查看类别、版本和提供程序的正确值。

命令:

aws codepipeline delete-custom-action-type --cli-input-json file://DeleteMyCustomAction.json

JSON 文件示例内容:

{ "category": "Build", "version": "1", "provider": "MyJenkinsProviderName" }

输出:

None.

以下代码示例演示了如何使用 delete-pipeline

AWS CLI

删除管道

此示例从 AWS CodePipeline 中删除了一个名为 MySecondPipeline 的管道。使用 list-pipelines 命令查看与您 AWS 账户关联的管道列表。

命令:

aws codepipeline delete-pipeline --name MySecondPipeline

输出:

None.
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 DeletePipeline

以下代码示例演示了如何使用 delete-webhook

AWS CLI

删除 webhook

以下 delete-webhook 示例删除 GitHub 版本 1 源操作的 webhook。必须先使用 deregister-webhook-with-third-party 命令取消注册 webhook,然后才能删除它。

aws codepipeline delete-webhook \ --name my-webhook

此命令不生成任何输出。

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的删除 GitHub 源的 webhook

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 DeleteWebhook

以下代码示例演示了如何使用 deregister-webhook-with-third-party

AWS CLI

取消注册 webhook

以下 deregister-webhook-with-third-party 示例删除 GitHub 版本 1 源操作的 webhook。必须先取消注册 webhook,然后才能删除它。

aws codepipeline deregister-webhook-with-third-party \ --webhook-name my-webhook

此命令不生成任何输出。

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的删除 GitHub 源的 webhook

以下代码示例演示了如何使用 disable-stage-transition

AWS CLI

禁用向管道中某个阶段的过渡

此示例禁止在 AWS CodePipeline 中过渡到 MyFirstPipeline 管道的 Beta 阶段。

命令:

aws codepipeline disable-stage-transition --pipeline-name MyFirstPipeline --stage-name Beta --transition-type Inbound

输出:

None.

以下代码示例演示了如何使用 enable-stage-transition

AWS CLI

启用向管道中某个阶段的过渡

此示例启用在 AWS CodePipeline 中过渡到 MyFirstPipeline 管道的 Beta 阶段。

命令:

aws codepipeline enable-stage-transition --pipeline-name MyFirstPipeline --stage-name Beta --transition-type Inbound

输出:

None.

以下代码示例演示了如何使用 get-job-details

AWS CLI

获取任务的详细信息

此示例返回有关 ID 为 f4f4ff82-2d11-EXAMPLE 的任务的详细信息。此命令仅用于自定义操作。调用此命令时,如果自定义操作需要,AWS CodePipeline 会返回用于存储管道构件的 Amazon S3 存储桶的临时凭证。此命令还将返回为操作定义的所有密钥值(如果有)。

命令:

aws codepipeline get-job-details --job-id f4f4ff82-2d11-EXAMPLE

输出:

{ "jobDetails": { "accountId": "111111111111", "data": { "actionConfiguration": { "__type": "ActionConfiguration", "configuration": { "ProjectName": "MyJenkinsExampleTestProject" } }, "actionTypeId": { "__type": "ActionTypeId", "category": "Test", "owner": "Custom", "provider": "MyJenkinsProviderName", "version": "1" }, "artifactCredentials": { "__type": "AWSSessionCredentials", "accessKeyId": "AKIAIOSFODNN7EXAMPLE", "secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "sessionToken": "fICCQD6m7oRw0uXOjANBgkqhkiG9w0BAQUFADCBiDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAldBMRAwDgYDVQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZBbWF6b24xFDASBgNVBAsTC0lBTSBDb25zb2xlMRIwEAYDVQQDEwlUZXN0Q2lsYWMxHzAdBgkqhkiG9w0BCQEWEG5vb25lQGFtYXpvbi5jb20wHhcNMTEwNDI1MjA0NTIxWhcNMTIwNDI0MjA0NTIxWjCBiDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAldBMRAwDgYDVQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZBbWF6b24xFDASBgNVBAsTC0lBTSBDb25zb2xlMRIwEAYDVQQDEwlUZXN0Q2lsYWMxHzAdBgkqhkiG9w0BCQEWEG5vb25lQGFtYXpvbi5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMaK0dn+a4GmWIWJ21uUSfwfEvySWtC2XADZ4nB+BLYgVIk60CpiwsZ3G93vUEIO3IyNoH/f0wYK8m9TrDHudUZg3qX4waLG5M43q7Wgc/MbQITxOUSQv7c7ugFFDzQGBzZswY6786m86gpEIbb3OhjZnzcvQAaRHhdlQWIMm2nrAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAtCu4nUhVVxYUntneD9+h8Mg9q6q+auNKyExzyLwaxlAoo7TJHidbtS4J5iNmZgXL0FkbFFBjvSfpJIlJ00zbhNYS5f6GuoEDmFJl0ZxBHjJnyp378OD8uTs7fLvjx79LjSTbNYiytVbZPQUQ5Yaxu2jXnimvw3rrszlaEXAMPLE=" }, "inputArtifacts": [ { "__type": "Artifact", "location": { "s3Location": { "bucketName": "codepipeline-us-east-1-11EXAMPLE11", "objectKey": "MySecondPipeline/MyAppBuild/EXAMPLE" }, "type": "S3" }, "name": "MyAppBuild" } ], "outputArtifacts": [], "pipelineContext": { "__type": "PipelineContext", "action": { "name": "MyJenkinsTest-Action" }, "pipelineName": "MySecondPipeline", "stage": { "name": "Testing" } } }, "id": "f4f4ff82-2d11-EXAMPLE" } }
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 GetJobDetails

以下代码示例演示了如何使用 get-pipeline-state

AWS CLI

获取有关管道状态的信息

此示例返回名为 MyFirstPipeline 管道的最新状态。

命令:

aws codepipeline get-pipeline-state --name MyFirstPipeline

输出:

{ "created": 1446137312.204, "pipelineName": "MyFirstPipeline", "pipelineVersion": 1, "stageStates": [ { "actionStates": [ { "actionName": "Source", "entityUrl": "https://console.aws.amazon.com/s3/home?#", "latestExecution": { "lastStatusChange": 1446137358.328, "status": "Succeeded" } } ], "stageName": "Source" }, { "actionStates": [ { "actionName": "CodePipelineDemoFleet", "entityUrl": "https://console.aws.amazon.com/codedeploy/home?#/applications/CodePipelineDemoApplication/deployment-groups/CodePipelineDemoFleet", "latestExecution": { "externalExecutionId": "d-EXAMPLE", "externalExecutionUrl": "https://console.aws.amazon.com/codedeploy/home?#/deployments/d-EXAMPLE", "lastStatusChange": 1446137493.131, "status": "Succeeded", "summary": "Deployment Succeeded" } } ], "inboundTransitionState": { "enabled": true }, "stageName": "Beta" } ], "updated": 1446137312.204 }
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 GetPipelineState

以下代码示例演示了如何使用 get-pipeline

AWS CLI

查看管道的结构

此示例返回名为 MyFirstPipeline 管道的结构。

命令:

aws codepipeline get-pipeline --name MyFirstPipeline

输出:

{ "pipeline": { "roleArn": "arn:aws:iam::111111111111:role/AWS-CodePipeline-Service", "stages": [ { "name": "Source", "actions": [ { "inputArtifacts": [], "name": "Source", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "S3" }, "outputArtifacts": [ { "name": "MyApp" } ], "configuration": { "S3Bucket": "awscodepipeline-demo-bucket", "S3ObjectKey": "aws-codepipeline-s3-aws-codedeploy_linux.zip" }, "runOrder": 1 } ] }, { "name": "Beta", "actions": [ { "inputArtifacts": [ { "name": "MyApp" } ], "name": "CodePipelineDemoFleet", "actionTypeId": { "category": "Deploy", "owner": "AWS", "version": "1", "provider": "CodeDeploy" }, "outputArtifacts": [], "configuration": { "ApplicationName": "CodePipelineDemoApplication", "DeploymentGroupName": "CodePipelineDemoFleet" }, "runOrder": 1 } ] } ], "artifactStore": { "type": "S3", "location": "codepipeline-us-east-1-11EXAMPLE11" }, "name": "MyFirstPipeline", "version": 1 } }
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 GetPipeline

以下代码示例演示了如何使用 list-action-executions

AWS CLI

列出操作执行

以下 list-action-executions 示例查看管道的操作执行详细信息,例如操作执行 ID、输入构件、输出构件、执行结果和状态。

aws codepipeline list-action-executions \ --pipeline-name myPipeline

输出:

{ "actionExecutionDetails": [ { "pipelineExecutionId": "EXAMPLE0-adfc-488e-bf4c-1111111720d3", "actionExecutionId": "EXAMPLE4-2ee8-4853-bd6a-111111158148", "pipelineVersion": 12, "stageName": "Deploy", "actionName": "Deploy", "startTime": 1598572628.6, "lastUpdateTime": 1598572661.255, "status": "Succeeded", "input": { "actionTypeId": { "category": "Deploy", "owner": "AWS", "provider": "CodeDeploy", "version": "1" }, "configuration": { "ApplicationName": "my-application", "DeploymentGroupName": "my-deployment-group" }, "resolvedConfiguration": { "ApplicationName": "my-application", "DeploymentGroupName": "my-deployment-group" }, "region": "us-east-1", "inputArtifacts": [ { "name": "SourceArtifact", "s3location": { "bucket": "artifact-bucket", "key": "myPipeline/SourceArti/key" } } ], "namespace": "DeployVariables" }, "output": { "outputArtifacts": [], "executionResult": { "externalExecutionId": "d-EXAMPLEE5", "externalExecutionSummary": "Deployment Succeeded", "externalExecutionUrl": "https://myaddress.com" }, "outputVariables": {} } }, { "pipelineExecutionId": "EXAMPLE0-adfc-488e-bf4c-1111111720d3", "actionExecutionId": "EXAMPLE5-abb4-4192-9031-11111113a7b0", "pipelineVersion": 12, "stageName": "Source", "actionName": "Source", "startTime": 1598572624.387, "lastUpdateTime": 1598572628.16, "status": "Succeeded", "input": { "actionTypeId": { "category": "Source", "owner": "AWS", "provider": "CodeCommit", "version": "1" }, "configuration": { "BranchName": "production", "PollForSourceChanges": "false", "RepositoryName": "my-repo" }, "resolvedConfiguration": { "BranchName": "production", "PollForSourceChanges": "false", "RepositoryName": "my-repo" }, "region": "us-east-1", "inputArtifacts": [], "namespace": "SourceVariables" }, "output": { "outputArtifacts": [ { "name": "SourceArtifact", "s3location": { "bucket": "my-bucket", "key": "myPipeline/SourceArti/key" } } ], "executionResult": { "externalExecutionId": "1111111ad99dcd35914c00b7fbea13995EXAMPLE", "externalExecutionSummary": "Edited template.yml", "externalExecutionUrl": "https://myaddress.com" }, "outputVariables": { "AuthorDate": "2020-05-08T17:45:43Z", "BranchName": "production", "CommitId": "EXAMPLEad99dcd35914c00b7fbea139951111111", "CommitMessage": "Edited template.yml", "CommitterDate": "2020-05-08T17:45:43Z", "RepositoryName": "my-repo" } } }, . . . .

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的查看操作执行(CLI)

以下代码示例演示了如何使用 list-action-types

AWS CLI

查看可用的操作类型

单独使用 list-action-types 命令时,会返回您 AWS 账户可用的所有操作的结构。此示例使用 --action-owner-filter 选项仅返回自定义操作。

命令:

aws codepipeline list-action-types --action-owner-filter Custom

输出:

{ "actionTypes": [ { "inputArtifactDetails": { "maximumCount": 5, "minimumCount": 0 }, "actionConfigurationProperties": [ { "secret": false, "required": true, "name": "MyJenkinsExampleBuildProject", "key": true, "queryable": true } ], "outputArtifactDetails": { "maximumCount": 5, "minimumCount": 0 }, "id": { "category": "Build", "owner": "Custom", "version": "1", "provider": "MyJenkinsProviderName" }, "settings": { "entityUrlTemplate": "http://192.0.2.4/job/{Config:ProjectName}", "executionUrlTemplate": "http://192.0.2.4/job/{Config:ProjectName}/{ExternalExecutionId}" } }, { "inputArtifactDetails": { "maximumCount": 5, "minimumCount": 0 }, "actionConfigurationProperties": [ { "secret": false, "required": true, "name": "MyJenkinsExampleTestProject", "key": true, "queryable": true } ], "outputArtifactDetails": { "maximumCount": 5, "minimumCount": 0 }, "id": { "category": "Test", "owner": "Custom", "version": "1", "provider": "MyJenkinsProviderName" }, "settings": { "entityUrlTemplate": "http://192.0.2.4/job/{Config:ProjectName}", "executionUrlTemplate": "http://192.0.2.4/job/{Config:ProjectName}/{ExternalExecutionId}" } } ] }
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ListActionTypes

以下代码示例演示了如何使用 list-pipeline-executions

AWS CLI

查看管道执行历史记录

以下 list-pipeline-executions 示例显示您 AWS 账户中管道的管道执行历史记录。

aws codepipeline list-pipeline-executions \ --pipeline-name MyPipeline

输出:

{ "pipelineExecutionSummaries": [ { "lastUpdateTime": 1496380678.648, "pipelineExecutionId": "7cf7f7cb-3137-539g-j458-d7eu3EXAMPLE", "startTime": 1496380258.243, "status": "Succeeded" }, { "lastUpdateTime": 1496591045.634, "pipelineExecutionId": "3137f7cb-8d494hj4-039j-d84l-d7eu3EXAMPLE", "startTime": 1496590401.222, "status": "Succeeded" }, { "lastUpdateTime": 1496946071.6456, "pipelineExecutionId": "4992f7jf-7cf7-913k-k334-d7eu3EXAMPLE", "startTime": 1496945471.5645, "status": "Succeeded" } ] }

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的查看执行历史记录

以下代码示例演示了如何使用 list-pipelines

AWS CLI

查看管道列表

此示例列出与用户的 AWS 帐户关联的所有 AWS CodePipeline 管道。

命令:

aws codepipeline list-pipelines

输出:

{ "pipelines": [ { "updated": 1439504274.641, "version": 1, "name": "MyFirstPipeline", "created": 1439504274.641 }, { "updated": 1436461837.992, "version": 2, "name": "MySecondPipeline", "created": 1436460801.381 } ] }
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ListPipelines

以下代码示例演示了如何使用 list-tags-for-resource

AWS CLI

列出标签

以下 list-tags-for-resource 示例检索附加到指定管道资源的所有标签的列表。

aws codepipeline list-tags-for-resource \ --resource-arn arn:aws:codepipeline:us-east-1:123456789012:MyPipeline

输出:

{ "tags": { "Project": "ProjectA", "IscontainerBased": "true" } }

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的查看管道的标签(CLI)

以下代码示例演示了如何使用 list-webhooks

AWS CLI

列出 webhook

以下 list-webhooks 示例检索附加到指定管道资源的所有标签的列表。

aws codepipeline list-webhooks \ --endpoint-url "https://codepipeline.eu-central-1.amazonaws.com" \ --region "eu-central-1"

输出:

{ "webhooks": [ { "url": "https://webhooks.domain.com/trigger111111111EXAMPLE11111111111111111": { "authenticationConfiguration": { "SecretToken": "Secret" }, "name": "my-webhook", "authentication": "GITHUB_HMAC", "targetPipeline": "my-Pipeline", "targetAction": "Source", "filters": [ { "jsonPath": "$.ref", "matchEquals": "refs/heads/{Branch}" } ] }, "arn": "arn:aws:codepipeline:eu-central-1:123456789012:webhook:my-webhook" } ] }

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的列出您账户中的 webhook

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 ListWebhooks

以下代码示例演示了如何使用 poll-for-jobs

AWS CLI

查看任何可用任务

此示例返回有关任何任务的信息,供任务工作人员采取行动。此示例使用预定义的 JSON 文件(MyActionTypeInfo.json)提供有关任务工作人员处理任务的操作类型的信息。此命令仅用于自定义操作。调用此命令时,AWS CodePipeline 会返回用于存储管道构件的 Amazon S3 存储桶的临时凭证。此命令还将返回为操作定义的所有密钥值(如果有)。

命令:

aws codepipeline poll-for-jobs --cli-input-json file://MyActionTypeInfo.json

JSON 文件示例内容:

{ "actionTypeId": { "category": "Test", "owner": "Custom", "provider": "MyJenkinsProviderName", "version": "1" }, "maxBatchSize": 5, "queryParam": { "ProjectName": "MyJenkinsTestProject" } }

输出:

{ "jobs": [ { "accountId": "111111111111", "data": { "actionConfiguration": { "__type": "ActionConfiguration", "configuration": { "ProjectName": "MyJenkinsExampleTestProject" } }, "actionTypeId": { "__type": "ActionTypeId", "category": "Test", "owner": "Custom", "provider": "MyJenkinsProviderName", "version": "1" }, "artifactCredentials": { "__type": "AWSSessionCredentials", "accessKeyId": "AKIAIOSFODNN7EXAMPLE", "secretAccessKey": "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", "sessionToken": "fICCQD6m7oRw0uXOjANBgkqhkiG9w0BAQUFADCBiDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAldBMRAwDgYDVQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZBbWF6b24xFDASBgNVBAsTC0lBTSBDb25zb2xlMRIwEAYDVQQDEwlUZXN0Q2lsYWMxHzAdBgkqhkiG9w0BCQEWEG5vb25lQGFtYXpvbi5jb20wHhcNMTEwNDI1MjA0NTIxWhcNMTIwNDI0MjA0NTIxWjCBiDELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAldBMRAwDgYDVQQHEwdTZWF0dGxlMQ8wDQYDVQQKEwZBbWF6b24xFDASBgNVBAsTC0lBTSBDb25zb2xlMRIwEAYDVQQDEwlUZXN0Q2lsYWMxHzAdBgkqhkiG9w0BCQEWEG5vb25lQGFtYXpvbi5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMaK0dn+a4GmWIWJ21uUSfwfEvySWtC2XADZ4nB+BLYgVIk60CpiwsZ3G93vUEIO3IyNoH/f0wYK8m9TrDHudUZg3qX4waLG5M43q7Wgc/MbQITxOUSQv7c7ugFFDzQGBzZswY6786m86gpEIbb3OhjZnzcvQAaRHhdlQWIMm2nrAgMBAAEwDQYJKoZIhvcNAQEFBQADgYEAtCu4nUhVVxYUntneD9+h8Mg9q6q+auNKyExzyLwaxlAoo7TJHidbtS4J5iNmZgXL0FkbFFBjvSfpJIlJ00zbhNYS5f6GuoEDmFJl0ZxBHjJnyp378OD8uTs7fLvjx79LjSTbNYiytVbZPQUQ5Yaxu2jXnimvw3rrszlaEXAMPLE=" }, "inputArtifacts": [ { "__type": "Artifact", "location": { "s3Location": { "bucketName": "codepipeline-us-east-1-11EXAMPLE11", "objectKey": "MySecondPipeline/MyAppBuild/EXAMPLE" }, "type": "S3" }, "name": "MyAppBuild" } ], "outputArtifacts": [], "pipelineContext": { "__type": "PipelineContext", "action": { "name": "MyJenkinsTest-Action" }, "pipelineName": "MySecondPipeline", "stage": { "name": "Testing" } } }, "id": "ef66c259-64f9-EXAMPLE", "nonce": "3" } ] }
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 PollForJobs

以下代码示例演示了如何使用 put-webhook

AWS CLI

创建 webhook

以下 put-webhook 示例创建 GitHub 版本 1 源操作的 webhook。创建 webhook 后,必须使用 register-webhook-with-third-party 命令进行注册。

aws codepipeline put-webhook \ --cli-input-json file://webhook_json.json \ --region "eu-central-1"

webhook_json.json 的内容:

{ "webhook": { "name": "my-webhook", "targetPipeline": "pipeline_name", "targetAction": "source_action_name", "filters": [ { "jsonPath": "$.ref", "matchEquals": "refs/heads/{Branch}" } ], "authentication": "GITHUB_HMAC", "authenticationConfiguration": { "SecretToken": "secret" } } }

输出:

{ "webhook": { "url": "https://webhooks.domain.com/trigger111111111EXAMPLE11111111111111111", "definition": { "authenticationConfiguration": { "SecretToken": "secret" }, "name": "my-webhook", "authentication": "GITHUB_HMAC", "targetPipeline": "pipeline_name", "targetAction": "Source", "filters": [ { "jsonPath": "$.ref", "matchEquals": "refs/heads/{Branch}" } ] }, "arn": "arn:aws:codepipeline:eu-central-1:123456789012:webhook:my-webhook" }, "tags": [ { "key": "Project", "value": "ProjectA" } ] }

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的创建 GitHub 源 webhook

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 PutWebhook

以下代码示例演示了如何使用 retry-stage-execution

AWS CLI

重试失败的操作

以下 retry-stage-execution 示例重试操作失败的阶段。

aws codepipeline retry-stage-execution \ --pipeline-name MyPipeline \ --stage-name Deploy \ --pipeline-execution-id b59babff-5f34-EXAMPLE \ --retry-mode FAILED_ACTIONS

输出:

{ "pipelineExecutionId": "b59babff-5f34-EXAMPLE" }

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的重试失败的操作(CLI)

以下代码示例演示了如何使用 start-pipeline-execution

AWS CLI

通过管道运行最新版本

此示例通过名为“MyFirstPipeline”的管道运行管道源阶段中存在的最新版本。

命令:

aws codepipeline start-pipeline-execution --name MyFirstPipeline

输出:

{ "pipelineExecutionId": "3137f7cb-7cf7-EXAMPLE" }

以下代码示例演示了如何使用 stop-pipeline-execution

AWS CLI

停止管道执行

以下 stop-pipeline-execution 示例默认为等待正在进行的操作完成,然后停止管道。如果执行已处于 Stopping 状态,则无法选择停止并等待。您可以选择停止并放弃已处于 Stopping 状态的执行。

aws codepipeline stop-pipeline-execution \ --pipeline-name MyFirstPipeline \ --pipeline-execution-id d-EXAMPLE \ --reason "Stopping pipeline after the build action is done"

此命令不返回任何输出。

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的停止管道执行(CLI)

以下代码示例演示了如何使用 tag-resource

AWS CLI

标记资源

以下 tag-resource 示例将一组提供的标签与管道关联。使用此命令添加或编辑标签。

aws codepipeline tag-resource \ --resource-arn arn:aws:codepipeline:us-east-1:123456789012:MyPipeline \ --tags key=Project,value=ProjectA key=IscontainerBased,value=true

此命令不生成任何输出。

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的向管道添加标签(CLI)

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 TagResource

以下代码示例演示了如何使用 untag-resource

AWS CLI

移除连接资源的 AWS 标签

以下 untag-resource 示例移除指定资源的标签。

aws codepipeline untag-resource \ --resource-arn arn:aws:codepipeline:us-east-1:123456789012:MyPipeline \ --tag-keys Project IscontainerBased

此命令不生成任何输出。

有关更多信息,请参阅《AWS CodePipeline 用户指南》中的移除管道的标签(CLI)

  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 UntagResource

以下代码示例演示了如何使用 update-pipeline

AWS CLI

更新管道的结构

此示例使用带有 --cli-input-json 参数的 update-pipeline 命令。此示例使用预定义的 JSON 文件(MyFirstPipeline.json)更新管道的结构。AWSCodePipeline 会识别 JSON 文件中包含的管道名称,然后应用管道结构中已修改字段的任何更改来更新管道。

创建预定义 JSON 文件时,请遵循以下指南:

如果使用 get-pipeline 命令检索管道结构,则必须移除 JSON 文件中管道结构的元数据部分(“metadata”:{ } 行以及其中的“created”、“pipelineARN”和“updated”字段)。管道名称无法更改。

命令:

aws codepipeline update-pipeline --cli-input-json file://MyFirstPipeline.json

JSON 文件内容示例:

{ "pipeline": { "roleArn": "arn:aws:iam::111111111111:role/AWS-CodePipeline-Service", "stages": [ { "name": "Source", "actions": [ { "inputArtifacts": [], "name": "Source", "actionTypeId": { "category": "Source", "owner": "AWS", "version": "1", "provider": "S3" }, "outputArtifacts": [ { "name": "MyApp" } ], "configuration": { "S3Bucket": "awscodepipeline-demo-bucket2", "S3ObjectKey": "aws-codepipeline-s3-aws-codedeploy_linux.zip" }, "runOrder": 1 } ] }, { "name": "Beta", "actions": [ { "inputArtifacts": [ { "name": "MyApp" } ], "name": "CodePipelineDemoFleet", "actionTypeId": { "category": "Deploy", "owner": "AWS", "version": "1", "provider": "CodeDeploy" }, "outputArtifacts": [], "configuration": { "ApplicationName": "CodePipelineDemoApplication", "DeploymentGroupName": "CodePipelineDemoFleet" }, "runOrder": 1 } ] } ], "artifactStore": { "type": "S3", "location": "codepipeline-us-east-1-11EXAMPLE11" }, "name": "MyFirstPipeline", "version": 1 } }

输出:

{ "pipeline": { "artifactStore": { "location": "codepipeline-us-east-1-11EXAMPLE11", "type": "S3" }, "name": "MyFirstPipeline", "roleArn": "arn:aws:iam::111111111111:role/AWS-CodePipeline-Service", "stages": [ { "actions": [ { "actionTypeId": { "__type": "ActionTypeId", "category": "Source", "owner": "AWS", "provider": "S3", "version": "1" }, "configuration": { "S3Bucket": "awscodepipeline-demo-bucket2", "S3ObjectKey": "aws-codepipeline-s3-aws-codedeploy_linux.zip" }, "inputArtifacts": [], "name": "Source", "outputArtifacts": [ { "name": "MyApp" } ], "runOrder": 1 } ], "name": "Source" }, { "actions": [ { "actionTypeId": { "__type": "ActionTypeId", "category": "Deploy", "owner": "AWS", "provider": "CodeDeploy", "version": "1" }, "configuration": { "ApplicationName": "CodePipelineDemoApplication", "DeploymentGroupName": "CodePipelineDemoFleet" }, "inputArtifacts": [ { "name": "MyApp" } ], "name": "CodePipelineDemoFleet", "outputArtifacts": [], "runOrder": 1 } ], "name": "Beta" } ], "version": 3 } }
  • 有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 UpdatePipeline