Update main.yml #6
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 EC2 | |
on: | |
push: | |
branches: | |
- main | |
jobs: | |
deploy: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Setup SSH Key | |
env: | |
EC2_PRIVATE_KEY: ${{ secrets.EC2_KEY }} | |
run: | | |
echo "$EC2_PRIVATE_KEY" > ~/ec2-key.pem | |
chmod 600 ~/ec2-key.pem | |
- name: Create SSH directory | |
run: | | |
mkdir -p ~/.ssh | |
- name: Add known host | |
run: | | |
ssh-keyscan ${{ secrets.EC2_HOST }} >> ~/.ssh/known_hosts | |
- name: Connect to EC2 and Run Commands | |
run: | | |
ssh -i ~/ec2-key.pem ${{ secrets.EC2_USER }}@${{ secrets.EC2_HOST }} << 'EOF' | |
echo "Connected to EC2" | |
EOF |