Skip to content

Instantly share code, notes, and snippets.

@artyomliou
Created September 18, 2021 05:29
Show Gist options
  • Save artyomliou/0b3acf1dafd0dfd4bfd00e1284348c5b to your computer and use it in GitHub Desktop.
Save artyomliou/0b3acf1dafd0dfd4bfd00e1284348c5b to your computer and use it in GitHub Desktop.
[wp] Push上Bitbucket並透過CodeDeploy部署到EC2 - user_data.sh
#!/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