1
2

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 5 years have passed since last update.

Bitbucket Pilelines/Deployments入門

Posted at

はじめに

Amazon S3とCloudFrontを使用してスタティックなWebコンテンツを配信・運用しているんですが、
AWSのコンソールポチポチするのストレス更新時の運用を楽にしたいんじゃ!」というディレクターやデザイナーの要望に答えるためにBitbucket Pipelinesを使用して自動化してみました。
スタティックなファイルのデプロイなので、PipelinesとしてもDeploymentsとしても入門レベルの内容だと思います。

前提

  • S3+CloudFrontでコンテンツ配信できる環境が整っていること
  • BitBucketのGitリポジトリがあること

手順

1. IAMユーザの作成

Bitbucket Pipelinesがデプロイに使用するIAMユーザを作成します。
IAMにはS3のオブジェクトを更新するのに必要な権限と、CloudFrontのInvalidationリクエストができる権限が必要です。
ポリシーそのものは適宜作成してください。

ユーザを作成したら、アクセスキーの生成をしてキーペアを取得してください。

2. BitBucketの設定

2.1 Pipelinesの有効化

リポジトリの左メニューより 設定 > PIPELINES Setting > Enable Pipelinesを有効にします。

2.2 環境変数の設定

リポジトリの左メニューより 設定 > PIPELINES Environment variablesに進み、以下の環境変数を定義します。

Variable name Value Secured
AWS_ACCESS_KEY_ID 手順1で取得したACCESS_KEY_ID :heavy_check_mark:
AWS_SECRET_ACCESS_KEY 手順1で取得したSECRET_ACCESS_KEY :heavy_check_mark:
CONTENTS_BUCKET デプロイ先のバケット名
DISTRIBUTION_ID CloudFront distributionのID

なんらかの理由でS3のバケットが変更になったり、CloudFrontのDistributionが変わったりした場合でもGitのコミットなしで変更できるようにキーペア以外も環境変数で定義してみました。

2.3 Pipelineの定義

以下のファイルをリポジトリ直下にコミットします。

bitbucket-pipelines.yml
image: python:3.6.4

pipelines:
  branches:
    master:
      - step:
          name: Print Message
          caches:
            - pip
          script:
            - echo "Deploy process to production environment started"
      - step:
          name: Deploy to Production(push to S3 and cache invalidation)
          deployment: production
          trigger: manual
          caches:
            - pip
          script:
            # Install
            - pip install awscli
            # Push to S3
            - aws s3 sync . s3://${CONTENTS_BUCKET}
              --delete
              --exclude '.git/*'
              --exclude '.gitignore'
              --exclude 'README.md'
              --exclude 'bitbucket-pipelines.yml'
            # Requesting cache invalidation
            - aws cloudfront create-invalidation --distribution-id ${DISTRIBUTION_ID} --path '/*'

この内容の場合、リモートリポジトリ(Bitbucket)のmasterブランチにマージされるとPipelineが走り出します。
trigger: manualは最初のstepには記述できないようなので、echoでメッセージを出力するだけのstepを記述し、2つめのstepに処理を記載しています。

これでデプロイはPipelinesの画面からボタンを押すだけでできるようになり、また、Deploymentsの画面からどのコミットがデプロイされているのかを追えるようになるので便利です。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?