Overview
GitHub webhooks notify Jenkins whenever code is pushed to the repository, automatically triggering the CI/CD pipeline. This enables continuous integration and deployment.Create a Jenkins Pipeline Job
Create New Item
- From the Jenkins dashboard, click New Item
- Enter a name for your pipeline (e.g., “nodejs-cicd-pipeline”)
- Select Pipeline as the project type
- Click OK
Set Up Build Triggers
Under Build Triggers, check:
- GitHub hook trigger for GITScm polling
Configure Pipeline Definition
Under Pipeline, select:
- Definition: Pipeline script from SCM
- SCM: Git
- Repository URL: Your GitHub repository URL
- Branch Specifier:
*/main - Script Path:
Jenkinsfile
The Jenkinsfile should be in the root of your repository.
Configure GitHub Webhook
Navigate to Repository Settings
- Go to your GitHub repository
- Click Settings > Webhooks
- Click Add webhook
Configure Webhook
Enter the following details:
- Payload URL:
http://<your-ec2-public-ip>:8080/github-webhook/ - Content type:
application/json - Which events would you like to trigger this webhook?: Select “Just the push event”
- Active: Check this box
Include the trailing slash in
/github-webhook/Save Webhook
Click Add webhook to save the configuration.GitHub will send a test ping to verify connectivity.
Understanding the Jenkinsfile
The pipeline is defined in aJenkinsfile at the root of the repository:
Pipeline Stages
- Clone: Checks out the code from the GitHub repository (main branch)
- Build: Builds a Docker image using the Dockerfile
- Test: Installs dependencies and runs npm tests
- Deploy: Runs the Docker container on port 80
Environment Variables
IMAGE_NAME: Set tonodejs-demo-appfor consistent image naming
Post Actions
- always: Executes regardless of pipeline result
- success: Executes only when pipeline succeeds
- failure: Executes only when pipeline fails
Test the Pipeline
Monitor Jenkins
- Go to the Jenkins dashboard
- You should see your pipeline job automatically start
- Click on the build number to view real-time logs
View Build Logs
Monitor the console output to see each stage execute:
- Clone stage: Repository checkout
- Build stage: Docker image creation
- Test stage: Dependency installation and tests
- Deploy stage: Container deployment
Troubleshooting
Webhook Not Triggering
- Verify the webhook URL includes the trailing slash
- Check that port 8080 is accessible from the internet
- Review the webhook delivery history in GitHub settings
Docker Permission Errors
- Ensure the Jenkins user is in the docker group (see Docker Configuration)
- Restart Jenkins after adding to the docker group
Build Failures
- Check the console output for specific error messages
- Verify the Jenkinsfile syntax
- Ensure Docker is installed and running
Every code push to the main branch will automatically trigger the pipeline. Monitor your builds regularly to catch issues early.
Next Steps
Your Jenkins CI/CD pipeline is now fully configured! Consider:- Adding more comprehensive tests
- Implementing deployment strategies (blue-green, canary)
- Adding notifications (Slack, email) for build status
- Implementing proper secrets management for credentials