AWS CodePipelineでGitHub バージョン 2を使用している場合にCommitするとパイプラインが自動実行される問題
2023.6.6
はじめに
CodePipeline でソースを GitHub にしてたときにパイプラインが自動実行される事象に遭遇しました。
解決方法を探しましたがなかなか出てこなかったので記事にしました。
似た問題として CodeCommit のポーリングがありますが、本質としては同じですが、解決方法が異なるため注意して下さい。
結論
問題の説明が長くなるため先に結論を述べます。
- CodePipeline で GitHub の Commitをトリガーとしてパイプラインが実行される設定がデフォルトになっている
- CodePipeline の設定で「DetectChanges」を明示的に false にしないと自動実行される
- コンソール上からは設定を確認&変更できないためAWS CLIを使用する
問題の説明
事象について説明した後、解決方法について回答します。
環境
- CodePipeline で GitHub バージョン 2 をソースとしたパイプラインを作成
- CodePipeline と GitHub の連携は CodeStar 接続を使用
- このパイプラインは、定期的に CodeBuild でコマンドを実行するためのもので、Commitのタイミングをトリガーとするものではない用途で使用を想定
問題の確認方法
- GitHub で Commit する
- CodePipeline の履歴から 1番右の列の「トリガー」で Webhook から実行されていることを確認
解決方法
CodePipeline 側でソースの変更を検知するパラメータが有効化になっているため発生した問題です。
以下が該当のパラメータとなっており、明示的に無効化していない限り有効化されております。
DetectChanges
必須: いいえ設定したリポジトリとブランチで新しいコミットが行われたときに、パイプラインを自動的にスタートするように制御します。未指定の場合、デフォルト値は true となり、フィールドはデフォルトで表示されません。このパラメータの有効な値:
true: CodePipeline 新しいコミットでパイプラインを自動的に開始します。
false: CodePipeline 新しいコミットでパイプラインを開始しません。
CodeStarSourceConnection Bitbucket、 GitHub、 GitHub およびエンタープライズサーバーアクション用 – AWS CodePipeline
前提として、AWS CLI で解決します。
解決方法の簡単な手順としては以下の通りです。
- 「get-pipeline」を実行して、「DetectChanges」が false になっていないことを確認する
- 「DetectChanges」がない場合は true になってます
- 取得したファイルを修正
- 「update-pipeline」を実行して、「DetectChanges」を false にする
なお、実行環境は CloudShell です。
「get-pipeline」を実行
現状の確認と「update-pipeline」を実行する準備をします。
「update-pipeline」は JSON ファイルを使用して設定を更新するため現状の設定値を「get-pipeline」を使って保存します。
get-pipeline — AWS CLI 2.11.21 Command Reference
設定を取得すると、「configuration」に「DetectChanges」が無いことがわかります。
この項目はデフォルト値が true なので、項目がない場合は true になってます。
「DetectChanges」は、provider が「CodeStarSourceConnection」に属するaction で定義できます。
それ以外のプロバイダーやアクションでは定義するとエラーになるためお気をつけ下さい。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | [cloudshell-user@ip-10-2-40-230 ~]$ aws codepipeline get-pipeline --name cold-airflow- test -detect-changes-pipeline > pipeline.json [cloudshell-user@ip-10-2-40-230 ~]$ cat pipeline.json { "pipeline" : { "name" : "cold-airflow-test-detect-changes-pipeline" , "roleArn" : "arn:aws:iam::375845453107:role/cold-airflow-test-detect-changes-CodePipeline-Role" , "artifactStore" : { "type" : "S3" , "location" : "cold-airflow-test-detect-changes-375845453107" }, "stages" : [ { "name" : "Source" , "actions" : [ { "name" : "cold-airflow-detect-changes-test" , "actionTypeId" : { "category" : "Source" , "owner" : "AWS" , "provider" : "CodeStarSourceConnection" , "version" : "1" }, "runOrder" : 1, "configuration" : { "ConnectionArn" : "arn:aws:codestar-connections:us-east-1:375845453107:connection/e83a6b33-d16f-49dd-9bf0-698911ea7658" , "FullRepositoryId" : "xxxxxx/xxxxxx" , "BranchName" : "main" }, "outputArtifacts" : [ { "name" : "cold-airflow-detect-changes-test" } ], "inputArtifacts" : [], "namespace" : "cold-airflow-detect-changes-test-variables" } ] }, { "name" : "Build" , "actions" : [ { "name" : "Build" , "actionTypeId" : { "category" : "Build" , "owner" : "AWS" , "provider" : "CodeBuild" , "version" : "1" }, "runOrder" : 1, "configuration" : { "ProjectName" : "cold-airflow-test-detect-changes-build" }, "outputArtifacts" : [ { "name" : "BuildArtifact" } ], "inputArtifacts" : [ { "name" : "cold-airflow-detect-changes-test" } ], "namespace" : "BuildVariables" } ] } ], "version" : 1 }, "metadata" : { "pipelineArn" : "arn:aws:codepipeline:us-east-1:375845453107:cold-airflow-test-detect-changes-pipeline" , "created" : "2023-05-20T08:07:38.659000+00:00" , "updated" : "2023-05-20T08:07:38.659000+00:00" } } |
取得したファイルを修正
このファイルを二箇所修正します。
- 「configuration」に「"DetectChanges": "false"」を追加する
- bool 型だと思ったら str 型 なのでダブルクォーテーションで囲って下さい
- 「metadata」を削除する
- 更新に不要な情報のため削除します
Edit a pipeline in CodePipeline – AWS CodePipeline
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | [cloudshell-user@ip-10-2-40-230 ~]$ cat pipeline.json { "pipeline" : { "name" : "cold-airflow-test-detect-changes-pipeline" , "roleArn" : "arn:aws:iam::375845453107:role/cold-airflow-test-detect-changes-CodePipeline-Role" , "artifactStore" : { "type" : "S3" , "location" : "cold-airflow-test-detect-changes-375845453107" }, "stages" : [ { "name" : "Source" , "actions" : [ { "name" : "cold-airflow-detect-changes-test" , "actionTypeId" : { "category" : "Source" , "owner" : "AWS" , "provider" : "CodeStarSourceConnection" , "version" : "1" }, "runOrder" : 1, "configuration" : { "ConnectionArn" : "arn:aws:codestar-connections:us-east-1:375845453107:connection/e83a6b33-d16f-49dd-9bf0-698911ea7658" , "FullRepositoryId" : "xxxxxx/xxxxxx" , "BranchName" : "main" , "DetectChanges" : "false" }, "outputArtifacts" : [ { "name" : "cold-airflow-detect-changes-test" } ], "inputArtifacts" : [], "namespace" : "cold-airflow-detect-changes-test-variables" } ] }, { "name" : "Build" , "actions" : [ { "name" : "Build" , "actionTypeId" : { "category" : "Build" , "owner" : "AWS" , "provider" : "CodeBuild" , "version" : "1" }, "runOrder" : 1, "configuration" : { "ProjectName" : "cold-airflow-test-detect-changes-build" }, "outputArtifacts" : [ { "name" : "BuildArtifact" } ], "inputArtifacts" : [ { "name" : "cold-airflow-detect-changes-test" } ], "namespace" : "BuildVariables" } ] } ], "version" : 1 } } |
「update-pipeline」を実行
先程作成したファイルを使用して 「update-pipeline」を実行します。
問題がなければ変更された内容で設定が出力されます。
1 | aws codepipeline update-pipeline --cli-input-json file : //pipeline .json |
update-pipeline — AWS CLI 2.11.21 Command Reference
これで、GitHub で Commit をしても自動実行されることはなくなりました。
逆に自動実行したい場合は同じ設定を行い「DetectChanges」を true にするだけです。
参考情報
テックブログ新着情報のほか、AWSやGoogle Cloudに関するお役立ち情報を配信中!
Follow @twitter2021年新卒入社。インフラエンジニアです。RDBが三度の飯より好きです。 主にデータベースやAWSのサーバレスについて書く予定です。あと寒いのは苦手です。
Recommends
こちらもおすすめ
-
AWS CodePipelineでTerraformパイプラインを実装する
2024.3.28
-
AWS CodePipelineのエラーを検知し、Backlog自動起票する手順
2023.12.20
-
AWS CodeシリーズでAnsible自動実行環境を構築する
2024.2.26

Special Topics
注目記事はこちら

データ分析入門
これから始めるBigQuery基礎知識
2024.02.28

AWSの料金が 10 %割引になる!
『AWSの請求代行リセールサービス』
2024.07.16