Update aws-deployment.yml #27
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/portfolio-app | |
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: Save SSH key to file | |
run: | | |
echo "${{ secrets.EC2_KEY_PAIR }}" > ec2_key.pem | |
chmod 400 ec2_key.pem | |
ls -l | |
- 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 }}" | scp -i - -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -r portfolio-app/. ${{ env.EC2_USERNAME }}@${{ env.EC2_PUBLIC_IP }}:${{ env.DEPLOY_PATH }} | |
# Restart the application on EC2 using pm2 | |
echo "${{ secrets.EC2_KEY_PAIR }}" | ssh -i - -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${{ env.EC2_USERNAME }}@${{ env.EC2_PUBLIC_IP }} "cd ${{ env.DEPLOY_PATH }} && pm2 restart portfolio-app" | |