helm repo add jenkins https://charts.jenkins.io
helm repo update
helm pull jenkins/jenkins --untar
Run
helm upgrade --install jenkins jenkins/
or install without making any changes to the chart by running
helm upgrade --install jenkins jenkins/jenkins --set controller.servicePort=80 --set controller.serviceType=LoadBalancer
Get password
kubectl exec -it jenkins-0 -- bash
cat /run/secrets/additional/chart-admin-password
https://artifacthub.io/packages/helm/sonarqube/sonarqube
helm repo add sonarqube https://SonarSource.github.io/helm-chart-sonarqube
helm repo update
helm pull sonarqube/sonarqube --untar
Run
helm upgrade --install sonarqube sonarqube/
Password: admin
https://artifacthub.io/packages/helm/sonatype/nexus-repository-manager
helm repo add sonatype https://sonatype.github.io/helm3-charts/
helm repo update
helm pull sonatype/nexus-repository-manager --untar
Run
helm upgrade --install nexus nexus-repository-manager/
Get Password
kubectl exec -it nexus-nexus-repository-manager-5745766765-5pjcf -- bash
cat /nexus-data/admin.password
helm repo add codecentric https://codecentric.github.io/helm-charts
helm repo update
helm pull codecentric/mailhog --untar
helm upgrade --install mail mailhog/
https://kubernetes.io/docs/concepts/storage/persistent-volumes/
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: maven-cache
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
https://plugins.jenkins.io/kubernetes/
kubernetes {
yaml '''
apiVersion: v1
kind: Pod
metadata:
labels:
app: test
spec:
containers:
- name: maven
image: maven:3.8.3-adoptopenjdk-11
command:
- cat
tty: true
- name: git
image: bitnami/git:latest
command:
- cat
tty: true
'''
}
https://www.jenkins.io/doc/book/pipeline/syntax/#options
options {
buildDiscarder logRotator(daysToKeepStr: '2', numToKeepStr: '10')
timeout(time: 10, unit: 'MINUTES')
}
git url: 'https://github.com/kunchalavikram1427/spring-petclinic.git',
branch: 'main'
mvn -Dmaven.test.failure.ignore=true clean package
docker build -t $IMAGE_NAME:$IMAGE_TAG .
docker tag $IMAGE_NAME:$IMAGE_TAG $IMAGE_NAME:latest
https://docs.sonarqube.org/latest/analysis/scan/sonarscanner/ https://www.jenkins.io/doc/pipeline/steps/sonar/
withSonarQubeEnv(credentialsId: 'CREDS', installationName: 'SONARSERVER') {
sh '''/opt/sonar-scanner/bin/sonar-scanner \
-Dsonar.projectKey=petclinic \
-Dsonar.projectName=petclinic \
-Dsonar.projectVersion=1.0 \
-Dsonar.sources=src/main \
-Dsonar.tests=src/test \
-Dsonar.java.binaries=target/classes \
-Dsonar.language=java \
-Dsonar.sourceEncoding=UTF-8 \
-Dsonar.java.libraries=target/classes
'''
}
https://www.jenkins.io/doc/pipeline/steps/sonar/
timeout(time: 1, unit: 'MINUTES') {
waitForQualityGate abortPipeline: true
}
https://blog.sonatype.com/workflow-automation-publishing-artifacts-to-nexus-using-jenkins-pipelines
script {
pom = readMavenPom file: "pom.xml";
filesByGlob = findFiles(glob: "target/*.${pom.packaging}");
echo "${filesByGlob[0].name} ${filesByGlob[0].path} ${filesByGlob[0].directory} ${filesByGlob[0].length} ${filesByGlob[0].lastModified}"
artifactPath = filesByGlob[0].path;
artifactExists = fileExists artifactPath;
if(artifactExists) {
echo "*** File: ${artifactPath}, group: ${pom.groupId}, packaging: ${pom.packaging}, version ${pom.version}";
nexusArtifactUploader(
nexusVersion: NEXUS_VERSION,
protocol: NEXUS_PROTOCOL,
nexusUrl: NEXUS_URL,
groupId: pom.groupId,
version: pom.version,
repository: NEXUS_REPOSITORY,
credentialsId: NEXUS_CREDENTIAL_ID,
artifacts: [
[artifactId: pom.artifactId,
classifier: '',
file: artifactPath,
type: pom.packaging],
[artifactId: pom.artifactId,
classifier: '',
file: "pom.xml",
type: "pom"]
]
);
} else {
error "*** File: ${artifactPath}, could not be found";
}
}
withCredentials([usernamePassword(credentialsId: 'CREDS', passwordVariable: 'PASS', usernameVariable: 'USER')]) {
sh "curl -v -u $USER:$PASS --upload-file target/${pom.artifactId}-${pom.version}.${pom.packaging} \
http://NEXUS-SERVER-URL/repository/maven-hosted/org/springframework/samples/${pom.artifactId}/${pom.version}/${pom.artifactId}-${pom.version}.${pom.packaging}"
}
https://docs.github.com/en/rest/commits/statuses#create-a-commit-status
Install Generic webhook trigger plugin
ACTION
withCredentials([string(credentialsId: 'TOKEN', variable: 'TOKEN')]) {
sh "curl -u kunchalavikram1427:$TOKEN -X POST 'https://api.github.com/repos/kunchalavikram1427/spring-petclinic/statuses/$SHA_ID' -H 'Accept: application/vnd.github.v3+json' -d '{\"state\": \"success\",\"context\": \"Maven Build\", \"description\": \"Jenkins\", \"target_url\": \"$JENKINS_URL/job/$JOB_NAME/$BUILD_NUMBER/console\"}' "
}
In function form
void sendStatus(String stage, String status) {
container('curl') {
withCredentials([string(credentialsId: 'TOKEN', variable: 'TOKEN')]) {
sh "curl -u kunchalavikram1427:$TOKEN -X POST 'https://api.github.com/repos/kunchalavikram1427/spring-petclinic/statuses/$SHA_ID' -H 'Accept: application/vnd.github.v3+json' -d '{\"state\": \"$status\",\"context\": \"$stage\", \"description\": \"Jenkins\", \"target_url\": \"$JENKINS_URL/job/$JOB_NAME/$BUILD_NUMBER/console\"}' "
}
}
}
Call function
sendStatus("Push to Nexus","success")
sendStatus("Deployment","failure")
https://plugins.jenkins.io/kubernetes-cli/
withKubeConfig(caCertificate: '', clusterName: '', contextName: '', credentialsId: 'KUBECONFIG', namespace: '', serverUrl: '') {
sh "helm upgrade --install petclinic petclinic-chart/"
}
https://www.jenkins.io/doc/book/pipeline/syntax/#post https://plugins.jenkins.io/mailer/
post {
failure {
mail to: '[email protected]',
from: '[email protected]',
subject: "Jenkins pipeline has failed for job ${env.JOB_NAME}",
body: "Check build logs at ${env.BUILD_URL}"
}
success {
mail to: '[email protected]',
from: '[email protected]',
subject: "Jenkins pipeline for job ${env.JOB_NAME} is completed successfully",
body: "Check build logs at ${env.BUILD_URL}"
}
}
FROM openjdk:8-jre-alpine
EXPOSE 8080
COPY target/*.war /usr/bin/spring-petclinic.war
ENTRYPOINT ["java","-jar","/usr/bin/spring-petclinic.war","--server.port=8080"]
Image available in Dockerhub as: kunchalavikram/kubectl_helm_cli:latest
FROM alpine/helm
RUN curl -LO https://dl.k8s.io/release/v1.25.0/bin/linux/amd64/kubectl \
&& mv kubectl /bin/kubectl \
&& chmod a+x /bin/kubectl
apiVersion: apps/v1
kind: Deployment
metadata:
name: petclinic
labels:
app: petclinic
spec:
replicas: 1
selector:
matchLabels:
app: petclinic
template:
metadata:
labels:
app: petclinic
spec:
containers:
- name: petclinic
image: kunchalavikram/spring-petclinic:latest
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: petclinic
spec:
type: LoadBalancer
selector:
app: petclinic
ports:
- port: 80
targetPort: 8080
Upload Helm chart
helm package <folder-name>
curl -u admin:admin123 http://NEXUS-URL/repository/HELM-REPO-NAME/ --upload-file petclinic-chart-0.1.0.tgz -v
Adding Hosted Repo
helm repo add helm-hosted http://admin:admin123@NEXUS-URL/repository/HELM-REPO-NAME/
helm repo update
Fetching Charts from Nexus
helm repo update
helm pull helm-hosted/petclinic-chart
https://plugins.jenkins.io/pipeline-stage-view/
https://plugins.jenkins.io/nexus-artifact-uploader/
https://plugins.jenkins.io/pipeline-utility-steps/
https://plugins.jenkins.io/junit/
https://plugins.jenkins.io/kubernetes-cli/
https://plugins.jenkins.io/sonar/
https://plugins.jenkins.io/generic-webhook-trigger/
Use these parameters if invoking Sonar Maven Plugin
-Dsonar.host.url=http://SONAR-URL
-Dsonar.login=sqp_dfe411bbe58dfba863f942c6b5fcac2f79e7db1a
https://github.com/jenkinsci/kubernetes-plugin/tree/master/examples
https://help.sonatype.com/repomanager3/nexus-repository-administration/formats/docker-registry/ssl-and-repository-connector-configuration
https://support.sonatype.com/hc/en-us/articles/217542177?_ga=2.135356529.1307852621.1661838709-1983751057.1661838709
- Vikram K (www.youtube.com/c/devopsmadeeasy)