A robust Jenkins pipeline configuration for automated deployment across multiple environments (Development, Staging, Production) with Docker containerization and health monitoring.
- Multi-Environment Support: Automated deployment to dev, staging, and production environments
- Branch-Based Deployment: Different environments triggered by specific branch patterns
- Docker Integration: Containerized application with automated image building and registry push
- Health Monitoring: Container status validation after deployment
- Notification System: Optional Mattermost integration for build notifications
- Security First: All sensitive data managed through Jenkins credentials
- Rollback Support: Deployment history and easy rollback capabilities
- SSH Agent Plugin
- Mattermost Plugin (optional, for notifications)
- Docker Pipeline Plugin
- Git Plugin
- Credentials Plugin
- Docker and Docker Compose installed on target servers
- SSH access configured for deployment user
- Network access to Docker registry
- Proper firewall configuration
Configure the following credentials in Jenkins (Manage Jenkins β Credentials):
Docker Registry Access:
docker-registry-credentials(Username/Password)
Server Access for each environment:
{env}-ssh-key(SSH Private Key){env}-remote-user(Secret Text){env}-server-address(Secret Text){env}-deployment-path(Secret Text)
Where {env} = dev, stage, prod
Optional Notifications:
mattermost-webhook-url(Secret Text)mattermost-channel(Secret Text)
π Detailed setup instructions: See jenkins-config-example.md
When running the pipeline, you can customize:
| Parameter | Description | Default |
|---|---|---|
IMAGE_NAME |
Docker image name | your-app-name |
DOCKERFILE |
Dockerfile path | Dockerfile |
CONTAINER_NAMES |
Containers to monitor | app-1, service-1, worker-1 |
CONTAINER_SERVICES |
Services to restart | app, service, worker |
| Branch Pattern | Environment | Image Tag | Manual Approval |
|---|---|---|---|
main, master |
Production | prod |
Required |
stage, staging |
Staging | stage |
Not required |
dev, develop, development |
Development | latest |
Not required |
| Feature branches | Development | feature-{branch} |
Not required |
- Extracts Git commit details (hash, author, message)
- Used for tracking and notifications
- Determines target environment based on branch
- Sets appropriate configuration variables
- Validates environment setup
- Builds Docker image with environment-specific tag
- Pushes to configured Docker registry
- Cleans up local images for security
- Connects to target server via SSH
- Auto-detects Docker Compose command
- Pulls latest image and recreates services
- Handles production approval workflow
- Validates container status post-deployment
- Fails pipeline if containers are unhealthy
- Provides detailed status reporting
git checkout dev
git add .
git commit -m "Development changes"
git push origin devβ Automatically deploys to development environment
git checkout stage
git add .
git commit -m "Staging release"
git push origin stageβ Automatically deploys to staging environment
git checkout main
git add .
git commit -m "Production release"
git push origin mainβ Requires manual approval before deployment
version: '3.8'
services:
app:
image: ${DOCKER_USER}/your-app:${IMAGE_TAG:-latest}
container_name: app-1
restart: unless-stopped
ports:
- "3000:3000"
environment:
- NODE_ENV=production
service:
image: ${DOCKER_USER}/your-app:${IMAGE_TAG:-latest}
container_name: service-1
restart: unless-stopped
worker:
image: ${DOCKER_USER}/your-app:${IMAGE_TAG:-latest}
container_name: worker-1
restart: unless-stoppedThe pipeline automatically checks container status:
- β Up: Container running normally
- β Restarting: Pipeline fails, requires investigation
- β Exited: Pipeline fails, check logs
- β Not Found: Pipeline fails, verify container names
Automated notifications include:
- Build status (Success/Failed)
- Environment and branch information
- Git commit details
- Direct link to Jenkins build
- Container deployment status
- Credential Management: All secrets stored in Jenkins credentials store
- SSH Key Authentication: No password-based access
- Image Cleanup: Local Docker images removed after push
- Network Security: SSH timeout and connection validation
- Audit Trail: Complete deployment history and logging
Build Fails at Docker Push
- Verify Docker registry credentials
- Check network connectivity to registry
SSH Connection Failed
- Validate SSH key credentials in Jenkins
- Test manual SSH connection to target server
- Check server firewall rules
Container Health Check Failed
- Review container logs:
docker logs container-name - Verify docker-compose.yml configuration
- Check if image exists in registry
Production Approval Timeout
- Look for pending input in Jenkins UI
- Configure input timeout in pipeline if needed
# SSH to problematic server
ssh user@server-address
# Check container status
docker ps -a
# View container logs
docker logs container-name
# Manual service restart
cd /path/to/deployment
docker-compose restart service-name
# Full redeployment
docker-compose down
docker-compose pull
docker-compose up -d --force-recreate- Feature Development: Work on feature branches
- Testing: Merge to
devbranch for development testing - Staging: Merge to
stagebranch for pre-production validation - Production: Merge to
mainbranch for production deployment
- Always test in lower environments first
- Use descriptive commit messages (appear in notifications)
- Monitor deployment notifications in communication channels
- Keep deployment rollback procedures documented
- Rotate SSH keys regularly
- Use least-privilege access on servers
- Scan Docker images for vulnerabilities
- Review Jenkins security settings periodically
- jenkins-config-example.md - Detailed setup instructions
- Docker Compose Best Practices
- Jenkins Security Best Practices
- Fork the repository
- Create a feature branch
- Update documentation as needed
- Test changes in development environment
- Submit pull request with detailed description
This project is licensed under the MIT License - see the LICENSE file for details.
For issues and questions:
- Check the troubleshooting section above
- Review Jenkins build logs for specific errors
- Validate server and container status manually
- Open an issue with detailed error information and environment details