Update aws-deployment.yml #32
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: AWS Deployment | |
on: | |
push: | |
branches: ["master"] | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
env: | |
EC2_PUBLIC_IP: ${{ secrets.EC2_PUBLIC_IP }} | |
EC2_USERNAME: ${{ secrets.EC2_USERNAME }} | |
EC2_KEY_PAIR: ${{ secrets.EC2_KEY_PAIR }} | |
DEPLOY_PATH: /home/ec2-user/ | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Clean up workspace | |
run: rm -rf ${{ github.workspace }}/* | |
- name: Configure AWS credentials | |
uses: aws-actions/configure-aws-credentials@v1 | |
with: | |
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} | |
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} | |
aws-region: ap-southeast-1 | |
- name: Install Node.js and npm | |
if: runner.os == 'Linux' | |
run: | | |
sudo apt-get update | |
sudo apt-get install -y nodejs npm | |
- name: Install pm2 | |
run: npm install -g pm2 | |
- name: Deploy the application to EC2 | |
run: | | |
# Clone the repository | |
git clone https://github.com/AdleeAfif/portfolio-app.git | |
# Install project dependencies | |
cd portfolio-app | |
npm install | |
# Build the application | |
npm run build | |
# Start the application with pm2 | |
pm2 start npm --name "portfolio-app" -- start | |
# Deploy the application to EC2 | |
echo "${{ secrets.EC2_KEY_PAIR }}" > ec2_key.pem | |
chmod 600 ec2_key.pem | |
scp -i ec2_ssh_key.pem -o StrictHostKeyChecking=no -r $PWD/portfolio-app/. $EC2_USERNAME@$EC2_PUBLIC_IP:$DEPLOY_PATH | |
# Restart the application on EC2 using pm2 | |
ssh -i ec2_key.pem -o StrictHostKeyChecking=no $EC2_USERNAME@$EC2_PUBLIC_IP "cd $DEPLOY_PATH && pm2 restart portfolio-app" | |
# Cleanup | |
rm ec2_key.pem |