Last active
October 26, 2024 11:26
-
-
Save davesave/74876b498bf8c118ffd1fc2e66d8ec70 to your computer and use it in GitHub Desktop.
Basic maven + Jenkinsfile + Dockerfile + Jenkins Agent on kubernetes: pipeline example for push with dind container, eclipse, jdk and dockerhub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Stage 1: Build the application using Maven | |
# FROM maven:3.8.6-openjdk-17 AS build | |
# WORKDIR /app | |
# COPY . . | |
# RUN mvn clean package -DskipTests | |
# Stage 1: jar is build on ci server | |
FROM busybox:latest AS build | |
WORKDIR /app | |
COPY target/my-app-1.0-SNAPSHOT.jar app.jar | |
################################################################ | |
# Stage 2: Package the application using the JDK as a base image | |
FROM eclipse-temurin:17-jdk-alpine | |
WORKDIR /app | |
COPY --from=build /app/app.jar app.jar | |
EXPOSE 8080 | |
ENTRYPOINT ["java", "-jar", "/app/app.jar"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @Library('sharedlib') _ | |
pipeline { | |
// agent any | |
agent { | |
kubernetes { | |
yamlFile 'JenkinsPodTemplate.yaml' | |
} | |
} | |
options { | |
skipDefaultCheckout true | |
ansiColor('xterm') | |
timeout(time: 1, unit: 'HOURS') | |
timestamps() | |
disableRestartFromStage() | |
disableConcurrentBuilds() | |
buildDiscarder(logRotator(numToKeepStr: '10')) | |
} | |
stages { | |
stage('Checkout') { | |
steps { | |
checkout scmGit(branches: [[name: '*/master']], | |
extensions: [], | |
userRemoteConfigs: [[url: 'https://github.com/jenkins-docs/simple-java-maven-app']]) | |
} | |
} | |
stage('Compile') { | |
steps { | |
container('maven') { | |
sh 'mvn -B --no-transfer-progress -DskipTests clean package' | |
} | |
} | |
} | |
stage('Unit Test') { | |
steps { | |
container('maven') { | |
sh 'mvn -B --no-transfer-progress test' | |
} | |
} | |
post { | |
always { | |
junit 'target/surefire-reports/*.xml' | |
} | |
} | |
} | |
stage('Build OCI Image') { | |
steps { | |
// DOCKERFILE IN ANOTHER REPO | |
checkout scmGit(branches: [[name: '*/main']], | |
extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'anotherrepo']], | |
userRemoteConfigs: [[credentialsId: 'gittea', url: '[email protected]:yourorgrepo/anotherrepo.git']]) | |
container('dind') { | |
sh 'cp anotherrepo/Dockerfile .' | |
sh 'docker build --tag b1234 .' | |
sh 'docker tag b1234 yourepo/reponame:tagname' | |
} | |
} | |
} | |
stage('Push OCI Image') { | |
steps { | |
withCredentials([string(credentialsId: 'DOCKERHUB_AUTH_TOKEN', variable: 'JENKINS_DOCKERHUB_AUTH_TOKEN')]) { | |
container('dind') { | |
sh 'echo \'{ "auths": { "https://index.docker.io/v1/": { "auth": "\'$JENKINS_DOCKERHUB_AUTH_TOKEN\'" } } }\'>~/.docker/config.json' | |
sh 'docker push yourepo/reponame:tagname' | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apiVersion: v1 | |
kind: Pod | |
spec: | |
volumes: | |
- name: docker-sock | |
emptyDir: {} | |
containers: | |
- name: maven | |
image: maven:3.9.9-eclipse-temurin-17 | |
command: | |
- cat | |
tty: true | |
resources: | |
limits: | |
memory: "512Mi" | |
cpu: "1" | |
requests: | |
memory: "128Mi" | |
cpu: "0.1" | |
- name: dind | |
image: docker:dind | |
volumeMounts: | |
- name: docker-sock | |
mountPath: /var/run | |
securityContext: | |
privileged: true | |
resources: | |
limits: | |
memory: "512Mi" | |
cpu: "1" | |
requests: | |
memory: "128Mi" | |
cpu: "0.1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment