Last active
October 13, 2021 18:09
-
-
Save moshest/c6abc2d2af943d2755c5 to your computer and use it in GitHub Desktop.
Node.js Project on AWS CodeDeploy CentOS
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
version: 0.0 | |
os: linux | |
files: | |
- source: / | |
destination: /home/ec2-user/node | |
permissions: | |
- object: /home/ec2-user | |
owner: ec2-user | |
group: ec2-user | |
type: | |
- directory | |
- file | |
hooks: | |
BeforeInstall: | |
- location: install.sh | |
timeout: 300 | |
runas: root | |
AfterInstall: | |
- location: post_install.sh | |
timeout: 600 | |
runas: ec2-user | |
ApplicationStart: | |
- location: run.sh | |
timeout: 120 | |
runas: ec2-user | |
ApplicationStop: | |
- location: stop.sh | |
timeout: 120 | |
runas: ec2-user | |
ValidateService: | |
- location: validate.sh | |
timeout: 60 | |
runas: ec2-user |
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
#!/usr/bin/env bash | |
set -e | |
# update instance | |
yum -y update | |
# install general libraries like Java or ImageMagick | |
yum -y install default-jre ImageMagick | |
# add nodejs to yum | |
curl --silent --location https://rpm.nodesource.com/setup_4.x | bash - | |
yum -y install nodejs #default-jre ImageMagick | |
# install pm2 module globaly | |
npm install -g pm2 | |
pm2 update |
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
#!/usr/bin/env bash | |
set -e | |
cd ~/node | |
npm install | |
# setup NODE_ENV | |
if [ ! -z "$DEPLOYMENT_GROUP_NAME" ]; then | |
export NODE_ENV=$DEPLOYMENT_GROUP_NAME | |
hasEnv=`grep "export NODE_ENV" ~/.bash_profile | cat` | |
if [ -z "$hasEnv" ]; then | |
echo "export NODE_ENV=$DEPLOYMENT_GROUP_NAME" >> ~/.bash_profile | |
else | |
sed -i "/export NODE_ENV=\b/c\export NODE_ENV=$DEPLOYMENT_GROUP_NAME" ~/.bash_profile | |
fi | |
fi | |
# add node to startup | |
hasRc=`grep "su -l $USER" /etc/rc.d/rc.local | cat` | |
if [ -z "$hasRc" ]; then | |
sudo sh -c "echo 'su -l $USER -c \"cd ~/node;sh ./run.sh\"' >> /etc/rc.d/rc.local" | |
fi |
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
#!/usr/bin/env bash | |
if [ ! -z "$DEPLOYMENT_GROUP_NAME" ]; then | |
export NODE_ENV=$DEPLOYMENT_GROUP_NAME | |
fi | |
cd ~/node | |
pm2 start bin/www -n www -i 0 |
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
#!/usr/bin/env bash | |
cd ~/node | |
pm2 stop www || true |
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
#!/usr/bin/env bash | |
sleep 10 | |
nc -zv 127.0.0.1 3000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great example - thanks!