この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
ちゃだいん(@chazuke4649)です。
先日のアップデートで、CodePipeline ソースアクションが CodeCommit の git clone をサポートしました!
AWS CodePipeline ソースアクションが、AWS CodeCommit リポジトリのクローンをサポートするようになりました。この改善により、ソースアクションを定義すると、CodePipeline は CodeCommit の git リポジトリのクローンを作成して、コミット履歴とメタデータを取り込むようになります。
AWS CodeCommit の AWS CodePipeline ソースアクションで git クローンのサポートを開始
実際に試してみる
公式ドキュメントにちょうど今回登場の機能を使用するチュートリアルがあったので、実際にやってみたいと思います。
Tutorial: Use full clone with a CodeCommit pipeline source - AWS CodePipeline
ステップ 1: README ファイルの作成
まずは、CodeCommitリポジトリを作成します。
そのリポジトリに README.md を追加します。
ステップ 2: パイプラインの作成とプロジェクトの構築
次に、パイプラインを作成します。
今回は、MyCodeCommitPipeline という名前で作成します。
ソースステージを追加する際に、出力アーティファクト形式にて、今回のアップデートである Full Clone (完全クローン)を選択します。
選択すると以下情報が表示されました。CodeBuildのサービスロールに、リポジトリに対してgit clone
する権限を追加で付与する必要があるようです。
以上で、ソースステージは完了です。続いてビルドステージを作成します。
今回は新たにCodeBuildを作成するため、「プロジェクトの作成」へ進みます。
新たにBuildProjectを作成し、今回は buildspec.yml をプロジェクト内に決め打ちで保存するので、チュートリアルのサンプルを挿入します。
チュートリアルに記載の buildspec.yml は以下の通りです。
version: 0.2
env:
git-credential-helper: yes
phases:
install:
#If you use the Ubuntu standard image 2.0 or later, you must specify runtime-versions.
#If you specify runtime-versions and use an image other than Ubuntu standard image 2.0, the build fails.
runtime-versions:
nodejs: 10
# name: version
#commands:
# - command
# - command
pre_build:
commands:
- ls -lt
- cat README.md
build:
commands:
- git log | head -100
- git status
- ls
- git describe --all
#post_build:
#commands:
# - command
# - command
#artifacts:
#files:
# - location
#name: $(date +%Y-%m-%d)
#discard-paths: yes
#base-directory: location
#cache:
#paths:
# - paths
CodeBuild側のProject作成が完了すると、CodePipeline側に戻ってきて、作成されたBuildProjectが登録されていることを確認できます。
ビルドステージが完了したら、次のデプロイステージはスキップして、パイプラインを完成させます。
ステップ 3: CodeBuildサービスロールのポリシーを更新してリポジトリのクローンを作成する
パイプラインを完成させると、1回目のパイプラインが自動的に走ります。
ただし、この段階では先ほど完全なクローンオプション使用に必要なCodeBuildサービスロールへの権限追加がまだなので、処理は失敗します。
ビルドステージの詳細から対象のCodeBuildのサービスロールに遷移して編集します。
CodeBuildのサービスロールには、CodeCommitリポジトリに対しgit clone
する権限がないので、以下ポリシーを作成しアタッチします。
codecommit-gitpull
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"codecommit:GitPull"
],
"Resource": "*",
"Effect": "Allow"
}
]
}
これで準備は完了です。
ステップ 4: ビルド出力でリポジトリコマンドを表示する
CodePipelineのサービスロールを更新後、先ほどのパイプラインを再試行すると、次は全て成功しました。
CodeBuildのBuild logsをみると、以下の通り buildspec.ymlに記述されたコマンドが実行されていることが確認できます。
- README.mdの内容を出力
- ディレクトリ内のファイルをリスト
- リポジトリのクローンを作成
- ログを表示
- git describe --all を実行
# 以上省略
[Container] 2020/11/21 18:48:42 Entering phase PRE_BUILD
[Container] 2020/11/21 18:48:42 Running command ls -lt
total 4
-rw-r--r-- 1 root root 55 Nov 21 18:48 README.md
[Container] 2020/11/21 18:48:42 Running command cat README.md
This is a CodeCommit repository! I finished a tutorial.
[Container] 2020/11/21 18:48:42 Phase complete: PRE_BUILD State: SUCCEEDED
[Container] 2020/11/21 18:48:42 Phase context status code: Message:
[Container] 2020/11/21 18:48:42 Entering phase BUILD
[Container] 2020/11/21 18:48:42 Running command git log | head -100
commit 9fff0cb30b3dbfae1f6f12dfa805758c65e53652
Author: chadain <MailAddress>
Date: Sat Nov 21 18:47:35 2020 +0000
Add a comment
commit 3803113d2345afdbc2471d0d800dce2908065aec
Author: chadain <MailAddress>
Date: Sat Nov 21 18:07:51 2020 +0000
frist commit
[Container] 2020/11/21 18:48:42 Running command git status
Not currently on any branch.
nothing to commit, working tree clean
[Container] 2020/11/21 18:48:42 Running command ls
README.md
[Container] 2020/11/21 18:48:42 Running command git describe --all
heads/master
[Container] 2020/11/21 18:48:42 Phase complete: BUILD State: SUCCEEDED
[Container] 2020/11/21 18:48:42 Phase context status code: Message:
[Container] 2020/11/21 18:48:42 Entering phase POST_BUILD
チュートリアルは以上となります。
終わりに
いままでCodePipelineでは対応していなかったCodeCommitリポジトリに対するクローンやメタデータの取得などが、今回改善されました。ぜひ一度お試しください。
それではこの辺で。ちゃだいん(@chazuke4649)でした。