[AWS][Beanstalk][Shell Script] デプロイスクリプト

AWS Elastic Beanstalk へのデプロイ

殴り書きです。

  • Gitからリポジトリを取得
  • 環境毎に設定を上書き
  • コンパイル
  • EB CLIでデプロイ

シェルスクリプト

sedで環境設定ファイルを書き換えている部分は引数渡しへ変更下さい。

#!/bin/bash
## Example of Beanstalk deploy
##
set -Ceu
PATH=/root/.local/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/opt/aws/bin:/root/bin

# Path
doc_path=/var/www/vhosts/
deploy_path=/var/www/vhosts/xxx
branch=develop

# Beanstalk Environment
prod_env=prod-xxx
stg_env=stg-xxx
dev_env=dev-xxx

# Beanstalk application
prod_app=xxx
stg_app=xxx
dev_app=xxx

# Beanstalk Keyname
prod_key=xxx
stg_key=xxx
dev_key=xxx

# AWS profile
prod_profile=prod
stg_profile=stg
dev_profile=dev

# Domain
prod_domain=xxx.com
stg_domain=xxx.com
dev_domain=xxx.com

# Internal ELB
prod_elb=internal-prod-xxx.com
sgc_elb=internal-stg-xxx.com
dev_elb=internal-dev-xxx.com

# Confirm Git CLI
if ! type 'git' > /dev/null 2>&1; then
  echo 'Git CLI not found.'
  exit 1
fi

cd ${doc_path}

# Confirm Deploy PATH
if [ -e "${deploy_path}" ]; then
  echo 'Remove existing Working copy.'
  rm -rf {woking copy}
fi

git clone https://github.com/xxx.git
cd ${deploy_path}

git checkout ${branch}

cd ${deploy_path}

git status

echo '~~~~~~~~~~~~ Overwrite conf file ~~~~~~~~~~~~'

# 00_application.conf
sed -i -e "s@${prod_elb}@${dev_elb}@g" ./src/main/ebextensions/httpd/conf.d/elasticbeanstalk/00_application.conf

# config.yml
sed -i -e "s@${prod_env}@${dev_env}@g" ./.elasticbeanstalk/config.yml
sed -i -e "s@${stg_env}@${dev_env}@g" ./.elasticbeanstalk/config.yml
sed -i -e "s@application_name: ${prod_app}@application_name: ${dev_app}@g" ./.elasticbeanstalk/config.yml
sed -i -e "s@default_ec2_keyname: ${prod_key}@default_ec2_keyname: ${dev_key}@g" ./.elasticbeanstalk/config.yml
sed -i -e "s@profile: ${prod_profile}@profile: ${dev_profile}@g" ./.elasticbeanstalk/config.yml

# httpd.conf ServerName
sed -i -e "s@${prod_domain}@${dev_domain}@g" ./src/main/ebextensions/httpd/conf/httpd.conf

# 00-basic-setting.config HOSTNAME
sed -i -e "s@${prod_domain}@${dev_domain}@g" ./src/main/ebextensions/00-basic-setting.config

if grep "${prod_elb}" "./src/main/ebextensions/httpd/conf.d/elasticbeanstalk/00_application.conf" >/dev/null; then
  echo 'failed to overwrite in 00_application.conf.'
  exit 1
elif grep "${prod_profile}" "./.elasticbeanstalk/config.yml" >/dev/null; then
  echo 'failed to overwrite in config.yml.'
  exit 1
elif grep "${prod_domain}" "./src/main/ebextensions/httpd/conf/httpd.conf" >/dev/null; then
  echo 'failed to overwrite in httpd.conf.'
  exit 1
else
  echo 'overwrite ok'
fi

echo '~~~~~~~~~~~~ Compile ~~~~~~~~~~~~'
mvn clean package

# Confirm EB CLI
if ! type 'eb' > /dev/null 2>&1; then
  echo 'EB CLI not found...'
  exit 1
fi

echo '~~~~~~~~~~~~ eb status ~~~~~~~~~~~~'
eb status ${dev_env}

echo '~~~~~~~~~~~~ Start deploy ~~~~~~~~~~~~'
eb deploy ${dev_env} --process --timeout 10

sleep 60s

echo '~~~~~~~~~~~~ eb logs ~~~~~~~~~~~~'
eb logs ${dev_env}

echo '~~~~~~~~~~~~ Deployment success ! ~~~~~~~~~~~~'

exit 0

関連記事

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です