Why does my GitHub repository fail to deploy to EC2 via CI/CD?
Deploying a GitHub repository to an EC2 instance via CI/CD can sometimes fail for a variety of reasons. In this post, I’ll walk through common pitfalls and how to fix them.
1️⃣ Initial Setup
Goal: Deploy the
DevOpsrepo from GitHub to an EC2 instance automatically via GitHub Actions when a tag is pushed.EC2: t3.micro, public IP
18.209.17.223, Security Group allowed SSH only from your IP.Workflow:
.github/workflows/deploy.ymlusingappleboy/ssh-action@v1.0.3.Private key:
DevOps.pem(AWS key pair).
2️⃣ Workflow YAML issues
Problem
My initial YAML looked like this:
script: cd /usr/share/nginx/html/DevOps
git pull origin main
sudo systemctl reload nginx
Only
cdwas being sent to SSHgit pullandsystemctlwere outside scriptResult: nothing executed, confusing logs.
Fix
Correct YAML formatting:
script: |
set -e
cd /usr/share/nginx/html/DevOps
git fetch origin
git checkout main
git pull origin main
sudo systemctl reload nginx
Multiline | ensures all commands run in one SSH session.
Added
set -eto stop on error.
3️⃣ Commit / Tag confusion
Problem
git tag v1.0.0
git push origin v1.0.0
fatal: tag 'v1.0.0' already exists
I tried to push a tag that already existed.
GitHub Actions triggers deploy only on tag push.
Fix
- Use new tag:
git tag v1.0.1
git push origin v1.0.1
- Workflow triggered properly.
4️⃣ SSH Key Issues
You had
~/.ssh/DevOps.pem(private key) but were unsure how to configure GitHub Secrets.-
Ensured the key was:
- Added as GitHub Secret
EC2_SSH_KEY - Permissions on local machine:
chmod 600 DevOps.pem
- Added as GitHub Secret
Verified key works locally:
ssh -i ~/.ssh/DevOps.pem ec2-user@18.209.17.223
5️⃣ First real error on deployment
GitHub Actions log:
dial tcp ***:22: i/o timeout
This is not YAML or SSH key error.
Cause: GitHub Actions runner could not reach EC2 on port 22.
EC2 Security Group only allowed your IP, blocking all GitHub runners.
6️⃣ Security Group / Networking Fix
- Observed EC2 security group:
Inbound rules:
SSH (22) → 106.51.87.228/32
HTTP (80) → 0.0.0.0/0
- Only my IP was allowed → GitHub Actions cannot SSH → timeout.
Fix
Edit Security Group
sg-0b901d4d95ca4555f→ Inbound rulesAdd:
| Type | Protocol | Port | Source |
|---|---|---|---|
| SSH | TCP | 22 | 0.0.0.0/0 |
Verified EC2 still accessible locally
After this, GitHub Actions runners can reach port 22.
7️⃣ Successful Deployment
- Push new tag:
git tag v1.0.3
git push origin v1.0.3
GitHub Actions triggered
Deploy via SSHScript executed successfully:
cd /usr/share/nginx/html/DevOps
git fetch origin
git checkout main
git pull origin main
sudo systemctl reload nginx
Nginx reloaded
Deployment complete ✅
8️⃣ Lessons Learned / Best Practices
- YAML formatting matters
- Multi-line scripts must use
|underscript.
- Private key setup
Add
.pemas GitHub SecretLocal key permissions:
chmod 600Public key on EC2
- Security Groups
GitHub runners have dynamic IPs → port 22 must be open to
0.0.0.0/0for now.Can be secured later using bastion or AWS SSM.
- Tag-based deploys
Workflow triggers on tag push, not commit
Avoid reusing tags
- i/o timeout ≠ SSH key issue
- Always check networking / firewall first
- Idempotent deploys
Commands use
git fetch+checkout+pullset -eprevents partial deployment
Top comments (0)