Skip to content

commit

commit #23

Workflow file for this run

name: Deploy Node.js App to EC2
on:
push:
branches:
- Aws_deploy
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Setup Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Create .ssh directory
run: mkdir -p ~/.ssh
- name: Add SSH key to agent
run: |
echo "${{ secrets.SERVER_SSH_KEY }}" > key.pem
chmod 400 key.pem
eval "$(ssh-agent -s)"
ssh-add key.pem
- name: Add server to known hosts
run: ssh-keyscan ${{ secrets.SERVER_HOST }} >> ~/.ssh/known_hosts
- name: Test SSH connection
run: ssh -i key.pem -o StrictHostKeyChecking=no ${{ secrets.SERVER_USERNAME }}@${{ secrets.SERVER_HOST }} "echo 'SSH connection successful'"
- name: Deploy to server
run: |
ssh -i key.pem ${{ secrets.SERVER_USERNAME }}@${{ secrets.SERVER_HOST }} "\
sudo apt-get update -y && \
sudo apt-get upgrade -y && \
sudo apt-get autoremove -y && \
sudo apt-get autoclean && \
sudo apt-get -f install && \
sudo dpkg --configure -a && \
sudo apt-get purge -y nodejs npm && \
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash - && \
sudo apt-get install -y git nodejs || exit 1; \
cd ~ && \
if [ ! -d \"CICD-Github-Actions\" ]; then \
git clone https://github.com/Julen-Smith/CICD-Github-Actions.git || exit 1; \
fi && \
cd CICD-Github-Actions && \
git checkout Aws_deploy || exit 1 && \
npm install || exit 1 && \
sudo npm install -g pm2 || exit 1 && \
pm2 restart all || pm2 start app.js || exit 1"