1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AWS CodePipelineの動的パラメータを試す

Last updated at Posted at 2023-11-06

はじめに

AWS UpdateでCodePipelineの動的パラメータ機能が追加されました。

この機能を利用することでCodePipelineを実行する毎に異なる値を入力することができ、パイプライン内で参照できます。
環境を指定するパラメータ等を組み込むと、パイプラインの再利用が出来そうです。

ためしてみる

DockerイメージをビルドしてECRにPushするパイプラインを作成し、動的パラメータを試してみます。
イメージにつけるタグとECRのリポジトリ名をPipeline実行時に渡してみます。

構築

CodePipelineのコンソールからパイプラインを作成します。
動的パラメータ機能はV2パイプラインでのみ利用できるので、パイプラインタイプはV2を指定します。
image01.png

変数の項目でタグ用のINPUT_IMAGE_TAGとリポジトリ用のINPUT_REPO_NAMEを作成します。
デフォルト値を空白にしておくことでPipeline実行の際に入力を求められるようになります。

image02.png
ソースステージではソースが存在するリポジトリとブランチを指定します。

ビルドステージの環境変数で、タグIMAGE_TAGとリポジトリ名IMAGE_REPO_NAMEの環境変数を作成します。
CodePipelineの動的パラメータを参照する為、Valueは#{variables.INPUT_IMAGE_TAG}#{variables.INPUT_REPO_NAME}とします。

image03.png

この設定を行うことでPipeline実行時に指定する値がビルド環境の環境変数に渡されます。
環境変数を利用して、buildspec内でタグとリポジトリ名を取得します。

利用したbuildspec.yml
version: 0.2

phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...
      - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .
      - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG

Pipelineを実行する

作成したパイプラインを選択し、「変更をリリース」を選択します。
するとパイプライン変数の入力を求められるので、設定したいタグ名と既存のリポジトリ名を入力し、リリースします。
image04.png

ECRに指定したタグ名でPushされることを確認できました。

image.png

なお、AWS CLIから実行する場合は以下のコマンドになります。

 aws codepipeline start-pipeline-execution --name test_pipeline \
    --variables name=INPUT_IMAGE_TAG,value=test_tag name=INPUT_REPO_NAME,value=test-rep

参考

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?