LoginSignup
9
5

More than 5 years have passed since last update.

CodePipelineの編集はAWS CLIを使ってJSONでさくっとやる

Posted at

tl;dr

  • GUIだと名前の変更も、要素のコピーなんかもできなくて辛い
  • AWS CLI の aws codepipeline get-pipeline で定義を取得し、それを編集して create-pipeline, update-pipeline で流し込めばOK

AWS CodePipeline

AWS CodePipeline は、CodeBuildやLambdaを実行して、Continuous Delivery、Release Automationができるサービス。各ステップの入出力 (Artifact) は、S3オブジェクトを介す、S3ドリブンな感じなのと、GUIで動いているのが見える感じが個人的には好きなサービスです。
AWS Lambdaのデプロイ自動化のドキュメントでは、CodePipeline / CodeBuild / AWS SAM / CloudFormation を使って実現する方法が書かれています。

だるいこと

上のデプロイメント自動化の手順を読みながら、操作すると、CodePipelineができあがって、コミットトリガでデプロイされる仕組みができるんですが、たとえば、ステージングデプロイ、承認、プロダクションデプロイ というフローをつくりたいと思うと、

  • Deploymentのステージをコピーして必要な修正だけしたいが、そんな仕組みはない
  • Deployment は2つステージができるので、Stage Deployment/Prod Deploymentという名前にしたいが、一度作ったステージやアクションは名前の変更ができない

ということでだるい。

AWS CLIで簡単に編集

現在の定義の取得

aws codepipeline get-pipeline で取ってこれる。また、あとの作業のため --querypipeline という要素の中のみを取得してきます。

aws codepipeline get-pipeline --name <pipeline name> --query pipeline > current.json

こんなのが取れる

{
  "roleArn": "arn:aws:iam::123456789012:role/AWS-CodePipeline-Service",
  "stages": [
    {
      "name": "Source",
      "actions": [
        {
          "inputArtifacts": [],
          "name": "Source",
          "actionTypeId": {
            "category": "Source",
            "owner": "AWS",
            "version": "1",
            "provider": "CodeCommit"
          },
          "outputArtifacts": [
            {
              "name": "MyApp"
            }
          ],
          "configuration": {
            "PollForSourceChanges": "false",
            "BranchName": "<branch>",
            "RepositoryName": "<repo-name>"
          },
          "runOrder": 1
        }
      ]
    },
    ...
  ],
  "artifactStore": {
    "type": "S3",
    "location": "codepipeline-us-west-2-123456789012"
  },
  "name": "<name>",
  "version": 7
}

編集

GUIと同じようにステージの中にアクションが並んでいるので、名前を変更したりコピーしたりする。

新規作成

JSONを編集し、最低限 pipeline.name の要素の値をコピー先の名前とする。
これだけでコピーされる

aws codepipeline create-pipeline --pipeline file://changed.json

更新

同様。バージョン番号がインクリメントされるが、ここは考慮されないので、元のバージョン番号のまま置いておいて大丈夫。

aws codepipeline update-pipeline --pipeline file://new.json

今後

AWS SAMでアプリケーション作ると、CloudFormationで定義されるのに、このパイプラインやCodeBuildの定義は、Consoleで手編集って微妙。このあたりもCloudFormation化するとよいが、書くのが大変。
これを、CLIの戻りからいい感じにCloudFormationのJSON/YAMLの定義に自動で書き換える仕組みが作れそう。

9
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
5