From 4030a967e7ac9c77865af226159fb4ae6e6fde18 Mon Sep 17 00:00:00 2001 From: sunt9751 Date: Sun, 21 Jul 2024 16:30:33 -0700 Subject: [PATCH] add jenkins --- Dockerfile.jenkins | 18 ++++++++++++++++ Jenkinsfile | 49 ++++++++++++++++++++++++++++++++++++++++++++ jenkins.yaml | 47 ++++++++++++++++++++++++++++++++++++++++++ spring-petclinic.yml | 16 +++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 Dockerfile.jenkins create mode 100644 Jenkinsfile create mode 100644 jenkins.yaml diff --git a/Dockerfile.jenkins b/Dockerfile.jenkins new file mode 100644 index 000000000..4c623ddab --- /dev/null +++ b/Dockerfile.jenkins @@ -0,0 +1,18 @@ +FROM jenkins/jenkins:lts + +USER root +RUN apt-get update && apt-get install -y docker.io + +# Install plugins +RUN jenkins-plugin-cli --plugins prometheus configuration-as-code + +# Copy JCasC configuration file +COPY jenkins.yaml /var/jenkins_home/casc_configs/jenkins.yaml + +# Set environment variable for JCasC +ENV CASC_JENKINS_CONFIG=/var/jenkins_home/casc_configs/jenkins.yaml + +USER jenkins + +# Expose the port the app runs on +EXPOSE 8080 \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 000000000..f339986f3 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,49 @@ +pipeline { + agent any + + environment { + DOCKER_CREDENTIALS_ID = 'your-docker-credentials-id' // Replace with your actual Docker credentials ID + } + + stages { + stage('Checkout') { + steps { + checkout scm + } + } + stage('Build') { + steps { + script { + sh './mvnw clean package' + } + } + } + stage('Build Docker Image') { + steps { + script { + // Build the Docker image + sh 'docker build -t petclinic:latest .' + } + } + } + stage('Push Docker Image') { + steps { + script { + // Push the Docker image to a registry + docker.withRegistry('https://index.docker.io/v1/', "${DOCKER_CREDENTIALS_ID}") { + sh 'docker tag petclinic:latest your-docker-username/petclinic:latest' + sh 'docker push your-docker-username/petclinic:latest' + } + } + } + } + stage('Deploy') { + steps { + script { + // Deploy the application using Docker Compose + sh 'docker-compose up -d' + } + } + } + } +} diff --git a/jenkins.yaml b/jenkins.yaml new file mode 100644 index 000000000..b32ef4b9c --- /dev/null +++ b/jenkins.yaml @@ -0,0 +1,47 @@ +jenkins: + systemMessage: "Jenkins configured automatically by Jenkins Configuration as Code plugin\n\n" + securityRealm: + local: + allowsSignup: false + users: + - id: "admin" + password: "admin" + authorizationStrategy: + loggedInUsersCanDoAnything: + allowAnonymousRead: true + +unclassified: + location: + url: "http://localhost:8080/" + prometheus: + defaultQuantiles: + - name: "0.5" + value: 0.5 + - name: "0.9" + value: 0.9 + - name: "0.99" + value: 0.99 + defaultBuckets: + - name: "0.1" + value: 0.1 + - name: "0.2" + value: 0.2 + - name: "0.5" + value: 0.5 + - name: "1.0" + value: 1.0 + - name: "2.5" + value: 2.5 + - name: "5.0" + value: 5.0 + - name: "10.0" + value: 10.0 + +tools: + installations: + - name: "JDK11" + jdkInstaller: + id: "jdk-11.0.2+9" + - name: "Maven3" + mavenInstaller: + id: "maven-3.6.3" diff --git a/spring-petclinic.yml b/spring-petclinic.yml index d79190c4b..aa0c2967a 100644 --- a/spring-petclinic.yml +++ b/spring-petclinic.yml @@ -33,9 +33,25 @@ services: depends_on: - prometheus + jenkins: + build: + context: . + dockerfile: Dockerfile.jenkins + ports: + - "8081:8080" + - "50000:50000" + volumes: + - jenkins_data:/var/jenkins_home + - ./jenkins.yaml:/var/jenkins_home/casc_configs/jenkins.yaml + networks: + - custom-network + environment: + - JENKINS_OPTS=--prefix=/jenkins + volumes: prometheus_data: grafana_data: + jenkins_data: networks: custom-network: \ No newline at end of file