[wp] Push上Bitbucket並透過CodeDeploy部署到EC2 - user_data.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# setup | |
apt update -y | |
apt install -y awscli ruby-full wget | |
# Ref: https://docs.aws.amazon.com/codedeploy/latest/userguide/codedeploy-agent-operations-install-ubuntu.html | |
if ! service codedeploy-agent status > /dev/null; then | |
echo "Installing code-deploy agent" | |
wget https://aws-codedeploy-ap-northeast-1.s3.ap-northeast-1.amazonaws.com/latest/install | |
chmod +x ./install | |
./install auto > /tmp/logfile | |
service codedeploy-agent status | |
fi | |
# Ref: https://gist.github.com/abhilash1in/638d928036c4a89421339f3ec2384129 | |
cat > /tmp/subscript.sh << EOF | |
# START UBUNTU USERSPACE | |
if ! command -v nvm; then | |
echo "Setting up nvm" | |
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash | |
fi | |
if ! command -v nvm; then | |
echo "Reloading /home/ubuntu/.nvm/nvm.sh, /home/ubuntu/.profile, /home/ubuntu/.bashrc" | |
# Dot source the files to ensure that variables are available within the current shell | |
. /home/ubuntu/.nvm/nvm.sh | |
. /home/ubuntu/.profile | |
. /home/ubuntu/.bashrc | |
fi | |
# Install NPM, Node.JS | |
nvm install 14 | |
# Install PM2 | |
if ! command -v pm2; then | |
npm install pm2 -g | |
fi | |
if ! pm2 describe pm2-logrotate > /dev/null; then | |
pm2 install pm2-logrotate | |
fi | |
EOF | |
chown ubuntu:ubuntu /tmp/subscript.sh && chmod a+x /tmp/subscript.sh | |
sleep 1; su - ubuntu -c "/tmp/subscript.sh" | |
function cleanup() | |
{ | |
rm /tmp/subscript.sh | |
} | |
trap cleanup exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment