Add Jenkins pipeline with automated builds

This commit is contained in:
arthurribeiro6 2025-10-31 18:49:27 -04:00
parent 725a9c7017
commit 1a19fb2866
3 changed files with 321 additions and 0 deletions

104
Jenkinsfile vendored Normal file
View file

@ -0,0 +1,104 @@
pipeline {
agent any
tools {
maven 'Maven 3.9.5'
jdk 'JDK 17'
}
triggers {
pollSCM('H/5 * * * *')
}
environment {
MAVEN_OPTS = '-Xmx1024m'
PROJECT_NAME = 'spring-petclinic'
}
stages {
stage('Checkout') {
steps {
echo 'Checking out code...'
checkout scm
script {
sh 'git rev-parse --short HEAD'
sh 'git log -1 --pretty=%B'
}
}
}
stage('Build') {
steps {
echo 'Building project...'
sh './mvnw clean compile -DskipTests'
}
}
stage('Test') {
steps {
echo 'Running tests...'
sh './mvnw test'
}
post {
always {
junit testResults: '**/target/surefire-reports/*.xml', allowEmptyResults: true
jacoco(
execPattern: '**/target/jacoco.exec',
classPattern: '**/target/classes',
sourcePattern: '**/src/main/java',
exclusionPattern: '**/*Test*.class'
)
}
}
}
stage('Code Quality') {
steps {
echo 'Running checkstyle...'
sh './mvnw checkstyle:checkstyle'
}
post {
always {
recordIssues(
enabledForFailure: true,
tool: checkStyle(pattern: '**/target/checkstyle-result.xml')
)
}
}
}
stage('Package') {
steps {
echo 'Packaging application...'
sh './mvnw package -DskipTests'
}
}
stage('Archive') {
steps {
echo 'Archiving artifacts...'
archiveArtifacts artifacts: '**/target/*.jar',
fingerprint: true,
allowEmptyArchive: false
}
}
}
post {
success {
echo 'Build successful!'
}
failure {
echo 'Build failed!'
}
always {
cleanWs(
cleanWhenNotBuilt: false,
deleteDirs: true,
disableDeferredWipeout: true,
notFailBuild: true
)
}
}
}

103
Jenkinsfile.gradle Normal file
View file

@ -0,0 +1,103 @@
pipeline {
agent any
tools {
jdk 'JDK 17'
}
triggers {
pollSCM('H/5 * * * *')
}
environment {
GRADLE_OPTS = '-Xmx1024m -Dorg.gradle.daemon=false'
PROJECT_NAME = 'spring-petclinic'
}
stages {
stage('Checkout') {
steps {
echo 'Checking out code...'
checkout scm
script {
sh 'git rev-parse --short HEAD'
sh 'git log -1 --pretty=%B'
}
}
}
stage('Build') {
steps {
echo 'Building with Gradle...'
sh './gradlew clean compileJava --no-daemon'
}
}
stage('Test') {
steps {
echo 'Running tests...'
sh './gradlew test --no-daemon'
}
post {
always {
junit testResults: '**/build/test-results/test/*.xml', allowEmptyResults: true
jacoco(
execPattern: '**/build/jacoco/*.exec',
classPattern: '**/build/classes',
sourcePattern: '**/src/main/java',
exclusionPattern: '**/*Test*.class'
)
}
}
}
stage('Code Quality') {
steps {
echo 'Running checkstyle...'
sh './gradlew checkstyleMain checkstyleTest --no-daemon'
}
post {
always {
recordIssues(
enabledForFailure: true,
tool: checkStyle(pattern: '**/build/reports/checkstyle/*.xml')
)
}
}
}
stage('Package') {
steps {
echo 'Packaging application...'
sh './gradlew bootJar --no-daemon'
}
}
stage('Archive') {
steps {
echo 'Archiving artifacts...'
archiveArtifacts artifacts: '**/build/libs/*.jar',
fingerprint: true,
allowEmptyArchive: false
}
}
}
post {
success {
echo 'Build successful!'
}
failure {
echo 'Build failed!'
}
always {
cleanWs(
cleanWhenNotBuilt: false,
deleteDirs: true,
disableDeferredWipeout: true,
notFailBuild: true
)
}
}
}

114
jenkins-quickstart.sh Executable file
View file

@ -0,0 +1,114 @@
#!/bin/bash
# Jenkins Quick Start Script for Spring PetClinic
# This script helps you set up and configure Jenkins for the project
set -e
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print colored output
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Function to check if a command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Check prerequisites
print_info "Checking prerequisites..."
if ! command_exists docker; then
print_error "Docker is not installed. Please install Docker first."
exit 1
fi
if ! command_exists docker-compose || ! command_exists "docker compose"; then
print_error "Docker Compose is not installed. Please install Docker Compose first."
exit 1
fi
print_success "Prerequisites check passed!"
# Start Jenkins
print_info "Starting Jenkins container..."
docker compose up -d jenkins
# Wait for Jenkins to start
print_info "Waiting for Jenkins to start (this may take a minute)..."
sleep 30
# Check if Jenkins is running
if ! docker ps | grep -q jenkins; then
print_error "Jenkins container failed to start"
exit 1
fi
print_success "Jenkins container is running!"
# Get initial admin password
print_info "Retrieving Jenkins initial admin password..."
INITIAL_PASSWORD=$(docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword 2>/dev/null || echo "")
if [ -z "$INITIAL_PASSWORD" ]; then
print_warning "Could not retrieve initial admin password. Jenkins might still be initializing."
print_info "You can retrieve it later with: docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword"
else
print_success "Initial Admin Password: ${GREEN}${INITIAL_PASSWORD}${NC}"
echo ""
echo "=================================================="
echo "IMPORTANT: Save this password!"
echo "Initial Admin Password: ${INITIAL_PASSWORD}"
echo "=================================================="
echo ""
fi
# Display access information
echo ""
print_info "Jenkins Setup Information:"
echo " - Jenkins URL: http://localhost:8080"
echo " - Container Name: jenkins"
echo " - Default Admin Password: See above"
echo ""
print_info "Next Steps:"
echo " 1. Open http://localhost:8080 in your browser"
echo " 2. Enter the initial admin password shown above"
echo " 3. Click 'Install suggested plugins'"
echo " 4. Create your first admin user"
echo " 5. Follow the setup guide in JENKINS_SETUP.md"
echo ""
print_info "Useful Commands:"
echo " - View Jenkins logs: docker logs -f jenkins"
echo " - Stop Jenkins: docker compose stop jenkins"
echo " - Restart Jenkins: docker restart jenkins"
echo " - Get admin password: docker exec jenkins cat /var/jenkins_home/secrets/initialAdminPassword"
echo ""
print_info "To set up the complete DevOps stack (Jenkins, SonarQube, Prometheus, Grafana, ZAP):"
echo " docker compose up -d"
echo ""
print_success "Jenkins quick start completed!"
print_info "For detailed setup instructions, see JENKINS_SETUP.md"