40 func/setup prod #15
Workflow file for this run
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: Deploy to AWS EC2 | |
on: | |
push: | |
branches: | |
- main | |
pull_request: | |
branches: | |
- main | |
env: | |
AWS_PRIVATE_KEY: ${{ secrets.KEYAWS }} | |
SSH_OPTIONS: '-o StrictHostKeyChecking=no -i key.pem ubuntu@98.80.44.121' | |
jobs: | |
deploy: | |
runs-on: ubuntu-24.04 | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
- name: Set permissions for private key | |
run: | | |
echo "${{ env.AWS_PRIVATE_KEY }}" > key.pem | |
chmod 600 key.pem | |
- name: Stop running containers | |
run: | | |
ssh ${{ env.SSH_OPTIONS }} 'if [ -d /home/ubuntu/tupan-back/ ]; then cd /home/ubuntu/tupan-back/ && sudo docker-compose down; fi' | |
- name: Create target directory on AWS instance | |
run: | | |
ssh ${{ env.SSH_OPTIONS }} 'mkdir -p /home/ubuntu/tupan-back/' | |
- name: Create .env file | |
run: | | |
echo "DB_USER=${{ secrets.DB_USERNAME }}" >> .env | |
echo "DB_PASSWORD=${{ secrets.DB_PASSWORD }}" >> .env | |
echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env | |
echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env | |
echo "DB_PORT=${{ secrets.DB_PORT }}" >> .env | |
- name: Transfer code to AWS instance | |
run: | | |
rsync -av --delete --exclude='.git' --exclude='.github' --exclude='.husky' -e "ssh -o StrictHostKeyChecking=no -i key.pem" --rsync-path="sudo rsync" ./ ubuntu@98.80.44.121:/home/ubuntu/tupan-back | |
- name: Run Docker Compose | |
run: | | |
ssh ${{ env.SSH_OPTIONS }} 'cd /home/ubuntu/tupan-back/ && sudo docker-compose up -d' | |
- name: Cleanup SSH key | |
run: | | |
rm -f key.pem |