Skip to content

Merge pull request #49 from codeit-moving/dev #27

Merge pull request #49 from codeit-moving/dev

Merge pull request #49 from codeit-moving/dev #27

Workflow file for this run

name: Deploy to EC2
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.EC2_SSH_PEM }}" > ~/.ssh/ec2.pem
chmod 600 ~/.ssh/ec2.pem
echo -e "Host *\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
- name: Set up environment variables
run: |
ssh -i ~/.ssh/ec2.pem ec2-user@${{ secrets.EC2_ADDRESS }} "bash -s" << 'EOF'
if [ ! -f ~/moving-be/.env ]; then
touch ~/moving-be/.env
fi
echo "${{ secrets.ENV_FILE }}" > ~/moving-be/.env
EOF
- name: Deploy to EC2
run: |
ssh -i ~/.ssh/ec2.pem -T ec2-user@${{ secrets.EC2_ADDRESS }} << 'EOF'
cd ~/moving-be
git fetch origin main
git reset --hard origin/main
npm install
npm run build
if ! pm2 list | grep -q "my-app"; then
pm2 start dist/app.js --name my-app -i max
else
pm2 reload my-app --update-env
fi
EOF