Sonarscanner MSBuild tool is not running in pipeline - Jenkins - asp.net

I am running this following pipeline which has stage "Build + Sonarscanner Analysis" for dotnetcore 2.2
The stage is set as follows
I have installed the tool in Global Configuration as suggested by
Sonarqube Documentation
Defined the tool in the environment as following
// Tools
MSBUILD_SQ_SCANNER_HOME = tool name: 'Scanner_for_MSBuild_4.7', type: 'hudson.plugins.sonar.MsBuildSQRunnerInstallation'
Pipeline Stage
stage ('Build + SonarQube analysis') {
agent {
docker {
image 'mcr.microsoft.com/dotnet/core/sdk:2.2'
}
}
steps {
dir ("app") {
withSonarQubeEnv('local') {
sh "dotnet ${MSBUILD_SQ_SCANNER_HOME}/SonarScanner.MSBuild.dll begin /k:\"Testing-Local\""
sh "dotnet build ${env.DotnetProjectName}"
sh "dotnet ${MSBUILD_SQ_SCANNER_HOME}/SonarScanner.MSBuild.dll end"
}
}
}
}
Result
I am getting the SonarScanner.MSBuild.dll is not executable as seen below
Verfication
The dll exists and the permissions is assigned to Jenkins
The dll is executable
The dll when run manually in that path - it runs
Directly added the path for the dll, It has the same result
stage ('Build + SonarQube analysis') {
agent {
docker {
image 'mcr.microsoft.com/dotnet/core/sdk:2.2'
}
}
steps {
dir ("app") {
withSonarQubeEnv('local') {
sh "dotnet /var/lib/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/Scanner_for_MSBuild_4.7/SonarScanner.MSBuild.dll begin /k:\"Testing-Local\""
sh "dotnet build ${env.DotnetProjectName}"
sh "dotnet var/lib/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/Scanner_for_MSBuild_4.7/SonarScanner.MSBuild.dll end"
}
}
}
}
Thanks for the help in advance.

I was able to solve this,
1. Installed the SonarScanner for dotnetcore in Jenkins Tools
The path for the sonarscanner will be the same as before
Default Path
/var/lib/jenkins/tools/hudson.plugins.sonar.MsBuildSQRunnerInstallation/Scanner_for_MSBuild_4.7/SonarScanner.MSBuild.dll
2. Initialize the tool in Jenkinsfile
// Tools
MSBUILD_SQ_SCANNER_HOME = tool name: 'Scanner_for_MSBuild_4.7', type: 'hudson.plugins.sonar.MsBuildSQRunnerInstallation'
stage ('Build + SonarQube analysis')
agent {
docker {
image 'mcr.microsoft.com/dotnet/core/sdk:2.2'
args '-v ${MSBUILD_SQ_SCANNER_HOME}:/opt/sonarscanner'
}
}
steps {
dir ("app") {
withSonarQubeEnv('local') {
sh "dotnet /opt/sonarscanner/SonarScanner.MSBuild.dll begin /k:\"Testing-Local\""
sh "dotnet build ${env.DotnetProjectName}"
sh "dotnet /opt/sonarscanner/SonarScanner.MSBuild.dll end"
}
}
}
}
How it works?
I am mounting the sonarscanner to the official docker image at the path /opt/sonarscanner/
With the mounted file as an argument for docker container during initialization, now the dll is avaliable for the docker dotnet command

Related

How to stop build through dockerfile when unit test failed (Dockerfile + .NET Framework + Nunit Test + Docker Window Container

I have to build stop when unit test fail through docker file. I am using .NET Framework (not Dot.Net Core). I have to write command either in power shell script or Docker file (if Unit test fail then build should be stop before execute next line of docker file.
.NET Framework 4.7.2
Docker Window container only
Unit test project (Nunit)
Asp.Net WebForms (old)
Nunit test runner
Steps,
I have create project for Unit test + WebForm + Class library.
I have created powershell script for run unit test project using Nunit test runner.
I have added path for script in docker file.
I have attached Docker file and power-shell script .
Docker file code here
`# escape=`
FROM mcr.microsoft.com/dotnet/framework/sdk:4.8-20200512-windowsservercore-ltsc2019
WORKDIR /src
COPY ./POC.Web/POC.Web.csproj ./POC.Web/
COPY ./POC.Entites/POC.Entites.csproj ./POC.Entites/
COPY ./POC.Business.Layer/POC.Business.Layer.csproj ./POC.Business.Layer/
COPY ./POC.UnitTests/POC.UnitTests.csproj ./POC.UnitTests/
COPY ./POC.UnitTests/packages.config ./POC.UnitTests/
COPY ./MttcPoc.sln .
RUN nuget restore MttcPoc.sln
COPY . .
RUN msbuild POC.UnitTests/POC.UnitTests.csproj /p:OutputPath=c:/out/tests
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop';"]
# testResult variable using in shellscript to get the result of unit test
ENV testResult="Passed"
RUN nuget install NUnit.Runners
COPY ./startuptest.ps1 /
RUN /startuptest.ps1
#RUN IF $testResult == "Passed" EXIT 0 ELSE EXIT -1
RUN if [ "$testResult" = "Passed" ] ; then true ; else false; fi
ENTRYPOINT ["powershell", "/startuptest.ps1"]
#*****************************************************
#Below line should not execute if unit test fail
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-20200512-windowsservercore-ltsc2019
ENV APP_ROOT=/inetpub/wwwroot `
CONNECTIONSTRINGS_CONFIG_MAP="" `
dockercomposeKey="From Docker file"
WORKDIR ${APP_ROOT}
RUN Import-Module WebAdministration; `
Set-ItemProperty -Path 'IIS:\AppPools\DefaultAppPool' -Name processModel.identityType -Value LocalSystem; `
New-WebApplication -Name 'app' -Site 'Default Web Site' -PhysicalPath $env:APP_ROOT
COPY ./startup.ps1 /
RUN /startup.ps1
#ENTRYPOINT ["powershell", "/startup.ps1"]
COPY --from=build c:/out/_PublishedWebsites/POC.Web ${APP_ROOT}
#************************************************************************`
Powershell script code here
$ProjectDir = "."
$OutDir = "$ProjectDir\testresults"
$testProjectPath = "c:\out\tests\POC.UnitTests.dll"
# Set nunit path test runner
$nunit_path = "$ProjectDir\NUnit.ConsoleRunner.3.11.1\tools\nunit3-console.exe"
# Run NUnit3 tests
& $nunit_path $testProjectPath --framework=net-4.7.2 --work=$OutDir
[XML]$resultDetails = Get-Content .\$OutDir\TestResult.xml
Write-Output $resultDetails
$string = "test-run"
Write-Output $resultDetails.$string.result
$env:testResult = $resultDetails.$string.result
#Passed
#Failed
#Below line added for if test fail then exit from docker build
If ($resultDetails.$string.result -eq 'Passed') {
Write-Output "Test passed"
Write-Output $env:testResult
return true
}else{
Write-Output "Test failed"
Write-Output $env:testResult
return -1
}

SBT terminates silently when Git repository for build plugin can't be downloaded

SBT is silently failing when it can't download a plugin via SSH from a Git repository.
This is the output of SBT when it's trying to download the repository:
[info] Updating ProjectRef(uri("ssh://git#repository.com/plugin.git"), "plugin")...
# (nothing after that line)
And it just terminates after that with no explanation. This is very likely a bug with SBT's downloading of plugins via SSH from a Git repository.
When downloading the plugin succeeds, this line is printed:
[info] Done updating.
So for some reason, SBT isn't stating what's wrong, even when executed like this:
sbt -Xdebug test
Here are the relevant configuration files:
# project/build-properties
sbt.version=1.1.5
# project/plugins.sbt
lazy val buildPlugin = RootProject(uri("ssh://git#repository.com/plugin.git"))
lazy val root = (project in file(".")).dependsOn(buildPlugin)
Questions:
1. How can I get SBT to print more debugging information?
2. Where in the SBT code could I fix this bug?
3. How can I build and use my own version of SBT?
How can I get SBT to print more debugging information?
Using the latest launching script available from https://www.scala-sbt.org/download.html (1.2.1 as of August, 2018), you can run:
$ sbt -debug
Where in the SBT code could I fix this bug?
See my answer here https://github.com/sbt/sbt/issues/1120#issuecomment-415553592:
Here are some of the relevant code:
Load.builtinLoader - https://github.com/sbt/sbt/blob/v1.2.1/main/src/main/scala/sbt/internal/Load.scala#L480-L488
RetrieveUnit - https://github.com/sbt/sbt/blob/v1.2.1/main/src/main/scala/sbt/internal/RetrieveUnit.scala
Resolvers.git - https://github.com/sbt/sbt/blob/v1.2.1/main/src/main/scala/sbt/Resolvers.scala#L82-L101
Resolvers.creates - https://github.com/sbt/sbt/blob/v1.2.1/main/src/main/scala/sbt/Resolvers.scala#L145-L155
val git: Resolver = (info: ResolveInfo) => {
val uri = info.uri.withoutMarkerScheme
val localCopy = uniqueSubdirectoryFor(uri.copy(scheme = "git"), in = info.staging)
val from = uri.withoutFragment.toASCIIString
if (uri.hasFragment) {
val branch = uri.getFragment
Some { () =>
creates(localCopy) {
run("git", "clone", from, localCopy.getAbsolutePath)
run(Some(localCopy), "git", "checkout", "-q", branch)
}
}
} else
Some { () =>
creates(localCopy) {
run("git", "clone", "--depth", "1", from, localCopy.getAbsolutePath)
}
}
}
....
def creates(file: File)(f: => Unit) = {
if (!file.exists)
try {
f
} catch {
case NonFatal(e) =>
IO.delete(file)
throw e
}
file
}
How can I build and use my own version of SBT?
https://github.com/sbt/sbt/blob/1.x/CONTRIBUTING.md#build-from-source
For this, you just need sbt/sbt, and publishLocal.

Jenkins - Symfony with environment variables

I've been struggling in building automated build using Jenkins with symfony 3.4.
How to properly set environment variables in Jenkins that symfony can find it.
here's my pipeline.
node {
def app
stage('composer install') {
sh 'export $(cat env/env_vars | xargs)'
sh 'composer install --optimize-autoloader'
}
stage('yarn install') {
sh 'yarn install'
}
stage ('build assets') {
sh 'yarn encore production'
}
stage('Clone repository') {
// clone
}
stage('Build image') {
// build here
}
stage('Push image') {
// push here
}
}
then after I run my build.
I always got this message
....
Creating the "app/config/parameters.yml" file
Some parameters are missing. Please provide them.
database_host ('%env(DATABASE_HOST)%'): Script Incenteev\ParameterHandler
\ScriptHandler::buildParameters handling the symfony-scripts event terminated with an exception
[Symfony\Component\Console\Exception\RuntimeException]
Aborted
....
I already used some jenkins plugin like EnvInjector and something similar. But still symfony can't find my environment variables.
You can probably solve this like this:
stage('composer install') {
sh 'export $(cat env/env_vars | xargs) && composer install --optimize-autoloader'
}
This will make the environment variables available in the same shell session.

gruntjs: how to move a file out of the way

I need to move a file out of the way before one of my Grunt tasks run, and then put it back after the task has completed.
How do I do this with GruntJS?
Basically I want to run this command:
# move node-webkit out of the way
mv app/node-webkit ./tmp
# run grunt task
# move node-webkit back
mv ./tmp/node-webkit ./app/
Yeah, have a look at grunt-shell. In your init config:
shell: {
move: {
command: 'mv app/node-webkit ./tmp'
},
moveback: {
command: 'mv ./tmp/node-webkit ./app/'
}
}
Then, register a function that runs the move command before the other tasks you want to run, then run the moveback task.
module.exports = function(grunt) {
'use strict';
grunt.registerTask('mytask', [
'shell:move',
'othertaskshere',
'shell:moveback'
]);
};

Symfony 2.2.1 rsync deploy - not working on remote server

I'm very new to Symfony and I'm trying to automate the deploy process with rsync, while keeping both the local and remote installs of Symfony working.
What I've done so far:
installed Cygwin on my local machine (Windows 7+Apache2.2+PHP 5.3+MySQL 5.1)
done a basic Symfony install on my local machine from shell with the command
php composer.phar create-project symfony/framework-standard-edition [path]/ 2.2.1
set up a remote LAMP Ubuntu server with php-fpm (fastcgi)
set up two different configuration files for local and remote in the app/config/ dir, parameters.yml and parameters.yml.remote
created an app/config/rsync_exclude.txt file containing a list of files not to rsync to the remote server (as suggested in this page)
created a deploy shell script that I run from Cygwin (see below)
The deploy script issues the commands:
rsync -avz /cygdrive/c/[path]/ user#server:[remote-path]/ --exclude-from=/cygdrive/c/[path]/app/config/rsync_exclude.txt
ssh user#server 'cd [remote-path]/ && php app/console --env=prod cache:clear && php app/console cache:clear'
ssh user#server 'mv [remote-path]/app/config/parameters.yml.remote ~/[remote-path]/app/config/parameters.yml'
The rsync, ssh and mv commands work, but the deployed site shows always a HTTP 500 error (both app.php and app_dev.php).
Looking at server error log the error is:
Fatal error: Class 'Composer\\Autoload\\ClassLoader' not found in /[remote-path]/vendor/composer/autoload_real.php on line 23
Any clue would be more than welcome.
Edit - here is my vendor/composer/autoload_real.php file (sorry for the making the question longer!):
<?php
// autoload_real.php generated by Composer
class ComposerAutoloaderInit9d50f07556e53717271b583e52c7de25
{
private static $loader;
public static function loadClassLoader($class)
{
if ('Composer\Autoload\ClassLoader' === $class) {
require __DIR__ . '/ClassLoader.php';
}
}
public static function getLoader()
{
if (null !== self::$loader) {
return self::$loader;
}
spl_autoload_register(array('ComposerAutoloaderInit9d50f07556e53717271b583e52c7de25', 'loadClassLoader'), true, true);
self::$loader = $loader = new \Composer\Autoload\ClassLoader();
// ^^^^^^ this is line 23 and gives the error ^^^^^^^^^^^
spl_autoload_unregister(array('ComposerAutoloaderInit9d50f07556e53717271b583e52c7de25', 'loadClassLoader'));
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
$map = require __DIR__ . '/autoload_namespaces.php';
foreach ($map as $namespace => $path) {
$loader->add($namespace, $path);
}
$classMap = require __DIR__ . '/autoload_classmap.php';
if ($classMap) {
$loader->addClassMap($classMap);
}
$loader->register(true);
require $vendorDir . '/kriswallsmith/assetic/src/functions.php';
require $vendorDir . '/swiftmailer/swiftmailer/lib/swift_required.php';
return $loader;
}
}
If there is an error with the autoloader generated by composer, performing ...
composer update
... will update your dependencies and create a new one.
You should invoke the command with the -o flag if you are deploying to a production system.
This way composer generates a classmap autoloader ( which performs way better ) instead of the classic autoloader.
composer update -o
I guess re-generating the autoloader will solve the issue :)

Resources