下面是一个使用 docker slave 的 sonarqube 例子
pipeline {
agent none
environment {
SONAR_LOGIN='xxxxxxxxxxxx'
}
stages {
stage ('Checkout') {
agent any
steps {
git branch: 'master', credentialsId: 'sonarpoc', url: 'https://github.com/zl86790/sonarhelloworld.git'
sh "ls /var/jenkins_home/workspace/${env.JOB_NAME}/"
}
}
stage('Scan') {
agent {
docker {
image 'docker:20.10.6'
args "-v /var/run/docker.sock:/var/run/docker.sock -v /var/jenkins_home/workspace/${env.JOB_NAME}/:/usr/src"
}
}
steps {
dir("/var/jenkins_home/workspace/${env.JOB_NAME}/") {
sh "cp -r ./ /usr/src"
sh "ls -sail /usr/src"
sh """
docker run --rm -e SONAR_HOST_URL="http://xxx.xxx.xxx.xxx:9000/" -e SONAR_LOGIN='${env.SONAR_LOGIN}' -v /usr/src:/usr/src sonarsource/sonar-scanner-cli \
-Dsonar.projectKey=test -Dsonar.sources=/usr/src
"""
}
}
}
}
}
例子2
pipeline {
agent none
environment {
SONAR_LOGIN='310f5acc8da8d088640b7da3b1123f2d626e0d3d'
}
stages {
stage ('Checkout') {
agent any
steps {
sh "rm -rf /var/jenkins_home/workspace/${env.JOB_NAME}/*"
git branch: 'development', credentialsId: '574ec31b-8c2f-4454-83f0-0708b49289f8', url: 'https://github.com/xxx/xxxx.git'
}
}
stage('Scan') {
agent {
docker {
image 'docker:20.10.6'
args "-v /var/run/docker.sock:/var/run/docker.sock -v /var/jenkins_home/workspace/${env.JOB_NAME}/:/usr/src"
}
}
steps {
dir("/var/jenkins_home/workspace/${env.JOB_NAME}/") {
sh """
ls /usr/src
rm -rf /usr/src/*
ls /usr/src
ls /var/jenkins_home/workspace/${env.JOB_NAME}
cp -r /var/jenkins_home/workspace/${env.JOB_NAME}/* /usr/src/
ls /usr/src
docker run --rm -e SONAR_HOST_URL="http://xxx.xxx.xxx.xxx:9000/" -e SONAR_LOGIN='${env.SONAR_LOGIN}' -v /var/jenkins_home/workspace/${env.JOB_NAME}/:/usr/src sonarsource/sonar-scanner-cli \
-Dsonar.projectKey=test -Dsonar.sources=./
"""
}
}
}
}
}