Step 1: Install Java (Required for Jenkins)
sudo apt update
sudo apt install -y openjdk-17-jdk
java -version
Step 2: Install Jenkins
curl -fsSL https://pkg.jenkins.io/debian/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo "deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian binary/" | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt update
sudo apt install -y jenkins
sudo systemctl start jenkins
sudo systemctl enable jenkins
Step 3: Access Jenkins Web Interface
Visit: http://<your-ec2-ip>:8080
Get the unlock password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
- Install Suggested Plugins
- Create Admin User
Step 4: Set Up SSH Access from Jenkins to GitHub
sudo su - jenkins
Generate SSH Key:
ssh-keygen -t ed25519 -C "jenkins@ec2"
# Press enter for defaults
cat ~/.ssh/id_ed25519.pub
Add Public Key to GitHub:
- GitHub > Profile > Settings > SSH and GPG keys
- Add a new key → Paste the public key
Trust GitHub Host:
ssh-keyscan github.com >> ~/.ssh/known_hosts
Test SSH:
ssh -T git@github.com
Exit:
exit
Step 5: Create Jenkins Job
- Go to New Item → Pipeline
- Under Source Code Management:
- Choose: Git
- URL:
git@github.com:username/repo.git
(Your Github Repo URL) - Under Build Triggers:
- ✅ Check: GitHub hook trigger for GITScm polling
- Under Pipeline:
- Select the Pipeline script from SCM
- Add Repo URL
- Add credential (Private key SSH)
- Save the job
Step 6: Set Up GitHub Webhook
- Go to your GitHub Repo → Settings → Webhooks → Add Webhook
- Fill in:
- Payload URL:
http://<your-ec2-ip>:8080/github-webhook/
- Content type:
application/json
- Events: Just the push event
- Click Add Webhook
- Payload URL:
Step 7: Test It!
- Push code to your GitHub repo.
- Check Jenkins if the job triggers.
Troubleshooting Errors & Fixes
❌ Error: No ED25519 host key is known for github.com
Fix:
sudo su - jenkins
ssh-keyscan github.com >> ~/.ssh/known_hosts
exit
Webhook Error: 404 Not Found
Cause: Wrong webhook URL.
✅ Fix: Use: http://<your-ec2-ip>:8080/github-webhook/
❌ Webhook Error: 403 Forbidden
or Connection Refused
Cause: Jenkins is not accessible from GitHub.
✅ Fixes: Open port 8080 in EC2 security group.