Skip to content

Instantly share code, notes, and snippets.

@raviagheda
Last active June 4, 2024 23:53
Show Gist options
  • Save raviagheda/c69ae5e884f4490b1af656dbd80c00dd to your computer and use it in GitHub Desktop.
Save raviagheda/c69ae5e884f4490b1af656dbd80c00dd to your computer and use it in GitHub Desktop.
Github Action with EC2 using SSH

Github Action with EC2 using SSH

Check this out on Dev.to

Configure SSH into aws ec2

Declare these git secrets

  • SSH_PRIVATE_KEY
  • HOST_NAME / IP_ADDRESS
  • USER_NAME
name: Deploy

on:
  push:
    branches: [ dev ]

jobs:
  Deploy:
    name: Deploy to EC2
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v2 
      - name: Build & Deploy
        env:
            PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
            HOSTNAME: ${{secrets.SSH_HOST}}
            USER_NAME: ${{secrets.USER_NAME}}
      
        run: |
          echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
          ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} '

              # Now we have got the access of EC2 and we will start the deploy .
              cd /home/ubuntu/<PROJECT_DIRECTORY> &&
              git checkout dev &&
              git fetch --all &&
              git reset --hard origin/dev &&
              git pull origin dev &&
              sudo npm i &&
              sudo npm run build &&
              sudo pm2 stop ./dist/index.js &&
              sudo pm2 start ./dist/index.js
              '
@krishna2808
Copy link

jobs:

build_and_test:
name: Build and Test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r blog/requirements.txt
- name: Run tests
run: python blog/manage.py test
deploy:
name: Deploy to EC2 on main branch push
# needs: build_and_test
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Deploy to Server 1
run: |
ssh -i ${{ secrets.EC2_SSH_KEY }} ubuntu@${{secrets.HOST_DNS}}
echo "Hello krishna"
env:
ACTIONS_RUNNER_DEBUG: false

   --------------------- error ---------------------------
  i have already pass secrets.EC2_SSH_KEY in github action variable. why same issue again again.
   
   
   Run ssh -i ***

Warning: Identity file -----BEGIN not accessible: No such file or directory.
ssh: Could not resolve hostname rsa: Temporary failure in name resolution
Error: Process completed with exit code 255.

@claudiokerekes
Copy link

claudiokerekes commented Jun 4, 2024

LAST EDIT: FORGET IT, SECRET KEY TYPO

THANKS


Warning: Permanently added '***' (ED25519) to the list of known hosts.
Load key "private_key": error in libcrypto
@: Permission denied (publickey).

any sugestions??

jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: create key
env:
PRIVATE_KEY: ${{ secrets.SSH_KEY }}
run: |
echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
- name: SSH and deploy
env:
HOSTNAME: ${{secrets.HOST}}
USER_NAME: ${{secrets.USER}}
run: |
ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} '

        # Now we have got the access of EC2 and we will start the deploy .
        cd /home/ubuntu/inads-backend &&
        git checkout main &&
        git fetch --all &&
        git pull origin main &&
        docker-compose build &&
        docker-compose up -d
        '

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment