DEV Community

GitHub Action with EC2 and SSH

Sometimes we are stuck with a use case which is not very efficient
but are essentials for the pipeline or CI/CD there are so many reasons why this could arise, cost, time & effort, it's not production we can do whatever we want!!

Here's How

you can connect any virtual machine whether it's deployed on AWS, Azure or GCP they all have something in common,
SSH either password or .pem file this method will work just fine.
I know this is not what we as a DevOps Guy prefer but you have to make something out of this situation you're stuck, right !!

For this, I'm gonna use GitHub Action, SSH DEPLOY action and AWS(I mean why not).

STEP I

Setup your authentication for my case ACCESS KEY & SECRET KEY
Add them to GitHub Secret

GitHub Secret
Password is the .pem key for the instances we gonna deploy the application.

NOTICE: After this setup the HOST, USERNAME, and PORT(if you have changed ur default SSH port) in the variable section

Here's the GitHub Action

name: Deployment to server
on:
    workflow_run:
        workflows: ["Docker Image CI"]
        types: 
            - completed
jobs:

  Deploy:
    name: Deploy
    runs-on: ubuntu-latest
    if: github.event.workflow_run.conclusion == 'success'

    steps:
        - uses: easingthemes/ssh-deploy@v5.0.0
          name: Deploy over SSH
          with:
            SSH_PRIVATE_KEY: ${{ secrets.PASSWORD }}
            REMOTE_HOST: ${{ vars.HOST }}
            REMOTE_USER: ${{ vars.USERNAME }}
            SCRIPT_AFTER: |
                ./script
                // PUT THE COMMANDS HERE

Enter fullscreen mode Exit fullscreen mode

And Voila!! here you go I like this GitHub action due to its simplicity and ease of use with private keys.
Deployment Done

Thank you for reading.
Feel free to reach out for any suggestions.

Top comments (0)