adds commit hash to jenkins version.
This commit is contained in:
		| @@ -1,7 +1,46 @@ | ||||
| def label = "${UUID.randomUUID().toString()}" | ||||
| def sonarScannerConfig = """ | ||||
|  | ||||
| podTemplate( | ||||
|     label: label, | ||||
|     name: "pipeline-runner", | ||||
|     yaml: """--- | ||||
| apiVersion: v1 | ||||
| kind: Pod | ||||
| spec: | ||||
|   containers: | ||||
|   - name: golang | ||||
|     image: docker.io/golang:latest | ||||
|     tty: true | ||||
|     command: | ||||
|     - cat | ||||
|   - name: sonarscanner | ||||
|     imagePullPolicy: Always | ||||
|     image: registry.test-chamber-13.lan:5000/sonarscanner:latest | ||||
|     tty: true | ||||
|     command: | ||||
|     - cat | ||||
|   - name: kaniko | ||||
|     imagePullPolicy: Always | ||||
|     image: gcr.io/kaniko-project/executor:debug | ||||
|     tty: true | ||||
|     command: | ||||
|     - /busybox/cat | ||||
| """, | ||||
| ) { | ||||
|   node (label) { | ||||
|     def workspace = pwd() | ||||
|     def shortCommit | ||||
|  | ||||
|     stage('Clone Repository') { | ||||
|       git url: 'ssh://git@gitlab.smoothnet.org:31822/The_Spider/certificate-monitor.git', | ||||
|         credentialsId: 'sonarqube-read-access', | ||||
|         branch: 'master' | ||||
|       shortCommit = sh(returnStdout: true, script: "git log -n 1 --pretty=format:'%h'").trim() | ||||
|     } | ||||
|     stage('Prepare SonarScanner') { | ||||
|       def sonarScannerConfig = """ | ||||
| sonar.projectKey=${env.JOB_BASE_NAME} | ||||
| sonar.host.url=https://sonar.cluster.test-chamber-13.lan | ||||
| sonar.projectVersion=${shortCommit} | ||||
|  | ||||
| sonar.sources=. | ||||
| sonar.exclusions=**/*_test.go,**/vendor/**,**/testdata/* | ||||
| @@ -11,7 +50,31 @@ sonar.test.inclusions=**/*_test.go | ||||
| sonar.test.exclusions=**/vendor/** | ||||
| sonar.go.coverage.reportPaths=cover.out | ||||
| """ | ||||
| def dockerfile = """ | ||||
|       writeFile file: 'sonar-project.properties', text: sonarScannerConfig | ||||
|     } | ||||
|     stage('Run Tests') { | ||||
|       container('golang') { | ||||
|         sh """ | ||||
|           go get -u github.com/jstemmer/go-junit-report | ||||
|           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 ./... | grep -v vendor/` | ||||
|           go test -v 2>&1 | go-junit-report > report.xml | ||||
|         """ | ||||
|       } | ||||
|     } | ||||
|     stage('SonarQube Analysis') { | ||||
|       container('sonarscanner') { | ||||
|         withSonarQubeEnv('SonarQube') { | ||||
|           sh "sonar-scanner --define sonar.host.url=https://sonar.cluster.test-chamber-13.lan" | ||||
|         } | ||||
|       } | ||||
|     } | ||||
|     stage('Build COntainer') { | ||||
|       container('kaniko') { | ||||
|         def dockerfile = """ | ||||
| FROM golang:alpine AS BUILDER | ||||
| RUN apk --no-cache add git upx | ||||
| WORKDIR /go/src/app | ||||
| @@ -50,66 +113,6 @@ COPY --from=BUILDER /go/src/app/app /app/ | ||||
|  | ||||
| ENTRYPOINT ./app | ||||
| """ | ||||
|  | ||||
| podTemplate( | ||||
|     label: label, | ||||
|     name: "pipeline-runner", | ||||
|     yaml: """--- | ||||
| apiVersion: v1 | ||||
| kind: Pod | ||||
| spec: | ||||
|   containers: | ||||
|   - name: golang | ||||
|     image: docker.io/golang:latest | ||||
|     tty: true | ||||
|     command: | ||||
|     - cat | ||||
|   - name: sonarscanner | ||||
|     imagePullPolicy: Always | ||||
|     image: registry.test-chamber-13.lan:5000/sonarscanner:latest | ||||
|     tty: true | ||||
|     command: | ||||
|     - cat | ||||
|   - name: kaniko | ||||
|     imagePullPolicy: Always | ||||
|     image: gcr.io/kaniko-project/executor:debug | ||||
|     tty: true | ||||
|     command: | ||||
|     - /busybox/cat | ||||
| """, | ||||
| ) { | ||||
| 	node (label) { | ||||
| 		def workspace = pwd() | ||||
| 		stage('Clone Repository') { | ||||
| 			git url: 'ssh://git@gitlab.smoothnet.org:31822/The_Spider/certificate-monitor.git', | ||||
| 				credentialsId: 'sonarqube-read-access', | ||||
| 				branch: 'master' | ||||
| 		} | ||||
| 		stage('Prepare SonarScanner') { | ||||
| 			writeFile file: 'sonar-project.properties', text: sonarScannerConfig | ||||
| 		} | ||||
| 		container('golang') { | ||||
| 			stage('Run Tests') { | ||||
| 				sh """ | ||||
| 					go get -u github.com/jstemmer/go-junit-report | ||||
| 					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 ./... | grep -v vendor/` | ||||
| 					go test -v 2>&1 | go-junit-report > report.xml | ||||
| 				""" | ||||
| 			} | ||||
| 		} | ||||
| 		container('sonarscanner') { | ||||
| 			stage('SonarQube Analysis') { | ||||
| 				withSonarQubeEnv('SonarQube') { | ||||
| 					sh "sonar-scanner --define sonar.host.url=https://sonar.cluster.test-chamber-13.lan" | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 		container('kaniko') { | ||||
| 			stage('Build COntainer') { | ||||
|         writeFile file: workspace + '/Dockerfile', text: dockerfile | ||||
|         sh '/kaniko/executor --insecure --skip-tls-verify --context "' + workspace + '" -f "' + workspace + '/Dockerfile" --destination registry.test-chamber-13.lan:5000/certificate-monitor:latest' | ||||
|       } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user