Skip to content

Instantly share code, notes, and snippets.

@raviagheda
Last active September 14, 2025 01:02
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.

Revisions

  1. raviagheda revised this gist May 2, 2024. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions github-action-ssh.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    # Github Action with EC2 using SSH
    Check this out on [Dev.to](https://dev.to/raviagheda/github-action-with-ec2-using-ssh-4ej4)

    ## Configure SSH into aws ec2

  2. raviagheda revised this gist Aug 14, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion github-action-ssh.md
    Original file line number Diff line number Diff line change
    @@ -28,7 +28,7 @@ jobs:
    env:
    PRIVATE_KEY: ${{ secrets.SSH_PRIVATE_KEY }}
    HOSTNAME: ${{secrets.SSH_HOST}}
    USER_NAME: ${secrets.USER_NAME}
    USER_NAME: ${{secrets.USER_NAME}}

    run: |
    echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
  3. raviagheda created this gist Dec 18, 2021.
    48 changes: 48 additions & 0 deletions github-action-ssh.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    # Github Action with EC2 using SSH

    ## Configure SSH into aws ec2

    - How to handle SSH keys with ec2-github actions https://zellwk.com/blog/github-actions-deploy/

    ### Declare these git secrets
    - SSH_PRIVATE_KEY
    - HOST_NAME / IP_ADDRESS
    - USER_NAME


    ```yml
    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
    '
    ```