{"id":4325,"date":"2020-12-13T14:41:18","date_gmt":"2020-12-13T20:41:18","guid":{"rendered":"https:\/\/coderrect.com\/?post_type=docs&p=4325"},"modified":"2021-09-04T05:43:36","modified_gmt":"2021-09-04T11:43:36","slug":"ci-cd-integration-and-installation","status":"publish","type":"docs","link":"https:\/\/coderrect.com\/docs\/ci-cd-integration-and-installation\/","title":{"rendered":"CI\/CD Integration and Installation"},"content":{"rendered":"\n

CI\/CD Integration and Installation<\/h1>\n\n\n\n

<\/p>\n\n\n\n

Coderrect and Jenkins Integration (Using Jenkinsfile)<\/h2>\n\n\n\n

This tutorial shows how to integrate Coderrect with Jenkins<\/a>, the most popular CI\/CD platform.<\/p>\n\n\n\n


\n\n\n\n

Coderrect<\/strong> is a static analysis tool for C\/C++ code. Unlike popular tools such as Cppcheck<\/a> and Clang static analyzer<\/a>, one of the powerful features of Coderrect is the detection of concurrency races caused by code using explicit multi-thread APIs (e.g. PThread<\/a>, std::thread<\/a>) or implicit parallel APIs (e.g. OpenMP<\/a>). <\/p>\n\n\n\n

Besides running Coderrect to scan the local source code via the command line, you can easily integrate it with Jenkins to include it in your CI\/CD pipeline so that Coderrect can be triggered automatically by a pull request, a code check-in, or a scheduled nightly build.<\/p>\n\n\n\n


\n\n\n\n

Overview<\/h4>\n\n\n\n

The diagram below illustrates the interaction among components in Coderrect-Jenkins integration.<\/p>\n\n\n\n

\"\"<\/figure>\n\n\n\n

When a build is scheduled Jenkins master asks Jenkins agents to prepare the source code (usually pull the latest code from a source control system), build the project, test and analysis the program. <\/p>\n\n\n\n

Upon receiving the request from Jenkins Master, the Jenkins agent runs Coderrect installed locally to analyze the code and report discoveries back to the master. <\/p>\n\n\n\n


\n\n\n\n

Install Coderrect<\/h4>\n\n\n\n

The first step is to install the Coderrect package to the Jenkins agent machine. Note that Coderrect just supports Linux for now. <\/p>\n\n\n\n

  1. Download the latest version of Coderrect from here<\/a> (the source can be found here<\/a>).<\/li>
  2. Unpack the downloaded tarball.<\/li>
  3. Add \/path\/to\/coderrect-installation\/bin into PATH.<\/li><\/ol>\n\n\n\n
    \n\n\n\n

    Install Coderrect Jenkins Plugin<\/h4>\n\n\n\n

    You can download Coderrect Jenkins plugin here. Then go to the \u201cAdvanced<\/strong>\u201d tab of \u201cManage Plugins<\/strong>\u201d page.<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    For \u201cUpload Plugin\u201d, click \u201cChoose File<\/strong>\u201d to select the downloaded coderrect.hpi<\/strong>. <\/p>\n\n\n\n


    \n\n\n\n

    Create a multi-branch project<\/h4>\n\n\n\n

    Now we can create a multi-branch project in Jenkins.<\/p>\n\n\n\n

    Go to Jenkins portal and click \u201cNew Item<\/strong>\u201c.<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    Enter the project name and select \u201cMultibranch Pipeline<\/strong>\u201d <\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    Set up the source control and build information. Make sure you use Jenkinsfile to control the build pipeline.<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n
    \n\n\n\n

    Create Jenkinsfile<\/h4>\n\n\n\n

    We already created a project in Jenkins. To let Jenkins know how to build, test and analyze the code, we have to create a file called Jenkinsfile under the root directory of your repository.<\/p>\n\n\n\n

    pipeline {\n    agent any\n\n    stages {\n        stage('Build') {\n            steps {\n                sh \"coderrect make\"\n            }\n        }\n\n        stage('Analysis') {\n            steps {\n                publishCoderrect \"\"\n            }\n        }\n    }\n}<\/pre>\n\n\n\n

    The Jenkinsfile above defines a pipeline with two stages. The \u201cbuild\u201d stage builds the project and runs coderrect to analyze the generated binary. The \u201canalysis\u201d stage reports the analysis results to the master.<\/p>\n\n\n\n

    You may be aware that the command \u201cpublishCoderrect\u201d has a parameter whose value is \u201c\u201d in our example. What is it? It is the relative path (to the project root directory) where you executes \u201ccoderrect\u201d. We execute \u201ccoderrect\u201d immediately under the project root so that we specify its value to \u201c\u201d.<\/p>\n\n\n\n

    If we execute \u201ccoderrect\u201d under a directory rather than the project root we need to set up this parameter correctly. For example, the following figure shows typical build steps for a cmake-based project:<\/p>\n\n\n\n

    # we are under the project root directory\nmkdir build\ncd build\ncmake ..\ncoderrect make<\/pre>\n\n\n\n

    In this case you execute \u201ccoderrect\u201d under project-root\/build<\/em><\/strong>. You need to set the build directory to \u201cbuild\u201d.<\/p>\n\n\n\n

    #pipeline {\n    agent any\n\n    stages {\n        stage('Build') {\n            steps {\n                sh \"mkdir build && cd build && cmake .. && coderrect make\"\n            }\n        }\n\n        stage('Analysis') {\n            steps {\n                     publishCoderrect \"build\"\n            }\n        }\n    }\n}<\/pre>\n\n\n\n

    Now we are done! We can go to the project dashboard and manually trigger a build.<\/p>\n\n\n\n


    \n\n\n\n

    Trigger a Build<\/h4>\n\n\n\n
    \"This<\/figure>\n\n\n\n

    Coderrect adds a section into the build summary page. The section shows the number of races in different categories for this build and the previous build. For example, the demo project above has 13 data races.<\/p>\n\n\n\n

    You can click the \u201cCoderrect\u201d item in the navigation bar to see details.<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    The detail report has three views \u2013 full, new and solved. The \u201cfull\u201d view shows all races found from the current build. The \u201cnew\u201d view shows just races introduced by the new committed code since previous build. The \u201csolved\u201d view shows races fixed by the new code.<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    I hope you enjoy this post. Please send to feedback@coderrect.com if you have any feedback.<\/p>\n\n\n\n


    \n\n\n\n

    Coderrect and Jenkins Integration (Using freestyle project)<\/h2>\n\n\n\n

    This tutorial shows how to integrate Coderrect with Jenkins<\/a>, the most popular CI\/CD platform.<\/p>\n\n\n\n


    \n\n\n\n

    Coderrect<\/strong> is a static analysis tool for C\/C++ code. Unlike popular tools such as Cppcheck<\/a> and Clang static analyzer<\/a>, one of the powerful features of Coderrect is the detection of concurrency races caused by code using explicit multi-thread APIs (e.g. PThread<\/a>, std::thread<\/a>) or implicit parallel APIs (e.g. OpenMP<\/a>). <\/p>\n\n\n\n

    Besides running Coderrect to scan the local source code via the command line, you can easily integrate it with Jenkins to include it in your CI\/CD pipeline so that Coderrect can be triggered automatically by a pull request, a code check-in, or a scheduled nightly build.<\/p>\n\n\n\n


    \n\n\n\n

    Overview<\/h3>\n\n\n\n

    The diagram below illustrates the interaction among components in Coderrect-Jenkins integration.<\/p>\n\n\n\n

    \"\"<\/figure>\n\n\n\n

    When a build is scheduled Jenkins master asks Jenkins agents to prepare the source code (usually pull the latest code from a source control system), build the project, test and analysis the program. <\/p>\n\n\n\n

    Upon receiving the request from Jenkins Master, the Jenkins agent runs Coderrect installed locally to analyze the code and report discoveries back to the master. <\/p>\n\n\n\n


    \n\n\n\n

    Install Coderrect<\/h3>\n\n\n\n

    The first step is to install the Coderrect package to the Jenkins agent machine. Note that Coderrect just supports Linux for now. <\/p>\n\n\n\n

    1. Download the latest version of Coderrect from here<\/a> (the source can be found here<\/a>).<\/li>
    2. Unpack the downloaded tarball.<\/li>
    3. Add \/path\/to\/coderrect-installation\/bin into PATH.<\/li><\/ol>\n\n\n\n
      \n\n\n\n

      Install Coderrect Jenkins Plugin<\/h3>\n\n\n\n

      You can download Coderrect Jenkins plugin file here<\/a>. Then go to the \u201cAdvanced<\/strong>\u201d tab of \u201cManage Plugins<\/strong>\u201d page.<\/p>\n\n\n\n

      \"\"<\/figure>\n\n\n\n

      For \u201cUpload Plugin\u201d, click \u201cChoose File<\/strong>\u201d to select the downloaded coderrect.hpi<\/strong>. <\/p>\n\n\n\n


      \n\n\n\n

      Create a Freestyle project<\/h3>\n\n\n\n

      Now we can create a freestyle project in Jenkins.<\/p>\n\n\n\n

      Go to Jenkins portal and click \u201cNew Item<\/strong>\u201c.<\/p>\n\n\n\n

      \"\"<\/figure>\n\n\n\n

      Enter the project name (TDEngine here) and select \u201cFreestyle project<\/strong>\u201d <\/p>\n\n\n\n

      \"\"<\/figure>\n\n\n\n

      Set up the source control and build information. For the build stage, I create the following script commands<\/p>\n\n\n\n

      \"\"<\/figure>\n\n\n\n

      The last step \u201ccoderrect -e taosd -t make\u201d tells coderrect to<\/p>\n\n\n\n