Step-by-step guide to implement your DevSecOps pipeline from scratch
This guide is organized into 5 phases, each building on the previous one:
- Phase 1: Local Security Setup (30 minutes)
- Phase 2: Security Tools Installation (45 minutes)
- Phase 3: Jenkins DevSecOps Setup (60 minutes)
- Phase 4: Pipeline Integration & Testing (45 minutes)
- Phase 5: Production Deployment (30 minutes)
First, let's make sure all DevSecOps files are in place:
# Check if all security files exist
ls -la .gitleaks.toml # ✅ Should exist
ls -la .secrets.baseline # ✅ Should exist
ls -la .pre-commit-config.yaml # ✅ Should exist
ls -la .trivyignore # ✅ Should exist
ls -la security/trivy-config.yaml # ✅ Should exist
ls -la security/security-policies.yaml # ✅ Should exist
ls -la scripts/security-scan.sh # ✅ Should exist
ls -la scripts/security-scan.ps1 # ✅ Should exist
ls -la Jenkinsfile.devsecops # ✅ Should exist
ls -la SECURITY.md # ✅ Should existExpected Output: All files should exist. If any are missing, they're already in your repository.
# Make security scripts executable
chmod +x scripts/security-scan.sh
chmod +x scripts/security-scan.ps1
# Verify permissions
ls -la scripts/Expected Output: Scripts should have execute permissions (x flag).
# Create directory for security reports
mkdir -p security-reports
# Add to .gitignore to avoid committing reports
echo "security-reports/" >> .gitignore
echo ".trivy/" >> .gitignoreCheckpoint 1: ✅ Repository structure verified, scripts executable, reports directory created.
For Linux/macOS:
# Update package manager
sudo apt update # Ubuntu/Debian
# or
brew update # macOS
# Install required packages
sudo apt install -y curl wget python3 python3-pip jq git # Ubuntu/Debian
# or
brew install curl wget python3 jq git # macOSFor Windows:
# Install Chocolatey (if not installed)
Set-ExecutionPolicy Bypass -Scope Process -Force
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072
iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
# Install prerequisites
choco install git python3 jq -yOption A: Use Our Installation Script (Recommended)
# Linux/macOS - Run our comprehensive installation script
./scripts/security-scan.sh
# Windows PowerShell
.\scripts\security-scan.ps1Option B: Manual Installation (if script fails)
# Install Gitleaks
curl -sSfL https://github.com/zricethezav/gitleaks/releases/download/v8.18.0/gitleaks_8.18.0_linux_x64.tar.gz | tar -xz
sudo mv gitleaks /usr/local/bin/
# Install Trivy
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin v0.46.0
# Install Hadolint
wget -O hadolint https://github.com/hadolint/hadolint/releases/download/v2.12.0/hadolint-Linux-x86_64
chmod +x hadolint && sudo mv hadolint /usr/local/bin/
# Install Python security tools
pip3 install detect-secrets semgrep checkov bandit safety
# Install kube-score
wget -O kube-score.tar.gz https://github.com/zegl/kube-score/releases/download/v1.16.1/kube-score_1.16.1_linux_amd64.tar.gz
tar -xzf kube-score.tar.gz && sudo mv kube-score /usr/local/bin/
rm kube-score.tar.gz# Verify all security tools are installed
echo "🔍 Verifying security tools installation..."
gitleaks version # Should show: v8.18.0
trivy version # Should show: Version: 0.46.0
hadolint --version # Should show: Haskell Dockerfile Linter 2.12.0
semgrep --version # Should show: 1.x.x
checkov --version # Should show: 2.4.9
detect-secrets --version # Should show: 1.4.0
bandit --version # Should show: 1.x.x
safety --version # Should show: 2.x.x
echo "✅ All tools installed successfully!"Expected Output: All commands should return version numbers without errors.
Checkpoint 2: ✅ All security tools installed and verified.
# Initialize detect-secrets baseline (if needed)
detect-secrets scan --baseline .secrets.baseline
# Verify baseline exists and is valid
cat .secrets.baseline | jq '.version'Expected Output: Should show version "1.4.0" and no errors.
# Test secret detection
echo "🔐 Testing secret detection..."
gitleaks detect --source . --report-format json --report-path security-reports/test-gitleaks.json --exit-code 0
echo "✅ Gitleaks test completed"
# Test SAST scanning
echo "📋 Testing SAST scanning..."
semgrep --config=auto --json --output=security-reports/test-semgrep.json . || echo "⚠️ SAST issues found (normal)"
echo "✅ Semgrep test completed"
# Test Dockerfile linting
echo "🐳 Testing Dockerfile linting..."
hadolint Dockerfile --format json > security-reports/test-hadolint.json || echo "⚠️ Dockerfile issues found (normal)"
echo "✅ Hadolint test completed"
# Test IaC security
echo "🛡️ Testing IaC security..."
checkov -d k8s/ --framework kubernetes --output json --output-file security-reports/test-checkov.json || echo "⚠️ IaC issues found (normal)"
echo "✅ Checkov test completed"# Run complete security scan
echo "🚀 Running complete security scan..."
# Linux/macOS
./scripts/security-scan.sh
# Windows PowerShell
# .\scripts\security-scan.ps1
echo "📊 Check security reports:"
ls -la security-reports/Expected Output:
- Script should complete without critical errors
- Multiple JSON report files should be generated
- Summary report should be created
Checkpoint 3: ✅ Local security scanning working, reports generated.
# Install pre-commit
pip3 install pre-commit
# Verify installation
pre-commit --version# Install pre-commit hooks
pre-commit install
# Install commit-msg hook (optional)
pre-commit install --hook-type commit-msg
# Test hooks on all files (first run will be slow)
echo "🔍 Testing pre-commit hooks (this may take a few minutes)..."
pre-commit run --all-files
echo "✅ Pre-commit hooks installed and tested"# Create a test file
echo "# Test file" > test-security.md
# Add and try to commit (should trigger hooks)
git add test-security.md
git commit -m "Test pre-commit hooks"
# Clean up
rm test-security.md
git reset --soft HEAD~1Expected Output: Pre-commit hooks should run and either pass or show specific issues.
Checkpoint 4: ✅ Pre-commit hooks installed and working.
Create docker-compose.yml for Jenkins with security tools:
version: '3.8'
services:
jenkins:
image: jenkins/jenkins:lts
user: root
ports:
- "8080:8080"
- "50000:50000"
volumes:
- jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
- ./scripts:/scripts:ro
environment:
- JAVA_OPTS=-Djenkins.install.runSetupWizard=false
command: >
bash -c "
apt-get update &&
apt-get install -y docker.io curl wget python3 python3-pip jq &&
# Install security tools
curl -sSfL https://github.com/zricethezav/gitleaks/releases/download/v8.18.0/gitleaks_8.18.0_linux_x64.tar.gz | tar -xz &&
mv gitleaks /usr/local/bin/ &&
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh | sh -s -- -b /usr/local/bin v0.46.0 &&
wget -O hadolint https://github.com/hadolint/hadolint/releases/download/v2.12.0/hadolint-Linux-x86_64 &&
chmod +x hadolint && mv hadolint /usr/local/bin/ &&
pip3 install detect-secrets semgrep checkov bandit safety &&
git config --global --add safe.directory '*' &&
/usr/local/bin/jenkins.sh
"
volumes:
jenkins_home:# Start Jenkins
docker-compose up -d
# Wait for Jenkins to start
echo "⏳ Waiting for Jenkins to start (this may take 2-3 minutes)..."
sleep 120
# Get initial admin password
echo "🔑 Jenkins admin password:"
docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword- Access Jenkins: Open
http://localhost:8080 - Enter admin password from previous step
- Install suggested plugins
- Create admin user
Go to Manage Jenkins → Plugins → Available plugins and install:
- ✅ HTML Publisher Plugin
- ✅ Warnings Next Generation Plugin
- ✅ Pipeline Stage View Plugin
- ✅ Blue Ocean (optional)
GitHub Credentials:
- Go to Manage Jenkins → Credentials → System → Global credentials
- Click Add Credentials
- Kind: Username with password
- Username: Your GitHub username
- Password: Your GitHub Personal Access Token
- ID:
github-creds - Description: GitHub Personal Access Token
DockerHub Credentials:
- Same path, click Add Credentials
- Kind: Username with password
- Username: Your DockerHub username
- Password: Your DockerHub password/token
- ID:
dockerhub-creds - Description: DockerHub Registry Credentials
-
Create New Item:
- Name:
devsecops-sample-app - Type: Pipeline
- Name:
-
Configure Pipeline:
- Description: DevSecOps CI/CD Pipeline with Security Scanning
- Build Triggers: ✅ GitHub hook trigger for GITScm polling
- Pipeline Definition: Pipeline script from SCM
- SCM: Git
- Repository URL:
https://github.com/eknathdj/devops-sample-app - Credentials:
github-creds - Branch:
*/main - Script Path:
Jenkinsfile.devsecops
-
Save the pipeline
Checkpoint 5: ✅ Jenkins running with security tools, pipeline created.
# Test security tools in Jenkins container
docker exec jenkins bash -c "
echo '🔍 Testing security tools in Jenkins...'
gitleaks version
trivy version
hadolint --version
semgrep --version
checkov --version
echo '✅ All tools working in Jenkins'
"- Go to your pipeline in Jenkins UI
- Click "Build Now"
- Monitor the build - it should go through these stages:
- 🔧 Setup & Checkout
- 🔍 Pre-Build Security Scans
- 🔨 Build & Test
- 🔍 Container Security Scans
- 📊 Security Report Generation
After the build completes:
-
Check Build Artifacts:
- Go to build → Build Artifacts
- Download
security-scan-results.tar.gz - Extract and review reports
-
View Security Summary:
- Look for DevSecOps Security Report link
- Review security metrics and findings
Common first-run issues and fixes:
If Gitleaks fails:
# Check what secrets were detected
# Add false positives to .gitleaks.toml allowlistIf Trivy finds critical vulnerabilities:
# Update Dockerfile base image
# Add justified ignores to .trivyignoreIf Semgrep finds critical issues:
# Fix code issues or add ignore comments
# Adjust thresholds in Jenkinsfile.devsecops if neededCheckpoint 6: ✅ Pipeline running successfully, security reports generated.
- Go to your GitHub repository
- Settings → Webhooks → Add webhook
- Payload URL:
http://YOUR_JENKINS_IP:8080/github-webhook/ - Content type:
application/json - Events: Just the push event
- Active: ✅ Checked
For local testing with ngrok:
# Install ngrok
curl -s https://ngrok-agent.s3.amazonaws.com/ngrok.asc | sudo tee /etc/apt/trusted.gpg.d/ngrok.asc >/dev/null
echo "deb https://ngrok-agent.s3.amazonaws.com buster main" | sudo tee /etc/apt/sources.list.d/ngrok.list
sudo apt update && sudo apt install ngrok
# Expose Jenkins
ngrok http 8080
# Use the ngrok URL in webhook: https://xxxx.ngrok.io/github-webhook/# Make a small change to trigger webhook
echo "# DevSecOps Pipeline Test" >> README.md
git add README.md
git commit -m "Test DevSecOps pipeline webhook"
git push origin mainExpected Result: Jenkins should automatically start a new build.
- Check Jenkins - new build should start automatically
- Monitor security scans - all stages should pass
- Review artifacts - security reports should be generated
- Check DockerHub - new image should be pushed (if build succeeds)
Checkpoint 7: ✅ GitHub webhook working, automated builds triggered.
Edit Jenkinsfile.devsecops to adjust security gates for your needs:
environment {
// Adjust these based on your risk tolerance
SECURITY_GATE_CRITICAL = "0" # No critical vulnerabilities
SECURITY_GATE_HIGH = "5" # Max 5 high vulnerabilities
SECURITY_GATE_MEDIUM = "10" # Max 10 medium vulnerabilities
}# Create monitoring script
cat > monitor-security.sh << 'EOF'
#!/bin/bash
# Security monitoring script
echo "📊 DevSecOps Security Dashboard"
echo "================================"
# Check latest security reports
if [ -d "security-reports" ]; then
echo "📋 Latest Security Scan: $(ls -t security-reports/*.json | head -1 | xargs stat -c %y)"
# Count vulnerabilities
if [ -f "security-reports/trivy-report.json" ]; then
CRITICAL=$(jq '[.Results[]?.Vulnerabilities[]? | select(.Severity == "CRITICAL")] | length' security-reports/trivy-report.json 2>/dev/null || echo "0")
HIGH=$(jq '[.Results[]?.Vulnerabilities[]? | select(.Severity == "HIGH")] | length' security-reports/trivy-report.json 2>/dev/null || echo "0")
echo "🔍 Vulnerabilities: Critical: $CRITICAL, High: $HIGH"
fi
# Check secrets
if [ -f "security-reports/gitleaks-report.json" ]; then
SECRETS=$(jq '. | length' security-reports/gitleaks-report.json 2>/dev/null || echo "0")
echo "🔐 Secrets Detected: $SECRETS"
fi
else
echo "❌ No security reports found. Run security scan first."
fi
EOF
chmod +x monitor-security.sh# Create deployment documentation
cat > DEPLOYMENT.md << 'EOF'
# DevSecOps Deployment Guide
## Current Configuration
- **Jenkins**: http://localhost:8080
- **Pipeline**: devsecops-sample-app
- **Security Tools**: Gitleaks, Trivy, Semgrep, Checkov, Hadolint
- **Security Gates**: Critical: 0, High: 5, Medium: 10
- **Reports**: security-reports/ directory
## Daily Operations
1. **Monitor Security Dashboard**: `./monitor-security.sh`
2. **Review Failed Builds**: Check Jenkins for security gate failures
3. **Update Security Tools**: Monthly tool updates
4. **Rotate Credentials**: Quarterly credential rotation
## Emergency Procedures
1. **Critical Vulnerability**: Stop deployment, patch immediately
2. **Secret Detected**: Rotate credentials, clean git history
3. **Pipeline Failure**: Check security reports, fix issues
EOFCheckpoint 8: ✅ Production-ready DevSecOps pipeline configured.
Run through this checklist to ensure everything is working:
- All security tools installed and working
- Pre-commit hooks installed and tested
- Security scans run successfully locally
- Reports generated in security-reports/
- Jenkins running with security tools installed
- DevSecOps pipeline created and configured
- GitHub and DockerHub credentials added
- Pipeline runs successfully with security scans
- GitHub webhook configured and working
- Automated builds triggered on push
- Security gates enforced (build fails on threshold breach)
- Security reports archived and accessible
- No secrets detected in repository
- Vulnerability thresholds appropriate for your risk level
- Security baseline established and maintained
- Incident response procedures documented
You now have a fully functional DevSecOps pipeline with:
- 🔐 Multi-layer security scanning (secrets, SAST, container, IaC)
- 🛡️ Automated security gates with configurable thresholds
- 📊 Comprehensive security reporting and dashboards
- 🔄 Continuous integration with GitHub webhooks
- 🚀 Secure deployment pipeline with ArgoCD integration
- Train your team on the new DevSecOps workflow
- Set up monitoring and alerting for security events
- Schedule regular security tool updates
- Plan credential rotation schedule
- Conduct security reviews of the pipeline quarterly
If you encounter issues:
- Check the logs: Jenkins build logs, Docker logs
- Review security reports: Look for specific error messages
- Consult documentation: SECURITY.md and README.md
- Test individually: Run security tools manually to isolate issues
🎯 Congratulations! You've successfully implemented a production-ready DevSecOps pipeline!