SonarScanner in Dockerfile

·

1 min read

JavaScript

FROM node:16.16.0 AS builder
# Set an ARG to decide whether to run SONARSCAN
ARG SONAR_SCANNER_VERSION=5.0.1.3006
ARG SONARSCAN=false
ARG SONAR_TOKEN
ARG SONAR_PROJECT_KEY
ARG SONAR_HOST_URL
# Install Sonar Scanner 
RUN if [ "$SONARSCAN" = "true" ]; then \
        mkdir -p /opt \
        && curl -fSL https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-$SONAR_SCANNER_VERSION-linux.zip -o /opt/sonar-scanner.zip \
        && unzip -qq /opt/sonar-scanner.zip -d /opt \
        && mv /opt/sonar-scanner-${SONAR_SCANNER_VERSION}-linux /sonar-scanner \
        && rm /opt/sonar-scanner.zip \
        && ln -s /sonar-scanner/bin/sonar-scanner /bin/sonar-scanner ; \
    else \
        echo "SONARSCAN is disabled for this Run"; \
    fi
# Start Sonar Scanner
RUN if [ "$SONARSCAN" = "true" ]; then \
        sonar-scanner \
        -Dsonar.host.url="$SONAR_HOST_URL" \
        -Dsonar.projectKey="$SONAR_PROJECT_KEY" \
        -Dsonar.token="$SONAR_TOKEN" \
        -Dsonar.exclusions="**/__tests__/**,**/__mocks__, **/jest.config.ts, src/schema/schema.ts,src/api/dummyWebAPI.ts,src/schema/types/index.ts,src/schema/enums/index.ts,src/schema/inputs/index.ts" \
        -Dsonar.javascript.lcov.reportPaths=coverage/lcov.info ; \
    else \
      echo "SONARSCAN: Disabled"; \
    fi
COPY /prod/.npmrc .
RUN npm install -g pnpm
RUN pnpm install --force

# Copy everything over to Docker environment
COPY . ./

# The test command will force Jest to run in CI-mode, and tests will only run once instead of launching the watcher
ENV CI=true

RUN pnpm build
RUN pnpm run test:coverage

# Run Unit Tests
RUN if [ "$SONARSCAN" = "true" ]; then \
        sonar-scanner end -Dsonar.token="$SONAR_TOKEN" ; \
    else \
      echo "SONARSCAN is disabled for this Run" ; \
    fi

.NET

Coming Soon...