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
name: Deploying to EC2 | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository//download the source code repository | |
uses: actions/checkout@v3 | |
- name: Create private key file and change permissions to read-only | |
run: | | |
echo "${{ secrets.AWS_PEM}}" > myPlayList-key-pair.pem | |
chmod 400 myPlayList-key-pair.pem | |
- name: Install dependencies and build | |
# multi-line bash script inside single quotes ' ' can't read indentation for the if block etc. | |
# <<- denotes a HERE Document which can accept a multi-line correctly indented/formatted script | |
run: | | |
ssh -i myPlayList-key-pair.pem -o StrictHostKeyChecking=no ${{vars.AWS_HOST_NAME}}@${{vars.AWS_HOST}} <<-'SSH_SCRIPT' | |
export PATH="$PATH:/home/ubuntu/.nvm/versions/node/v20.11.1/bin" && | |
cd Full-Stack-Project-Assessment/server && | |
git pull && | |
if pm2 list | grep -q fullstackproject; then | |
echo "fullstack project process found" && | |
echo "stopping the process" && | |
pm2 stop fullstackproject && | |
echo "deleting the process" && | |
pm2 delete fullstackproject | |
fi | |
echo "starting the process" && | |
pm2 start server.js --name fullstackproject | |
SSH_SCRIPT |