use library functions and adds sonar scan
This commit is contained in:
162
Jenkinsfile
vendored
162
Jenkinsfile
vendored
@ -1,72 +1,124 @@
|
||||
// cmake '-DCMAKE_TOOLCHAIN_FILE=cmake/toolchain-mingw.cmake' .
|
||||
|
||||
def label = "jenkins-${UUID.randomUUID().toString()}"
|
||||
|
||||
def repository = "registry.c.test-chamber-13.lan"
|
||||
|
||||
podTemplate(
|
||||
label: label,
|
||||
name: "pipeline-worker",
|
||||
yaml: """---
|
||||
apiVersion: v1
|
||||
kind: Pod
|
||||
metadata:
|
||||
name: pipeline-worker
|
||||
spec:
|
||||
containers:
|
||||
- name: compile
|
||||
imagePullPolicy: Always
|
||||
image: ${repository}/dockerhub/library/golang:latest
|
||||
tty: true
|
||||
command:
|
||||
- /bin/cat
|
||||
""") {
|
||||
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()
|
||||
|
||||
container("compile") {
|
||||
stage ("Pull Local Repo") {
|
||||
checkout([
|
||||
$class: "GitSCM",
|
||||
branches: [
|
||||
[
|
||||
name: "refs/remotes/origin/main",
|
||||
],
|
||||
],
|
||||
userRemoteConfigs: [
|
||||
[
|
||||
url: "ssh://git@gitea.smoothnet.org:31822/nhyatt/go-temper.git",
|
||||
credentialsId: "Gitea-Read-Only-Token",
|
||||
],
|
||||
],
|
||||
extensions: [
|
||||
[
|
||||
$class: "CloneOption",
|
||||
shallow: true,
|
||||
],
|
||||
[
|
||||
$class: "CheckoutOption",
|
||||
timeout: 2,
|
||||
],
|
||||
],
|
||||
])
|
||||
}
|
||||
def shortCommit
|
||||
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("Build go-temper") {
|
||||
container("compile") {
|
||||
sh """
|
||||
apt update
|
||||
apt install --yes --no-install-recommends libusb-1.0-0-dev gcc g++
|
||||
GOOS=linux GOARCH=amd64 go build -ldflags="-s -w" -o go-temper ./cmd/go-temper
|
||||
"""
|
||||
stage('Run Tests') {
|
||||
container('golang') {
|
||||
writeFile(file: workspace + "/test-chamber-13.lan.root.crt", text: functions.getLocalRootCA())
|
||||
sh """
|
||||
if [ ! "/usr/bin/curl" ] || [ ! -x "/usr/bin/curl" ]; then
|
||||
apk add --no-cache curl
|
||||
fi
|
||||
if [ ! "/usr/bin/git" ] || [ ! -x "/usr/bin/git" ]; then
|
||||
apk add --no-cache git
|
||||
fi
|
||||
apk add --no-cache gcc musl-dev
|
||||
curl \
|
||||
--silent \
|
||||
--location \
|
||||
--cacert "${workspace}/test-chamber-13.lan.root.crt" \
|
||||
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 test -v ./... 2>&1 | go-junit-report > report.xml
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
stage('Prepare SonarScanner') {
|
||||
def sonarScannerConfig = """
|
||||
sonar.projectKey=${env.JOB_BASE_NAME.replace(" ", "-")}
|
||||
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 ("Store Artifacts") {
|
||||
archiveArtifacts (artifacts: "go-temper",
|
||||
fingerprint: true,
|
||||
allowEmptyArchive: false,
|
||||
onlyIfSuccessful: true,
|
||||
stage("Build go-temper") {
|
||||
container("golang") {
|
||||
sh """
|
||||
if [ ! "/usr/bin/git" ] || [ ! -x "/usr/bin/git" ]; then
|
||||
apk add --no-cache git
|
||||
fi
|
||||
if [ ! "/usr/bin/upx" ] || [ ! -x "/usr/bin/upx" ]; then
|
||||
apk add --no-cache upx
|
||||
fi
|
||||
apk add --no-cache libusb-dev gcc g++
|
||||
go install -v ./...
|
||||
GOOS=linux GOARCH=amd64 go build -v -ldflags="-s -w" -tags timetzdata -o tplink ./cmd/go-temper
|
||||
upx --lzma go-temper
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
stage ("Store Artifacts") {
|
||||
archiveArtifacts (artifacts: "go-temper",
|
||||
fingerprint: true,
|
||||
allowEmptyArchive: false,
|
||||
onlyIfSuccessful: true,
|
||||
)
|
||||
}
|
||||
|
||||
stage ('Push Artifacts') {
|
||||
container('alpine') {
|
||||
functions.pushArtifact(
|
||||
repoCreds: "nexus-generic-upload-bot",
|
||||
fileName: "go-temper",
|
||||
filePath: workspace + "/",
|
||||
fileURL: "https://nexus.c.test-chamber-13.lan/repository/generic/go/"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user