From 45cfc5c6fd161277a88d7537d41c61be607b4d1d Mon Sep 17 00:00:00 2001 From: Toni Kangas Date: Sat, 5 Jul 2025 14:19:20 +0300 Subject: [PATCH] Use `docker inspect` to parse network name --- .../jenkins/sonarqube-jenkins/Jenkinsfile | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/tutorials/jenkins/sonarqube-jenkins/Jenkinsfile b/docs/tutorials/jenkins/sonarqube-jenkins/Jenkinsfile index 2a3e169..b6ace66 100644 --- a/docs/tutorials/jenkins/sonarqube-jenkins/Jenkinsfile +++ b/docs/tutorials/jenkins/sonarqube-jenkins/Jenkinsfile @@ -1,9 +1,22 @@ +def net = '' // Set the value to 'host' to use host networking. + pipeline { agent any stages { + stage ('Prepare') { + steps { + script { + if (net == '') { + // Get the name of the network that Jenkins container is using. + net = sh returnStdout: true, script: 'docker inspect $(hostname) -f \'{{ range $k, $v := .NetworkSettings.Networks }}{{ $k }}{{ end }}\'' + } + } + } + } stage('Checkout') { steps { - git "${GIT_URL}" + // Replace the git step below with step that pulls the repository you want to analyze. + git branch: 'main', url: 'https://github.com/cicd-tutorials/feedback.git' } } stage('Analyze') { @@ -11,7 +24,7 @@ pipeline { docker { image 'sonarsource/sonar-scanner-cli' // In order to be able to use http://sonarqube:9000 we need to be in the same network as Jenkins and SonarQube are in. - args '--net jenkins_default' + args "--net $net" // To quarantee that the workspace contains the sources pulled in previous stage, we need to use the pipeline level workspace. reuseNode true }