Skip to content

Instantly share code, notes, and snippets.

@raviagheda
Last active May 2, 2024 10:06
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • 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
              '
@sylitas
Copy link

sylitas commented Jul 3, 2023

Thanks!!! Love this

@marcodali
Copy link

USER_NAME: ${secrets.USER_NAME} is missing another opening and ending brackets

@raviagheda
Copy link
Author

raviagheda commented Aug 14, 2023

USER_NAME: ${secrets.USER_NAME} is missing another opening and ending brackets

Thanks you @marcodali for highlighting it! :)
I've updated the gist.

@MarinGarcia
Copy link

Nice ! thanks ! have been looking for this simple example for a long time

@raviagheda
Copy link
Author

Nice ! thanks ! have been looking for this simple example for a long time

Thank you for the positive feedback @MarinGarcia ,
I've created a blog on it, should be easy to find this out now.
https://dev.to/raviagheda/github-action-with-ec2-using-ssh-4ej4

@rashgaroth
Copy link

hii! how can you access env var(s) inside the string?

example:

ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} '
echo $FOO
'

@Arisfx
Copy link

Arisfx commented Oct 25, 2023

May i ask how do you secure and whitelist what to reach your ec2's public ipv4? what you allowed on the ec2's security ingress group please?

@giasuddin90
Copy link

Very nice documentation, working fine. Thank you so much

@aristeoibarra
Copy link

Thank you so much!

@jsveron23
Copy link

hii! how can you access env var(s) inside the string?

example:

ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} '
echo $FOO
'

if you want to use it.
it helps

uses: appleboy/ssh-action@v1.0.0

@arif98741
Copy link

Thansk for this awesome contributions 🔥 🔥 🔥 🔥 🔥 🔥

@anandchakru
Copy link

anandchakru commented Dec 26, 2023

The only way for it to work is to open 22 from everywhere in EC2's SG? Closest I could get was this https://stackoverflow.com/a/72494602/234110 but still it sounds hacky

@Brandonxy
Copy link

but how can i allow github actions to connect to ec2 if the ip of the runner needs to be whilelisted?

@jsveron23
Copy link

but how can i allow github actions to connect to ec2 if the ip of the runner needs to be whilelisted?

      - name: Get VPC IP
        id: vpc-ip
        uses: haythem/public-ip@v1.2

      - name: Add IP to AWS Security group
        id: get-sg-rule-id
        run: |
          id=$(aws ec2 authorize-security-group-ingress \
            --group-id $SG \
            --ip-permissions IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges="[{CidrIp=${IP},Description=${DESC}}]" \
            | jq --raw-output '.SecurityGroupRules | map(.SecurityGroupRuleId) | join("")')
          echo "::set-output name=rule_id::$id"
        env:
          IP: ${{ steps.vpc-ip.outputs.ipv4 }}/32
          DESC: 'Github'

@akarsh-nagariya-trj
Copy link

akarsh-nagariya-trj commented Feb 5, 2024

Hi how i can get ${{secrets.USER_NAME}} value after getting the ssh access so i need to access other secrets data like echo ${USER_NAME} but like this its not working

              cd /home/ubuntu/<PROJECT_DIRECTORY> &&
              git checkout dev &&
              git fetch --all &&
              git reset --hard origin/dev &&
              git pull origin dev &&
              sudo npm i &&
              echo ${USER_NAME}
              sudo npm run build &&
              sudo pm2 stop ./dist/index.js &&
              sudo pm2 start ./dist/index.js
              

@joyyjoel
Copy link

joyyjoel commented Feb 9, 2024

ssh -o StrictHostKeyChecking=no -i private_key ${USER_NAME}@${HOSTNAME} '

The above line will give you an error message.
What the error message tells you, you have a closing quotation mark ( ' ). Just remove the ' from the line above and you should be fine:

@bajpangosh
Copy link

Run echo "$PRIVATE_KEY" > private_key && chmod 600 private_key
Warning: Permanently added '***' (ED25519) to the list of known hosts.
@: Permission denied (publickey).

how to handle private repositories?

@kupilikula
Copy link

@joyyjoel That quotation mark is necessary. It's the opening quotation mark on the end of the ssh line. The closing quotation mark is at the very bottom of the workflow file. This sends that string within the quotation marks as a set of commands to be run after you ssh in to the ec2.

@santoshsiva
Copy link

Run echo "$PRIVATE_KEY" > private_key && chmod 600 private_key Warning: Permanently added '' (ED25519) to the list of known hosts. _@*_: Permission denied (publickey).

how to handle private repositories?

I'm also facing the same

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