diff --git a/Jenkinsfile b/Jenkinsfile index b2776ef..3c95805 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,69 +1,84 @@ -def label = "jenkins-${UUID.randomUUID().toString()}" + +#!groovy def repository = "registry.c.test-chamber-13.lan" def repositoryCreds = "harbor-repository-creds" +def label = "kubernetes-${UUID.randomUUID().toString()}" +def templateName = "pipeline-worker" + podTemplate( label: label, - name: "pipeline-worker", - yaml: """--- -apiVersion: v1 -kind: Pod -metadata: - name: pipeline-worker -spec: - containers: - - name: kaniko - imagePullPolicy: Always - image: ${repository}/library/kaniko:latest - tty: true - command: - - /busybox/sh - - name: compile - imagePullPolicy: Always - image: ${repository}/dockerhub/library/golang:latest - tty: true - command: - - /bin/sh - - name: alpine - image: ${repository}/library/alpine:latest - imagePullPolicy: Always - tty: true - command: - - /bin/sh -""") { + name: templateName, + yaml: functions.podYaml( + repo: repository, + templateName: templateName, + kaniko: true, + alpine: true, + [ + [ + name: "sonar", + path: "${repository}/library/sonarscanner:latest", + command: "/bin/sh" + ], + [ + name: "golang", + path: "${repository}/dockerhub/library/golang:alpine", + command: "/bin/sh" + ] + ] + ) +) { node (label) { def workspace = pwd() - stage ("Pull Local Repo") { - checkout([ - $class: "GitSCM", - branches: [ - [ - name: "refs/remotes/origin/main", - ], - ], - userRemoteConfigs: [ - [ - url: "ssh://git@gitea.smoothnet.org:31822/nhyatt/tplinkcmd.git", - credentialsId: "Gitea-Read-Only-Token", - ], - ], - extensions: [ - [ - $class: "CloneOption", - shallow: true, - ], - [ - $class: "CheckoutOption", - timeout: 2, - ], - ], + stage('Clone Repository') { + checkout ([$class: "GitSCM", + branches: scm.branches, + extensions: scm.extensions + [$class: 'CloneOption', shallow: true], + userRemoteConfigs: scm.userRemoteConfigs, ]) + shortCommit = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim() + } + + stage('Run Tests') { + container('golang') { + sh """ + curl --silent --location https://nexus.c.test-chamber-13.lan/repository/github-releases/jstemmer/go-junit-report/releases/download/v1.0.0/go-junit-report-v1.0.0-linux-amd64.tar.gz | tar -z -x -f - -C /usr/local/bin + ln -s "${workspace}" "/go/src/${env.JOB_BASE_NAME}" + cd "/go/src/${env.JOB_BASE_NAME}" + go get -d -v ./... + go install -v ./... + go test -short -coverprofile=cover.out `go list ./...` + go test -v 2>&1 | go-junit-report > report.xml + """ + } + } + + stage('Prepare SonarScanner') { + def sonarScannerConfig = """ +sonar.projectKey=emonesp-exporter +sonar.projectVersion=${shortCommit} +sonar.sources=. +sonar.exclusions=**/*_test.go,**/vendor/**,**/testdata/* +sonar.tests=. +sonar.test.inclusions=**/*_test.go +sonar.test.exclusions=**/vendor/** +sonar.go.coverage.reportPaths=cover.out +""" + writeFile file: 'sonar-project.properties', text: sonarScannerConfig + } + + stage('SonarQube Analysis') { + container('sonar') { + withSonarQubeEnv('SonarQube') { + sh "sonar-scanner --define sonar.host.url=https://sonar.c.test-chamber-13.lan" + } + } } stage("Build tplinkcmd") { - container("compile") { + container("golang") { sh """ apt-get update apt-get install upx -y @@ -82,28 +97,19 @@ spec: ) } - container ("kaniko") { - stage ("Prepare Kaniko") { - withCredentials([usernameColonPassword( - credentialsId: repositoryCreds, - variable: "dCreds", - )]) { - def dockerJSON = """{ - "auths": { - "${repository}": { - "auth": "${dcreds.bytes.encodeBase64().toString()}" - } - } - }""" - sh """ - set +x - echo '${dockerJSON}' > /kaniko/.docker/config.json - """ - } + stage ('Push Artifacts') { + container('alpine') { + functions.pushArtifact( + repoCreds: "nexus-generic-upload-bot", + fileName: "tplink", + filePath: workspace + "/", + fileURL: "https://nexus.c.test-chamber-13.lan/repository/generic/go/" + ) } + } - stage ("Build Container") { - def DF = """ + stage ("Build Container") { + def DF = """ FROM ${repository}/library/alpine:latest as certHost FROM ${repository}/dockerhub/library/golang:alpine as builder @@ -131,35 +137,30 @@ WORKDIR /app/ ENTRYPOINT ["/app/app"] """ - writeFile(file: workspace + "/Dockerfile", text: DF) - sh """ - /kaniko/executor \\ - --cleanup \\ - --context "${workspace}" \\ - -f "${workspace}/Dockerfile" \\ - --destination "${repository}/library/tplink-api:latest" - """ + writeFile(file: workspace + "/Dockerfile", text: DF) + sh """ + /kaniko/executor \\ + --cleanup \\ + --context "${workspace}" \\ + -f "${workspace}/Dockerfile" \\ + --destination "${repository}/library/tplink-api:latest" + """ + } + + stage('Delete Running Pod') { + functions.deletePod( + kubeAuth: "k8s-monitoring-access", + kubeURL: "https://kubernetes.test-chamber-13.lan:6443", + namespace: "monitoring", + selector: "app=emonesp-exporter" + ) + } + + stage('Submit Testing Report to Jenkins') { + catchError{ + junit 'report.xml' } } - stage ("Update Deployment") { - container ("alpine") { - sh """ - apk add --no-cache curl - curl -L --silent https://storage.googleapis.com/kubernetes-release/release/\$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl - chmod +x /usr/local/bin/kubectl - """ - - withKubeConfig([ - credentialsId: "rancher-admin-token", - serverUrl: "https://rancher.test-chamber-13.lan/k8s/clusters/c-mc9cq", - namespace: "webservers" - ]) { - sh "for i in \$(kubectl get pods --selector app=\"tplink-api\" -o name); do kubectl delete \${i}; done" - } - } - } - - } } \ No newline at end of file