Skip to content

Commit 72a3902

Browse files
authored
Use admonition for host IP alternative (#41)
1 parent f471c70 commit 72a3902

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

docs/tutorials/jenkins/sonarqube-jenkins/Jenkinsfile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
1-
def net = '' // Set the value to 'host' to use host networking.
1+
def attachJenkinsNetwork = true
2+
// If true, the pipeline will attach the SonarQube scanner to the same network
3+
// with Jenkins and SonarQube containers.
4+
5+
def scannerArgs = ''
26

37
pipeline {
48
agent any
59
stages {
610
stage ('Prepare') {
711
steps {
812
script {
9-
if (net == '') {
13+
if (attachJenkinsNetwork == true) {
1014
// Get the name of the network that Jenkins container is using.
11-
net = sh returnStdout: true, script: 'docker inspect $(hostname) -f \'{{ range $k, $v := .NetworkSettings.Networks }}{{ $k }}{{ end }}\''
15+
def net = sh returnStdout: true, script: 'docker inspect $(hostname) -f \'{{ range $k, $v := .NetworkSettings.Networks }}{{ $k }}{{ end }}\''
16+
scannerArgs = "--network ${net.trim()}"
1217
}
1318
}
1419
}
@@ -24,7 +29,7 @@ pipeline {
2429
docker {
2530
image 'sonarsource/sonar-scanner-cli'
2631
// In order to be able to use http://sonarqube:9000 we need to be in the same network as Jenkins and SonarQube are in.
27-
args "--net $net"
32+
args scannerArgs
2833
// To quarantee that the workspace contains the sources pulled in previous stage, we need to use the pipeline level workspace.
2934
reuseNode true
3035
}

docs/tutorials/jenkins/sonarqube-jenkins/README.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ To get started with SonarQube, access the [Web UI](http://localhost:9000) and lo
2929

3030
In order to test the SonarQube installation, run `sonar-scanner` in a code repository. This can be done, for example, by using the [sonarsource/sonar-scanner-cli](https://hub.docker.com/r/sonarsource/sonar-scanner-cli) Docker image:
3131

32-
```bash
32+
```sh
3333
# This command should be run in the repository root directory
3434
docker run \
3535
--rm \
@@ -47,16 +47,20 @@ If this succeeds, you are ready to move this analysis into Jenkins.
4747

4848
First, install SonarQube Scanner plugin to your Jenkins instance through [Manage Jenkins > Manage plugins](http://localhost:8080/pluginManager/available) menu. Configure SonarQube instance to SonarQube servers section of the [Manage Jenkins > Configure System](http://localhost:8080/configure) menu: Use `http://sonarqube:9000` as the server URL and create a secret text credential for the access token you stored earlier.
4949

50+
??? example "Alternatives for using `sonarqube` hostname"
51+
52+
The `sonarqube` domain name is by default only available for containers attached to the same network. Thus the the connection from Jenkins container works with the `sonarqube` hostname, but opening these links from the browser will fail.
53+
54+
To define an URL that works in both Jenkins and browser, we could use hosts IP address instead of the `sonarqube` hostname. The URL would then be in `http://${HOST_IP}:9000` format. On linux systems, you can find your local IP with `hostname -I` command. On Windows systems, you can find your local by looking for IP address from the output of `ipconfig` command. Configure the with hosts IP to the [Manage Jenkins > Configure System](http://localhost:8080/configure) menu.
55+
56+
When using URL with hosts IP, you can omit the network argument from the docker agent block and the links from the job view should work as is. In the example Jenkinsfile this can be done by setting `attachJenkinsNetwork` to false.
57+
5058
After the SonarQube server is configured to Jenkins, sonar-scanner can be executed in a stage that uses the same [sonarsource/sonar-scanner-cli](https://hub.docker.com/r/sonarsource/sonar-scanner-cli) Docker image that was used in the previous step as well. This can be done with a stage level Docker agent:
5159

5260
```Groovy title="Jenkinsfile"
5361
---8<-- "docs/tutorials/jenkins/sonarqube-jenkins/Jenkinsfile"
5462
```
5563

56-
See [Jenkinsfile](./Jenkinsfile) for a example of a complete pipeline. If you try to execute this example pipeline replace `${GIT_URL}` with the URL to your git repository of choice.
57-
5864
After the pipeline with sonar-scanner run has been executed, the job view in Jenkins should include the SonarQube quality gate status of the linked Sonar Project. Note that in this demo setup these links will not work as the Jenkins uses the `http://sonarqube:9000` URL for the SonarQube server which is likely not accessible from your browser. To see the projects in SonarQube, replace `http://sonarqube:9000` with `http://localhost:9000` in the URL.
5965

60-
Alternative for using `http://sonarqube:9000` or `http://localhost:9000` as the SonarQube URL would be to use your host machines local IP: `http://${HOST_IP}:9000`. On linux systems, you can find your local IP with `hostname -I` command. On Windows systems, you can find your local by looking for IP address from the output of `ipconfig` command. You only need to configure this to the [Manage Jenkins > Configure System](http://localhost:8080/configure) menu. When using local host IP, you can omit the network argument from docker agent block and the links from the job view should work as is.
61-
6266
Note that this setup should only be used for development. For anything production like, configure SonarQube to use database such as postgres, do not use root or admin credentials, and setup the Jenkins and SonarQube to a suitable private network. See also Jenkins and SonarQube documentation for production usage instructions.

0 commit comments

Comments
 (0)