Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PipeCommand.tsx 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import CodeSnippet from '../../../common/CodeSnippet';
  22. import { CompilationInfo } from '../../components/CompilationInfo';
  23. import { BuildTools } from '../../types';
  24. export interface PipeCommandProps {
  25. branchesEnabled?: boolean;
  26. buildTool: BuildTools;
  27. projectKey: string;
  28. }
  29. const BUILD_TOOL_SPECIFIC = {
  30. [BuildTools.Gradle]: { image: 'gradle:jre11-slim', script: () => 'gradle sonarqube' },
  31. [BuildTools.Maven]: {
  32. image: 'maven:3.6.3-jdk-11',
  33. script: () => `
  34. - mvn verify sonar:sonar`
  35. },
  36. [BuildTools.DotNet]: {
  37. image: 'mcr.microsoft.com/dotnet/core/sdk:latest',
  38. script: (projectKey: string) => `
  39. - "apt-get update"
  40. - "apt-get install --yes openjdk-11-jre"
  41. - "dotnet tool install --global dotnet-sonarscanner"
  42. - "export PATH=\\"$PATH:$HOME/.dotnet/tools\\""
  43. - "dotnet sonarscanner begin /k:\\"${projectKey}\\" /d:sonar.login=\\"$SONAR_TOKEN\\" /d:\\"sonar.host.url=$SONAR_HOST_URL\\" "
  44. - "dotnet build"
  45. - "dotnet sonarscanner end /d:sonar.login=\\"$SONAR_TOKEN\\""`
  46. },
  47. [BuildTools.Other]: {
  48. image: `
  49. name: sonarsource/sonar-scanner-cli:latest
  50. entrypoint: [""]`,
  51. script: () => `
  52. - sonar-scanner`
  53. }
  54. };
  55. export default function PipeCommand({ projectKey, branchesEnabled, buildTool }: PipeCommandProps) {
  56. let command: string;
  57. if (buildTool === BuildTools.CFamily) {
  58. command = `image: <image ready for your build toolchain>
  59. cache:
  60. paths:
  61. - .sonar
  62. stages:
  63. - download
  64. - build
  65. - scan
  66. download:
  67. stage: download
  68. script:
  69. - mkdir -p .sonar
  70. - curl -sSLo build-wrapper-linux-x86.zip $SONAR_HOST_URL/static/cpp/build-wrapper-linux-x86.zip
  71. - unzip -o build-wrapper-linux-x86.zip -d .sonar
  72. build:
  73. stage: build
  74. script:
  75. - .sonar/build-wrapper-linux-x86/build-wrapper-linux-x86-64 --out-dir .sonar/bw-output <your clean build command>
  76. sonarqube-check:
  77. stage: scan
  78. script:
  79. - curl -sSLo sonar-scanner.zip https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.6.2.2472-linux.zip
  80. - unzip -o sonar-scanner.zip -d .sonar
  81. - .sonar/sonar-scanner-4.6.2.2472-linux/bin/sonar-scanner -Dsonar.cfamily.build-wrapper-output=.sonar/bw-output
  82. allow_failure: true`;
  83. } else {
  84. const onlyBlock = branchesEnabled
  85. ? `- merge_requests
  86. - master # or the name of your main branch
  87. - develop`
  88. : '- master # or the name of your main branch';
  89. const { image, script } = BUILD_TOOL_SPECIFIC[buildTool];
  90. command = `sonarqube-check:
  91. image: ${image}
  92. variables:
  93. SONAR_USER_HOME: "\${CI_PROJECT_DIR}/.sonar" # Defines the location of the analysis task cache
  94. GIT_DEPTH: "0" # Tells git to fetch all the branches of the project, required by the analysis task
  95. cache:
  96. key: "\${CI_JOB_NAME}"
  97. paths:
  98. - .sonar/cache
  99. script: ${script(projectKey)}
  100. allow_failure: true
  101. only:
  102. ${onlyBlock}
  103. `;
  104. }
  105. return (
  106. <>
  107. <CodeSnippet snippet={command} />
  108. {buildTool === BuildTools.CFamily && <CompilationInfo />}
  109. </>
  110. );
  111. }