created: 2020-12-06T13:07:00.000Z

Chalice の generate pipeline を試す (コードはGitHub)

chalice pipeline の動作検証

私用のAWSアカウントで chalice pipeline コマンドを試してみた。 デフォルトだとソースコードの管理は AWS Code Commit になるが、GitHubが使いたいのでそのようにした。

AWS => GitHub へのアクセストークン

GitHubからソースコードを取得できるように、GitHubのアクセストークンを払い出す。権限はreposが読めればよい。

トークンをこんな感じのJSONに格納して、AWS Secrets Manager に置く。

$ cat /tmp/secrets.json
{"OAuthToken": "xxxxxxxx"}
$ aws secretsmanager create-secret --name GithubRepoAccess \
  --description "Token for Github Repo Access" \
  --secret-string file:///tmp/secrets.json

設定ファイルの生成

chalice generate-pipeline でパイプラインをつくるためのcloudformationテンプレートを生成する。

$ chalice generate-pipeline \
    --pipeline-version v2 \
    --source github \
    --buildspec-file buildspec.yml \
    pipeline.json

自分の場合はjsonだとコメントがつけられなくて不便だったのでyamlに変換した。

cat ./pipeline.json | yq -y . | tee ./pipeline.yaml
rm ./pipeline.json

cloudformation deploy

cloudformationのテンプレートを適用する。

aws cloudformation deploy \
    --template-file pipeline.json \
    --capabilities CAPABILITY_IAM \
    --stack-name {{ }} \
    --parameter-overrides \
        GithubOwner={{ }} \
        GithubRepoName={{  }} \

--capabilities オプションは、AWSの仕様で承認していることを示すためにつける必要があるオプション。

CloudFormation の仕様として、AWSアカウントのアクセス権限に影響するリソース(Roleの作成など)を含む可能性があるテンプレートを指定する場合、明示的に --capabilities を使ってテンプレート機能の承認を行う必要があります。

オプション

自動生成されるpipeline.yamlに用意されているオプションは以下の通り

$ cat ./pipeline.yaml | yq -y .Parameters
ApplicationName:
  Default: sandbox
  Type: String
  Description: Enter the name of your application
CodeBuildImage:
  Default: aws/codebuild/amazonlinux2-x86_64-standard:3.0
  Type: String
  Description: Name of codebuild image to use.
GithubOwner:
  Type: String
  Description: The github owner or org name of the repository.
GithubRepoName:
  Type: String
  Description: The name of the github repository.
GithubRepoSecretId:
  Type: String
  Default: GithubRepoAccess
  Description: The name/ID of the SecretsManager secret that contains the personal
    access token for the github repo.
GithubRepoSecretJSONKey:
  Type: String
  Default: OAuthToken
  Description: The name of the JSON key in the SecretsManager secret that contains
    the personal access token for the github repo.

この2つは必須のオプション

GithubOwner

The github owner or org name of the repository.

GithubRepoName

The name of the github repository.

GitHubのURLはこうなってるので

/repos/:owner/:repo/git/trees/:sha

たとえばchaliceの場合はこうなる

  • GithubOwner=aws
  • GithubRepoName=chalice

実行

circleciのように buildspec.yml に定義したスクリプトが実行される。

アレ何? 大事典
[ad] アレ何? 大事典
佐々木 正孝, 篠崎 晃一 (単行本)