Amazon ECS にデプロイする

このオプションを使用すると、ECS へのデプロイを簡略化できます。このアプローチでは、ユーザーに代わってパイプラインが維持されるため、メンテナンスが少なくて済みます。ただし、このオプションでは、デプロイで制御可能な範囲が制限されます。

前提条件

以下が必要です。

  • An existing image registry such as Docker Hub or ECR. Alternatively, you can also use your own Docker registry.

  • An existing AWS Elastic Container Service cluster running a service, which will be updated with the task definition in the repo.

  • RegisterTaskDefinition および UpdateService アクションを実行するための権限と、プログラミング経由でアクセスする権限を持つ、AWS IAM ユーザー。

手順

1. Clone AWS ECS deployment example repository.

2. Configure your registry variables by going to Repository settings > Pipelines > Repository variables, and selecting Add.

これらの変数は、デプロイ環境、リポジトリ、またはワークスペース内で定義できます。

3. AWS 認証情報を追加します。

結果: これらの変数を bitbucket-pipelines.yml 内から参照できるようになりました。

4. bitbucket-pipelines.yml に移動し、クラスター名とサービス名を編集して、既存の ECS クラスターとサービスに一致させます。

例:

- pipe: atlassian/aws-ecs-deploy:1.0.0 variables: AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION CLUSTER_NAME: 'aws-ecs-deploy-example' SERVICE_NAME: 'aws-ecs-deploy-example-service' TASK_DEFINITION: 'taskDefinition.json'

結果: 変更がプッシュされると、Pipelines はアプリをビルドして Docker コンテナにパッケージ化し、Docker Hub にプッシュ後、コンテナを ECS にデプロイします。

AWS CLI を使用して ECS にデプロイする

このオプションは、カスタマイズをより細かく制御する必要がある高度なシナリオにおすすめします。

前提条件

以下が必要です。

  • An existing image registry such as Docker Hub or ECR. Alternatively, you can also use your own Docker registry.

  • An existing AWS Elastic Container Service cluster running a service, which will be updated with the task definition in the repo.

  • プログラムによるアクセス権を持ち、RegisterTaskDefinitionUpdateService のアクションを実行するための十分な権限を持つ AWS IAM ユーザー。

  • Install the deploy tools. The deployment script requires the AWS CLI, and the jq library as part of your build. If you use a different Docker image or you've created your own, ensure these packages are installed.

手順

  1. Install the AWS CLI.

例:

次の例では、インストールされたツールのイメージを確認できます。

- apt-get update && apt-get install -y jq - curl "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip" -o "awscli-bundle.zip" - unzip awscli-bundle.zip - ./awscli-bundle/install -b ~/bin/aws - export PATH=~/bin:$PATH


2. 新しくプッシュされた Docker イメージを参照する ECS タスク定義を登録します。ECS クラスターでタスク定義を登録すると、バージョンが返されます。これを環境変数に保存することで、後で ECS サービスを更新する際に参照できます。

例:

# Replace the container name in the task definition with the new image. - export IMAGE_NAME="${DOCKERHUB_USERNAME}/${BITBUCKET_REPO_SLUG}:${BITBUCKET_BUILD_NUMBER}" - envsubst < task-definition.json > task-definition-envsubst.json # Update the task definition and capture the latest revision. - > export UPDATED_TASK_DEFINITION=$(aws ecs register-task-definition --cli-input-json file://task-definition-envsubst.json | jq '.taskDefinition.taskDefinitionArn' --raw-output)

3. ECS サービスを更新します。

例:

- aws ecs update-service --service example-ecs-service --cluster example-ecs-cluster --task-definition ${UPDATED_TASK_DEFINITION}

さらにヘルプが必要ですか?

アトラシアン コミュニティをご利用ください。