Add cmake-sonar job 09/14509/1
authorThanh Ha <thanh.ha@linuxfoundation.org>
Sat, 9 Feb 2019 22:02:00 +0000 (17:02 -0500)
committerThanh Ha <thanh.ha@linuxfoundation.org>
Sat, 9 Feb 2019 22:22:07 +0000 (17:22 -0500)
This job only supports SonarCloud configuration.

Change-Id: I4f2b5100e3316f21c866155199aeed0533d13b8b
Signed-off-by: Thanh Ha <thanh.ha@linuxfoundation.org>
docs/jjb/lf-c-cpp-jobs.rst
jjb/lf-c-cpp-jobs.yaml
shell/cmake-sonar.sh [new file with mode: 0644]

index 17d4581..0e01491 100644 (file)
@@ -5,6 +5,61 @@ C/C++ Jobs
 Job Templates
 =============
 
+CMake Sonar
+-----------
+
+Sonar job which runs cmake && make then publishes to Sonar.
+
+This job purposely runs on the master branch as there are configuration needed
+to support multi-branch.
+
+:Template Names:
+
+    - {project-name}-cmake-sonar
+    - gerrit-cmake-sonar
+    - github-cmake-sonar
+
+:Comment Trigger: run-sonar
+
+:Required parameters:
+
+    :build-node: The node to run build on.
+    :jenkins-ssh-credential: Credential to use for SSH. (Configure in
+        defaults.yaml)
+    :sonar-scanner-version: Version of sonar-scanner to install.
+    :sonarcloud-project-key: SonarCloud project key.
+    :sonarcloud-project-organization: SonarCloud project organization.
+    :sonarcloud-api-token: SonarCloud API Token.
+
+:Optional parameters:
+
+    :build-days-to-keep: Days to keep build logs in Jenkins. (default: 7)
+    :build-timeout: Timeout in minutes before aborting build. (default: 60)
+    :cmake-opts: Parameters to pass to cmake. (default: '')
+    :cron: Cron schedule when to trigger the job. This parameter also
+        supports multiline input via YAML pipe | character in cases where
+        one may want to provide more than 1 cron timer.  (default: '@daily')
+    :git-url: URL clone project from. (default: $GIT_URL/$PROJECT)
+    :install-prefix: CMAKE_INSTALL_PREFIX to use for install.
+        (default: $BUILD_DIR/output)
+
+        .. code-block:: bash
+           :caption: Example
+
+           install-prefix: |
+               #!/bin/bash
+               echo "Hello World."
+
+    :make-opts: Parameters to pass to make. (default: '')
+    :pre-build: Shell script to run before performing build. Useful for
+        setting up dependencies. (default: '')
+    :submodule-recursive: Whether to checkout submodules recursively.
+        (default: true)
+    :submodule-timeout: Timeout (in minutes) for checkout operation.
+        (default: 10)
+
+    :gerrit_sonar_triggers: Override Gerrit Triggers.
+
 CMake Stage
 -----------
 
index 07e5ec4..2eb394e 100644 (file)
@@ -1,4 +1,136 @@
 ---
+###############
+# CMAKE Sonar #
+###############
+
+- lf_cmake_sonar: &lf_cmake_sonar
+    name: lf-cmake-sonar
+
+    ######################
+    # Default parameters #
+    ######################
+
+    branch: master
+    build-days-to-keep: 7
+    build-dir: '$WORKSPACE/target'
+    build-timeout: 15
+    cmake-opts: ''
+    cron: '@daily'
+    disable-job: false
+    git-url: '$GIT_URL/$PROJECT'
+    github-url: 'https://github.com'
+    install-prefix: '$BUILD_DIR/output'
+    make-opts: ''
+    pre-build: ''
+    sonar-scanner-version: 3.3.0.1492
+    sonarcloud-api-token: ''
+    sonarcloud-organization: ''
+    sonarcloud-project-key: ''
+    submodule-recursive: true
+    submodule-timeout: 10
+
+    #####################
+    # Job Configuration #
+    #####################
+
+    project-type: freestyle
+    node: '{build-node}'
+    disabled: '{disable-job}'
+
+    properties:
+      - lf-infra-properties:
+          build-days-to-keep: '{build-days-to-keep}'
+
+    parameters:
+      - lf-infra-parameters:
+          project: '{project}'
+          branch: '{branch}'
+          stream: '{stream}'
+          lftools-version: '{lftools-version}'
+      - lf-cmake-parameters:
+          build-dir: '{build-dir}'
+          cmake-opts: '{cmake-opts}'
+          install-prefix: '{install-prefix}'
+          make-opts: '{make-opts}'
+
+    wrappers:
+      - lf-infra-wrappers:
+          build-timeout: '{build-timeout}'
+          jenkins-ssh-credential: '{jenkins-ssh-credential}'
+
+    builders:
+      - lf-infra-pre-build
+      - shell: '{pre-build}'
+      - inject:
+          # Switch this to the sonar wrapper when JJB 2.0 is available
+          properties-content: |
+            SONAR_HOST_URL=https://sonarcloud.io
+            SONAR_SCANNER_VERSION={sonar-scanner-version}
+            PROJECT_KEY={sonarcloud-project-key}
+            PROJECT_ORGANIZATION={sonarcloud-organization}
+            API_TOKEN={sonarcloud-api-token}
+      - shell: !include-raw-escape: ../shell/cmake-sonar.sh
+      - lf-provide-maven-settings-cleanup
+
+    publishers:
+      - lf-infra-publish
+
+- job-template:
+    name: '{project-name}-cmake-sonar'
+    id: gerrit-cmake-sonar
+    concurrent: false
+    <<: *lf_cmake_sonar
+
+    scm:
+      - lf-infra-gerrit-scm:
+          branch: '$GERRIT_BRANCH'
+          jenkins-ssh-credential: '{jenkins-ssh-credential}'
+          git-url: '{git-url}'
+          refspec: '$GERRIT_REFSPEC'
+          submodule-recursive: '{submodule-recursive}'
+          submodule-timeout: '{submodule-timeout}'
+          choosing-strategy: gerrit
+
+    triggers:
+      - timed: '{obj:cron}'
+      - gerrit:
+          server-name: '{gerrit-server-name}'
+          trigger-on:
+            - comment-added-contains-event:
+                comment-contains-value: '^Patch Set[ ]+[0-9]+:([ ]+|[\n]+)(stage-release)$'
+          projects:
+            - project-compare-type: 'ANT'
+              project-pattern: '{project}'
+              branches:
+                - branch-compare-type: 'ANT'
+                  branch-pattern: '**/{branch}'
+
+- job-template:
+    name: '{project-name}-cmake-sonar'
+    id: github-cmake-sonar
+    concurrent: false
+    <<: *lf_cmake_sonar
+
+    properties:
+      - lf-infra-properties:
+          build-days-to-keep: '{build-days-to-keep}'
+      - github:
+          url: '{github-url}/{github-org}/{project}'
+
+    scm:
+      - lf-infra-github-scm:
+          url: '{git-clone-url}{github-org}/{project}'
+          refspec: '+refs/pull/*:refs/remotes/origin/pr/*'
+          branch: '$sha1'
+          submodule-recursive: '{submodule-recursive}'
+          submodule-timeout: '{submodule-timeout}'
+          choosing-strategy: default
+          jenkins-ssh-credential: '{jenkins-ssh-credential}'
+
+    triggers:
+      - timed: '{obj:cron}'
+
+
 ###############
 # CMAKE STAGE #
 ###############
diff --git a/shell/cmake-sonar.sh b/shell/cmake-sonar.sh
new file mode 100644 (file)
index 0000000..cd7ee71
--- /dev/null
@@ -0,0 +1,49 @@
+#!/bin/bash
+# SPDX-License-Identifier: EPL-1.0
+##############################################################################
+# Copyright (c) 2019 The Linux Foundation and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+##############################################################################
+echo "---> cmake-sonar.sh"
+
+build_dir="${BUILD_DIR:-$WORKSPACE/target}"
+cmake_opts="${CMAKE_OPTS:-}"
+make_opts="${MAKE_OPTS:-}"
+
+################
+# Script start #
+################
+
+set -ex -o pipefail
+
+cd /tmp || exit 1
+wget -O /tmp/sonar-scan.zip \
+    "https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SONAR_SCANNER_VERSION-linux.zip"
+unzip sonar-scan.zip
+sudo mv sonar-scanner-* /opt/sonar-scanner
+
+wget -O /tmp/bw.zip \
+    "https://sonarcloud.io/static/cpp/build-wrapper-linux-x86.zip"
+unzip bw.zip
+sudo mv build-wrapper-* /opt/build-wrapper
+
+mkdir -p "$build_dir"
+cd "$build_dir" || exit 1
+# $cmake_opts needs to wordsplit to pass options.
+# shellcheck disable=SC2086
+eval cmake -DCMAKE_INSTALL_PREFIX="$INSTALL_PREFIX" $cmake_opts ..
+
+/opt/build-wrapper/build-wrapper-linux-x86-64 --out-dir "$WORKSPACE/bw-output" \
+    make $make_opts
+
+/opt/sonar-scanner/bin/sonar-scanner \
+    -Dsonar.projectKey=${PROJECT_KEY} \
+    -Dsonar.organization=${PROJECT_ORGANIZATION} \
+    -Dsonar.sources=. \
+    -Dsonar.cfamily.build-wrapper-output="$WORKSPACE/bw-output" \
+    -Dsonar.host.url=${SONAR_HOST_URL} \
+    -Dsonar.login=${API_TOKEN}