【AWS】CodePipeline ~ Boto3編 ~

■ はじめに

https://dk521123.hatenablog.com/entry/2020/01/23/231827

の続き。

https://dk521123.hatenablog.com/entry/2021/12/21/134528

で行ったように  boto3 API を使って
AWS CodePipeline を作成する必要があったので、メモ。

目次

【1】関連する boto3 API
【2】その他・技術事項
 1)CodePipeline のアクションタイプ
【3】サンプル
 例1:Github -> 承認 -> CodeBuildのPipeline作成

【1】関連する boto3 API

* create_pipeline : パイプライン作成

https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/codepipeline.html#CodePipeline.Client.create_pipeline
使用上の注意

* 上記のAPI仕様だと「tags」があるが、実際に指定すると、
 エラー「Unknown parameter in pipeline "tags", must be one of:
  name, roleArn, artifactSotre, artifactStores, stages, version」
 で怒られる (boto3 の Version が悪いから?)

【2】その他・技術事項

1)CodePipeline のアクションタイプ

https://docs.aws.amazon.com/ja_jp/codepipeline/latest/userguide/reference-pipeline-structure.html#actions-valid-providers

[1] Source
[2] Build
[3] Test
[4] Deploy
[5] Approval
[6] Invoke

【3】サンプル

例1:Github -> 承認 -> CodeBuildのPipeline作成

前提条件

* 以下を事前に用意しておくこと
 + codepipeline用のRole(roleArn)
 + S3バケットおよびそのKMSキー(artifactStore)
 + Github および Auth Token
 + Approval用のSNS設定(NotificationArn付近)
 + CodeBuildプロジェクト(以下の関連記事で実行してもいい)

https://dk521123.hatenablog.com/entry/2021/12/21/134528

プログラム

import boto3


codepipeline_client = boto3.client('codepipeline')

# See: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/codepipeline.html#CodePipeline.Client.create_pipeline
response = codepipeline_client.create_pipeline(
  pipeline={
    'name': 'demo-pipeline',
    'roleArn': 'arn:aws:iam::1111111:role:xxxxxxx',
    'artifactStore': {
      'type': 'S3',
      'location': 'your-s3-bucket',
      'encryptionKey': {
        'id': 'kms/sample-kye',
        'type': 'KMS'
      }
    },
    'artifactStores': {
      'string': {
        'type': 'S3',
        'location': 'your-s3-bucket-name',
        'encryptionKey': {
          'id': 'your-kms/your-kms-for-codebuild',
          'type': 'KMS'
        }
      }
    },
    'stages': [
      {
        'name': 'step1',
        'actions': [
          {
            'name': 'step1-source',
            'actionTypeId': {
              'category': 'Source',
              'owner': 'ThirdParty',
              'provider': 'Github',
              'version': '1'
            },
            'configuration': {
              'Repo': 'your-repository-name',
              'Branch': 'release',
              'Owner': 'your-user',
              'OAuthToken': 'xxxxxxxxxxxxxxxxxxxx'
            },
            'outputArtifacts': [
              {
                'name': 'Source'
              }
            ]
          }
        ],
      },
      {
        'name': 'step2',
        'actions': [
          {
            'name': 'step2-Approval',
            'actionTypeId': {
              'category': 'Approval',
              'owner': 'AWS',
              'provider': 'Manual',
              'version': '1'
            },
            'configuration': {
              'NotificationArn': 'arn:aws:sns:us-east-2:1111111:xxxxxxx',
              'ExternalEntityLink': 'http://your-url.com/xxxxx',
              'CustomData': 'Would you check the deployment? If ok, please approval it.'
            }
          }
        ],
      },
      {
        'name': 'step3',
        'actions': [
          {
            'name': 'step3-Build',
            'actionTypeId': {
              'category': 'Build',
              'owner': 'AWS',
              'provider': 'CodeBuild',
              'version': '1'
            },
            'configuration': {
              'ProjectName': 'demo-codebuild-project'
            },
            'inputArtifacts': [
              {
                'name': 'Source'
              },
            ],
            'outputArtifacts': [
              {
                'name': 'Build'
              }
            ]
          }
        ]
      }
    ]
  }
)
print(response)

関連記事

CodePipeline ~ 入門編 ~
https://dk521123.hatenablog.com/entry/2020/01/23/231827
CodeBuild ~ Boto3編 ~
https://dk521123.hatenablog.com/entry/2021/12/21/134528