DEV Community

If you're working on a web app and want to automate your deployment from GitHub to your server, this article is for you. I’ll walk you through everything step by step—from creating an EC2 server on AWS to setting up GitHub Actions to deploy your Docker app.
🧱 What You Need
A GitHub repository with your app (Dockerized)
An AWS account
Basic knowledge of the command line
🖥️ Step 1: Launch an EC2 Instance on AWS
Login to AWS Console → Go to EC2
Click
Launch Instance
Choose a free-tier AMI, like Ubuntu 22.04 LTS
Select t2.micro (free-tier)
Create a new key pair (this will give you a .pem file to SSH later)
Allow port 22 (SSH) in security group
Launch the instance
🔑 Step 2: SSH into Your Server
Once your instance is running, SSH into the server from your local:
chmod 400 your-key.pem # Make the key secure
ssh -i your-key.pem ubuntu@<your-ec2-public-ip>
You’re now inside your server 🎉
🐳 Step 3: Install Docker
& Git
Run the following commands on the server:
sudo apt update
sudo apt install docker.io docker-compose git -y
sudo usermod -aG docker $USER
newgrp docker
Make sure Docker works:
docker --version
🗝️ Step 4: Setup SSH Key for GitHub Access
We’ll generate an SSH key to allow this server to pull from your GitHub repo:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Press Enter to save at the default location (/home/ubuntu/.ssh/id_rsa), and also press Enter when asked for a passphrase.
Then:
cat ~/.ssh/id_rsa.pub
Copy the output.
Go to GitHub > Settings > SSH and GPG Keys > New SSH key
Paste the public key there.
Now test:
ssh -T git@github.com
It should say Hi <your username!> You’ve successfully authenticated.
🐙 Step 5: Clone Your Repo
- Visit your repo on github
- Click the code button
- Switch to the SSH tab
- Copy the Git URL.
git clone git@github.com:your-username/your-repo.git
Make sure your app is using Docker (e.g., you have a Dockerfile and docker-compose.yml).
🔐 Step 6: Add Your EC2 Private Key to GitHub Secrets
Go to the GitHub repository of your project:
Click Settings > Secrets and variables > Actions
-
Click New repository secret
- Name:
SSH_PRIVATE_KEY
- Value: Paste the content of your
.pem
file (e.g.,your-key.pem
) Also add:
- Name:
HOST
→ EC2 PUBLIC IPUSERNAME
→ ubuntu
⚙️ Step 7: Add GitHub Actions Workflow
In your project folder, create:
.github/workflows/deploy.yml
Here’s the content:
name: Deploy App
run-name: Deploy - ${{ github.event.head_commit.message }}
on:
push:
branches:
- main
jobs:
deploy:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Deploy via SSH
uses: appleboy/ssh-action@v0.1.10
with:
host: ${{ secrets.HOST }}
username: ${{ secrets.USERNAME }}
key: ${{ secrets.SSH_PRIVATE_KEY }}
port: 22
script: |
cd your-repo-folder
git pull origin main
docker compose up --build -d --force-recreate
docker image prune -f
🚀 Step 8: Push and Watch it Deploy
Every time you push to main
, the workflow will:
SSH into the server
Pull the latest changes
Rebuild and restart your Docker app
You can view logs on GitHub under Actions tab.
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)