AWS CodePipelineでGitHub バージョン 2を使用している場合にCommitするとパイプラインが自動実行される問題

AWS

2023.6.6

Topics

はじめに

CodePipeline でソースを GitHub にしてたときにパイプラインが自動実行される事象に遭遇しました。
解決方法を探しましたがなかなか出てこなかったので記事にしました。

似た問題として CodeCommit のポーリングがありますが、本質としては同じですが、解決方法が異なるため注意して下さい。

結論

問題の説明が長くなるため先に結論を述べます。

  • CodePipeline で GitHub の Commitをトリガーとしてパイプラインが実行される設定がデフォルトになっている
  • CodePipeline の設定で「DetectChanges」を明示的に false にしないと自動実行される
  • コンソール上からは設定を確認&変更できないためAWS CLIを使用する

問題の説明

事象について説明した後、解決方法について回答します。

環境

  • CodePipeline で GitHub バージョン 2 をソースとしたパイプラインを作成
  • CodePipeline と GitHub の連携は CodeStar 接続を使用
  • このパイプラインは、定期的に CodeBuild でコマンドを実行するためのもので、Commitのタイミングをトリガーとするものではない用途で使用を想定

GitHub 接続 – AWS CodePipeline

問題の確認方法

  1. GitHub で Commit する
  2. CodePipeline の履歴から 1番右の列の「トリガー」で Webhook から実行されていることを確認

Codepipelineの履歴

解決方法

CodePipeline 側でソースの変更を検知するパラメータが有効化になっているため発生した問題です。

以下が該当のパラメータとなっており、明示的に無効化していない限り有効化されております。

DetectChanges
必須: いいえ

設定したリポジトリとブランチで新しいコミットが行われたときに、パイプラインを自動的にスタートするように制御します。未指定の場合、デフォルト値は true となり、フィールドはデフォルトで表示されません。このパラメータの有効な値:
true: CodePipeline 新しいコミットでパイプラインを自動的に開始します。
false: CodePipeline 新しいコミットでパイプラインを開始しません。
CodeStarSourceConnection Bitbucket、 GitHub、 GitHub およびエンタープライズサーバーアクション用 – AWS CodePipeline

前提として、AWS CLI で解決します。
解決方法の簡単な手順としては以下の通りです。

  1. 「get-pipeline」を実行して、「DetectChanges」が false になっていないことを確認する
    1. 「DetectChanges」がない場合は true になってます
  2. 取得したファイルを修正
  3. 「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 で定義できます。
それ以外のプロバイダーやアクションでは定義するとエラーになるためお気をつけ下さい。

[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"
    }
}

取得したファイルを修正

このファイルを二箇所修正します。

  1. 「configuration」に「"DetectChanges": "false"」を追加する
    1. bool 型だと思ったら str 型 なのでダブルクォーテーションで囲って下さい
  2. 「metadata」を削除する
    1. 更新に不要な情報のため削除します

Edit a pipeline in CodePipeline – AWS CodePipeline

[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」を実行します。
問題がなければ変更された内容で設定が出力されます。

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

update-pipeline — AWS CLI 2.11.21 Command Reference

これで、GitHub で Commit をしても自動実行されることはなくなりました。
逆に自動実行したい場合は同じ設定を行い「DetectChanges」を true にするだけです。

参考情報

AWS CodePipeline が 2 回実行される場合のトラブルシューティング | AWS re:Post

Cold-Airflow

2021年新卒入社。インフラエンジニアです。RDBが三度の飯より好きです。 主にデータベースやAWSのサーバレスについて書く予定です。あと寒いのは苦手です。

Recommends

こちらもおすすめ

Special Topics

注目記事はこちら